a lil bit refactoring

This commit is contained in:
Egor Aristov 2025-11-08 10:49:08 +03:00
parent 10ac5dfae8
commit 45eae71fe3
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
4 changed files with 30 additions and 38 deletions

View File

@ -1,5 +1,11 @@
package common package common
import (
"fmt"
"efprojects.com/kitten-ipc/kitcom/api"
)
type Parser struct { type Parser struct {
Files []string Files []string
} }
@ -7,3 +13,21 @@ type Parser struct {
func (p *Parser) AddFile(path string) { func (p *Parser) AddFile(path string) {
p.Files = append(p.Files, path) p.Files = append(p.Files, path)
} }
func (p *Parser) MapFiles(parseFile func(path string) ([]api.Endpoint, error)) (*api.Api, error) {
var apis api.Api
for _, f := range p.Files {
endpoints, err := parseFile(f)
if err != nil {
return nil, fmt.Errorf("parse file: %w", err)
}
apis.Endpoints = append(apis.Endpoints, endpoints...)
}
if len(apis.Endpoints) == 0 {
return nil, fmt.Errorf("no endpoints found")
}
return &apis, nil
}

View File

@ -2,7 +2,5 @@ github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e h1:Lf/gRko
github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e/go.mod h1:uNVvRXArCGbZ508SxYYTC5v1JWoz2voff5pm25jU1Ok= github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e/go.mod h1:uNVvRXArCGbZ508SxYYTC5v1JWoz2voff5pm25jU1Ok=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=

View File

@ -17,26 +17,11 @@ type GoApiParser struct {
*common.Parser *common.Parser
} }
func (g *GoApiParser) Parse() (*api.Api, error) { func (p *GoApiParser) Parse() (*api.Api, error) {
return p.MapFiles(p.parseFile)
var apis api.Api
for _, f := range g.Files {
endpoints, err := g.parseFile(f)
if err != nil {
return nil, fmt.Errorf("parse file: %w", err)
}
apis.Endpoints = append(apis.Endpoints, endpoints...)
}
if len(apis.Endpoints) == 0 {
return nil, fmt.Errorf("no endpoints found")
}
return &apis, nil
} }
func (g *GoApiParser) parseFile(sourceFile string) ([]api.Endpoint, error) { func (p *GoApiParser) parseFile(sourceFile string) ([]api.Endpoint, error) {
var endpoints []api.Endpoint var endpoints []api.Endpoint
fileSet := token.NewFileSet() fileSet := token.NewFileSet()

View File

@ -21,26 +21,11 @@ type TypescriptApiParser struct {
*common.Parser *common.Parser
} }
func (t *TypescriptApiParser) Parse() (*api.Api, error) { func (p *TypescriptApiParser) Parse() (*api.Api, error) {
return p.MapFiles(p.parseFile)
var apis api.Api
for _, f := range t.Files {
endpoints, err := t.parseFile(f)
if err != nil {
return nil, fmt.Errorf("parse file: %w", err)
}
apis.Endpoints = append(apis.Endpoints, endpoints...)
}
if len(apis.Endpoints) == 0 {
return nil, fmt.Errorf("no endpoints found")
}
return &apis, nil
} }
func (t *TypescriptApiParser) parseFile(sourceFilePath string) ([]api.Endpoint, error) { func (p *TypescriptApiParser) parseFile(sourceFilePath string) ([]api.Endpoint, error) {
var endpoints []api.Endpoint var endpoints []api.Endpoint
f, err := os.Open(sourceFilePath) f, err := os.Open(sourceFilePath)