Skip to content

Commit

Permalink
chore: refactor goctl api (#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Oct 7, 2023
1 parent 02c9510 commit ff230c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions tools/goctl/api/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 15 additions & 5 deletions tools/goctl/pkg/parser/api/parser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
})
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ff230c4

Please sign in to comment.