stubs for codegen

This commit is contained in:
Egor Aristov 2025-10-16 13:07:39 +03:00
parent a4029e2399
commit 8552a1d7da
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
5 changed files with 61 additions and 16 deletions

View File

@ -1 +1,9 @@
package main
type GoApiGenerator struct {
}
func (g *GoApiGenerator) Generate(api *Api, destFile string) error {
//TODO implement me
panic("implement me")
}

View File

@ -19,14 +19,14 @@ type apiStruct struct {
type GoApiParser struct {
}
func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
func (g *GoApiParser) Parse(sourceFile string) (*Api, error) {
var apiStructs []*apiStruct
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, sourceFile, nil, parser.ParseComments|parser.SkipObjectResolution)
if err != nil {
return Api{}, fmt.Errorf("parse file: %w", err)
return nil, fmt.Errorf("parse file: %w", err)
}
pkgName := astFile.Name.Name
@ -65,11 +65,11 @@ func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
}
if len(apiStructs) == 0 {
return Api{}, fmt.Errorf("no api struct found")
return nil, fmt.Errorf("no api struct found")
}
if len(apiStructs) > 1 {
return Api{}, fmt.Errorf("multiple api struct found")
return nil, fmt.Errorf("multiple api struct found")
}
for _, decl := range astFile.Decls {
@ -106,5 +106,5 @@ func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
}
}
return Api{}, nil
return nil, nil
}

View File

@ -31,7 +31,11 @@ type Api struct {
}
type ApiParser interface {
Parse(sourceFile string) (Api, error)
Parse(sourceFile string) (*Api, error)
}
type ApiGenerator interface {
Generate(api *Api, destFile string) error
}
func main() {
@ -51,9 +55,10 @@ func main() {
log.Panic(err)
}
//Dest, err = filepath.Abs(Dest)
//if err != nil {
//}
destAbs, err := filepath.Abs(Dest)
if err != nil {
log.Panic(err)
}
if err := checkIsFile(srcAbs); err != nil {
log.Panic(err)
@ -64,10 +69,19 @@ func main() {
log.Panic(err)
}
_, err = apiParser.Parse(srcAbs)
api, err := apiParser.Parse(srcAbs)
if err != nil {
log.Panic(err)
}
apiGenerator, err := apiGeneratorByExt(destAbs)
if err != nil {
log.Panic(err)
}
if err := apiGenerator.Generate(api, destAbs); err != nil {
log.Panic(err)
}
}
func checkIsFile(src string) error {
@ -95,3 +109,18 @@ func apiParserByExt(src string) (ApiParser, error) {
return nil, fmt.Errorf("unsupported file extension: %s", path.Ext(src))
}
}
func apiGeneratorByExt(dest string) (ApiGenerator, error) {
switch path.Ext(dest) {
case ".go":
return &GoApiGenerator{}, nil
case ".ts":
return &TypescriptApiGenerator{}, nil
case ".js":
return nil, fmt.Errorf("vanilla javascript is not supported and never will be")
case "":
return nil, fmt.Errorf("could not find file extension for %s", dest)
default:
return nil, fmt.Errorf("unsupported file extension: %s", path.Ext(dest))
}
}

View File

@ -1 +1,9 @@
package main
type TypescriptApiGenerator struct {
}
func (g *TypescriptApiGenerator) Generate(api *Api, destFile string) error {
//TODO implement me
panic("implement me")
}

View File

@ -21,17 +21,17 @@ type TypescriptApiParser struct {
type apiClass struct {
}
func (t *TypescriptApiParser) Parse(sourceFilePath string) (Api, error) {
func (t *TypescriptApiParser) Parse(sourceFilePath string) (*Api, error) {
f, err := os.Open(sourceFilePath)
if err != nil {
return Api{}, fmt.Errorf("failed to open file: %w", err)
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
fileContents, err := io.ReadAll(f)
if err != nil {
return Api{}, fmt.Errorf("failed to read file: %w", err)
return nil, fmt.Errorf("failed to read file: %w", err)
}
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
@ -74,12 +74,12 @@ func (t *TypescriptApiParser) Parse(sourceFilePath string) (Api, error) {
})
if len(apiClasses) == 0 {
return Api{}, fmt.Errorf("no api class found")
return nil, fmt.Errorf("no api class found")
}
if len(apiClasses) > 1 {
return Api{}, fmt.Errorf("multiple api classes found")
return nil, fmt.Errorf("multiple api classes found")
}
return Api{}, nil
return nil, nil
}