stubs for codegen
This commit is contained in:
parent
a4029e2399
commit
8552a1d7da
@ -1 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
type GoApiGenerator struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GoApiGenerator) Generate(api *Api, destFile string) error {
|
||||||
|
//TODO implement me
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|||||||
@ -19,14 +19,14 @@ type apiStruct struct {
|
|||||||
type GoApiParser struct {
|
type GoApiParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
|
func (g *GoApiParser) Parse(sourceFile string) (*Api, error) {
|
||||||
|
|
||||||
var apiStructs []*apiStruct
|
var apiStructs []*apiStruct
|
||||||
|
|
||||||
fileSet := token.NewFileSet()
|
fileSet := token.NewFileSet()
|
||||||
astFile, err := parser.ParseFile(fileSet, sourceFile, nil, parser.ParseComments|parser.SkipObjectResolution)
|
astFile, err := parser.ParseFile(fileSet, sourceFile, nil, parser.ParseComments|parser.SkipObjectResolution)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Api{}, fmt.Errorf("parse file: %w", err)
|
return nil, fmt.Errorf("parse file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pkgName := astFile.Name.Name
|
pkgName := astFile.Name.Name
|
||||||
@ -65,11 +65,11 @@ func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(apiStructs) == 0 {
|
if len(apiStructs) == 0 {
|
||||||
return Api{}, fmt.Errorf("no api struct found")
|
return nil, fmt.Errorf("no api struct found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(apiStructs) > 1 {
|
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 {
|
for _, decl := range astFile.Decls {
|
||||||
@ -106,5 +106,5 @@ func (g *GoApiParser) Parse(sourceFile string) (Api, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Api{}, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,11 @@ type Api struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ApiParser interface {
|
type ApiParser interface {
|
||||||
Parse(sourceFile string) (Api, error)
|
Parse(sourceFile string) (*Api, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ApiGenerator interface {
|
||||||
|
Generate(api *Api, destFile string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -51,9 +55,10 @@ func main() {
|
|||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Dest, err = filepath.Abs(Dest)
|
destAbs, err := filepath.Abs(Dest)
|
||||||
//if err != nil {
|
if err != nil {
|
||||||
//}
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := checkIsFile(srcAbs); err != nil {
|
if err := checkIsFile(srcAbs); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
@ -64,10 +69,19 @@ func main() {
|
|||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = apiParser.Parse(srcAbs)
|
api, err := apiParser.Parse(srcAbs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
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 {
|
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))
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
type TypescriptApiGenerator struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *TypescriptApiGenerator) Generate(api *Api, destFile string) error {
|
||||||
|
//TODO implement me
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|||||||
@ -21,17 +21,17 @@ type TypescriptApiParser struct {
|
|||||||
type apiClass 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)
|
f, err := os.Open(sourceFilePath)
|
||||||
if err != nil {
|
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()
|
defer f.Close()
|
||||||
|
|
||||||
fileContents, err := io.ReadAll(f)
|
fileContents, err := io.ReadAll(f)
|
||||||
if err != nil {
|
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{
|
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
||||||
@ -74,12 +74,12 @@ func (t *TypescriptApiParser) Parse(sourceFilePath string) (Api, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if len(apiClasses) == 0 {
|
if len(apiClasses) == 0 {
|
||||||
return Api{}, fmt.Errorf("no api class found")
|
return nil, fmt.Errorf("no api class found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(apiClasses) > 1 {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user