feature: refactor api parse to g4 (#365)
* feature: refactor api parse to g4 * new g4 parser * add CHANGE_LOG.MD * refactor * fix byte bug * refactor * optimized * optimized * revert * update readme.md * update readme.md * update readme.md * update readme.md * remove no need * fix java gen * add upgrade * resolve confilits Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -21,11 +21,7 @@ func KtCommand(c *cli.Context) error {
|
||||
return errors.New("missing -pkg")
|
||||
}
|
||||
|
||||
p, e := parser.NewParser(apiFile)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
api, e := p.Parse()
|
||||
api, e := parser.Parse(apiFile)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ktgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -44,7 +45,7 @@ func parseType(t string) string {
|
||||
}
|
||||
|
||||
if strings.HasPrefix(t, "map") {
|
||||
tys, e := util.DecomposeType(t)
|
||||
tys, e := decomposeType(t)
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
@@ -68,6 +69,47 @@ func parseType(t string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func decomposeType(t string) (result []string, err error) {
|
||||
add := func(tp string) error {
|
||||
ret, err := decomposeType(tp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result = append(result, ret...)
|
||||
return nil
|
||||
}
|
||||
if strings.HasPrefix(t, "map") {
|
||||
t = strings.ReplaceAll(t, "map", "")
|
||||
if t[0] == '[' {
|
||||
pos := strings.Index(t, "]")
|
||||
if pos > 1 {
|
||||
if err = add(t[1:pos]); err != nil {
|
||||
return
|
||||
}
|
||||
if len(t) > pos+1 {
|
||||
err = add(t[pos+1:])
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(t, "[]") {
|
||||
if len(t) > 2 {
|
||||
err = add(t[2:])
|
||||
return
|
||||
}
|
||||
} else if strings.HasPrefix(t, "*") {
|
||||
err = add(t[1:])
|
||||
return
|
||||
} else {
|
||||
result = append(result, t)
|
||||
return
|
||||
}
|
||||
|
||||
err = fmt.Errorf("bad type %q", t)
|
||||
return
|
||||
}
|
||||
|
||||
func add(a, i int) int {
|
||||
return a + i
|
||||
}
|
||||
|
||||
@@ -126,10 +126,25 @@ func genBase(dir, pkg string, api *spec.ApiSpec) error {
|
||||
}
|
||||
|
||||
func genApi(dir, pkg string, api *spec.ApiSpec) error {
|
||||
name := strcase.ToCamel(api.Info.Title + "Api")
|
||||
properties := api.Info.Properties
|
||||
if properties == nil {
|
||||
return fmt.Errorf("none properties")
|
||||
}
|
||||
|
||||
title := properties["Title"]
|
||||
if len(title) == 0 {
|
||||
return fmt.Errorf("none title")
|
||||
}
|
||||
|
||||
desc := properties["Desc"]
|
||||
if len(desc) == 0 {
|
||||
return fmt.Errorf("none desc")
|
||||
}
|
||||
|
||||
name := strcase.ToCamel(title + "Api")
|
||||
path := filepath.Join(dir, name+".kt")
|
||||
api.Info.Title = name
|
||||
api.Info.Desc = pkg
|
||||
api.Info.Desc = desc
|
||||
|
||||
e := os.MkdirAll(dir, 0755)
|
||||
if e != nil {
|
||||
|
||||
Reference in New Issue
Block a user