From 8552a1d7da7da947331d23e03d372236e4239769 Mon Sep 17 00:00:00 2001 From: Egor Aristov Date: Thu, 16 Oct 2025 13:07:39 +0300 Subject: [PATCH] stubs for codegen --- kitcom/gogen.go | 8 ++++++++ kitcom/goparser.go | 10 +++++----- kitcom/main.go | 39 ++++++++++++++++++++++++++++++++++----- kitcom/tsgen.go | 8 ++++++++ kitcom/tsparser.go | 12 ++++++------ 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/kitcom/gogen.go b/kitcom/gogen.go index 06ab7d0..d402be3 100644 --- a/kitcom/gogen.go +++ b/kitcom/gogen.go @@ -1 +1,9 @@ package main + +type GoApiGenerator struct { +} + +func (g *GoApiGenerator) Generate(api *Api, destFile string) error { + //TODO implement me + panic("implement me") +} diff --git a/kitcom/goparser.go b/kitcom/goparser.go index a695a5d..5500d9f 100644 --- a/kitcom/goparser.go +++ b/kitcom/goparser.go @@ -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 } diff --git a/kitcom/main.go b/kitcom/main.go index 5a01311..1977b72 100644 --- a/kitcom/main.go +++ b/kitcom/main.go @@ -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)) + } +} diff --git a/kitcom/tsgen.go b/kitcom/tsgen.go index 06ab7d0..9abfda1 100644 --- a/kitcom/tsgen.go +++ b/kitcom/tsgen.go @@ -1 +1,9 @@ package main + +type TypescriptApiGenerator struct { +} + +func (g *TypescriptApiGenerator) Generate(api *Api, destFile string) error { + //TODO implement me + panic("implement me") +} diff --git a/kitcom/tsparser.go b/kitcom/tsparser.go index d9917b5..cc968f9 100644 --- a/kitcom/tsparser.go +++ b/kitcom/tsparser.go @@ -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 }