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 { func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
name := "" var name string
tag := "" var tag string
if !field.IsAnonymous { if !field.IsAnonymous {
name = field.Name.Text() name = field.Name.Text()
if field.Tag != nil { if field.Tag != nil {
tag = field.Tag.Text() tag = field.Tag.Text()
} }
} }
return spec.Member{ return spec.Member{
Name: name, Name: name,
Type: p.astTypeToSpec(field.DataType), Type: p.astTypeToSpec(field.DataType),

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

Loading…
Cancel
Save