chore: refactor goctl api (#3605)

master
Kevin Wan 1 year ago committed by GitHub
parent 02c95108b9
commit ff230c4b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -174,14 +174,15 @@ func (p parser) findDefinedType(name string) (*spec.Type, error) {
}
func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
name := ""
tag := ""
var name string
var tag string
if !field.IsAnonymous {
name = field.Name.Text()
if field.Tag != nil {
tag = field.Tag.Text()
}
}
return spec.Member{
Name: name,
Type: p.astTypeToSpec(field.DataType),

@ -19,13 +19,14 @@ type Analyzer struct {
func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
isLiteralType := func(dt ast.DataType) bool {
_, ok := dt.(*ast.BaseDataType)
if ok {
if _, ok := dt.(*ast.BaseDataType); ok {
return true
}
_, ok = dt.(*ast.AnyDataType)
_, ok := dt.(*ast.AnyDataType)
return ok
}
switch v := (in).(type) {
case *ast.BaseDataType:
raw := v.RawText()
@ -34,6 +35,7 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
RawName: raw,
}, nil
}
return spec.DefineStruct{RawName: raw}, nil
case *ast.AnyDataType:
return nil, ast.SyntaxError(v.Pos(), "unsupported any type")
@ -48,10 +50,12 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
if !v.Key.CanEqual() {
return nil, ast.SyntaxError(v.Pos(), "map key <%T> must be equal data type", v)
}
value, err := a.astTypeToSpec(v.Value)
if err != nil {
return nil, err
}
return spec.MapType{
RawName: v.RawText(),
Key: v.RawText(),
@ -67,6 +71,7 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
if err != nil {
return nil, err
}
return spec.PointerType{
RawName: v.RawText(),
Type: value,
@ -75,10 +80,12 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
if v.Length.Token.Type == token.ELLIPSIS {
return nil, ast.SyntaxError(v.Pos(), "Array: unsupported dynamic length")
}
value, err := a.astTypeToSpec(v.DataType)
if err != nil {
return nil, err
}
return spec.ArrayType{
RawName: v.RawText(),
Value: value,
@ -88,6 +95,7 @@ func (a *Analyzer) astTypeToSpec(in ast.DataType) (spec.Type, error) {
if err != nil {
return nil, err
}
return spec.ArrayType{
RawName: v.RawText(),
Value: value,
@ -105,6 +113,7 @@ func (a *Analyzer) convert2Spec() error {
if err := a.fillService(); err != nil {
return err
}
sort.SliceStable(a.spec.Types, func(i, j int) bool {
return a.spec.Types[i].Name() < a.spec.Types[j].Name()
})
@ -120,6 +129,7 @@ func (a *Analyzer) convert2Spec() error {
return groups[i].Annotation.Properties["group"] < groups[j].Annotation.Properties["group"]
})
a.spec.Service.Groups = groups
return nil
}
@ -165,6 +175,7 @@ func (a *Analyzer) fieldToMember(field *ast.ElemExpr) (spec.Member, error) {
if field.Tag != nil {
m.Tag = field.Tag.Token.Text
}
return m, nil
}
@ -261,8 +272,7 @@ func (a *Analyzer) fillTypes() error {
for _, item := range a.api.TypeStmt {
switch v := (item).(type) {
case *ast.TypeLiteralStmt:
err := a.fillTypeExpr(v.Expr)
if err != nil {
if err := a.fillTypeExpr(v.Expr); err != nil {
return err
}
case *ast.TypeGroupStmt:

Loading…
Cancel
Save