1
0
mirror of https://github.com/beego/bee.git synced 2024-11-21 23:50:54 +00:00

update parameter name

This commit is contained in:
Yu Huang 2021-05-23 10:19:48 -04:00
parent e0e24c8e7f
commit 7d3817ce1a
2 changed files with 15 additions and 15 deletions

View File

@ -87,7 +87,7 @@ func NewStructParser(filePath string, src interface{}, rootStruct string, format
return nil, err return nil, err
} }
cg := &StructParser{ sp := &StructParser{
FieldFormatter: formatter, FieldFormatter: formatter,
Info: info, Info: info,
} }
@ -109,27 +109,27 @@ func NewStructParser(filePath string, src interface{}, rootStruct string, format
return true return true
} }
cg.MainStruct = cg.ParseStruct(structName, s) sp.MainStruct = sp.ParseStruct(structName, s)
return false return false
}) })
if cg.MainStruct == nil { if sp.MainStruct == nil {
return nil, errors.New("non-exist root struct") return nil, errors.New("non-exist root struct")
} }
return cg, nil return sp, nil
} }
func (cg *StructParser) ToJSON() ([]byte, error) { func (sp *StructParser) ToJSON() ([]byte, error) {
value := cg.MainStruct.ToKV() value := sp.MainStruct.ToKV()
return json.MarshalIndent(value, "", " ") return json.MarshalIndent(value, "", " ")
} }
// ParseField parses struct field in nested way // ParseField parses struct field in nested way
func (cg *StructParser) ParseField(field *ast.Field) *StructField { func (sp *StructParser) ParseField(field *ast.Field) *StructField {
// ast.Print(nil, field) // ast.Print(nil, field)
fieldName := field.Names[0].Name fieldName := field.Names[0].Name
fieldType := cg.Info.TypeOf(field.Type) fieldType := sp.Info.TypeOf(field.Type)
fieldTag := "" fieldTag := ""
if field.Tag != nil { if field.Tag != nil {
@ -146,7 +146,7 @@ func (cg *StructParser) ParseField(field *ast.Field) *StructField {
var nestedStruct *StructNode var nestedStruct *StructNode
if s, isInlineStruct := field.Type.(*ast.StructType); isInlineStruct { if s, isInlineStruct := field.Type.(*ast.StructType); isInlineStruct {
nestedStruct = cg.ParseStruct("", s) nestedStruct = sp.ParseStruct("", s)
} }
if _, isNamedStructorBasic := field.Type.(*ast.Ident); isNamedStructorBasic && field.Type.(*ast.Ident).Obj != nil { if _, isNamedStructorBasic := field.Type.(*ast.Ident); isNamedStructorBasic && field.Type.(*ast.Ident).Obj != nil {
@ -159,7 +159,7 @@ func (cg *StructParser) ParseField(field *ast.Field) *StructField {
if !ok { if !ok {
return nil return nil
} }
nestedStruct = cg.ParseStruct(ts.Name.Name, s) nestedStruct = sp.ParseStruct(ts.Name.Name, s)
} }
// fieldType.(*types.Basic) // basic type // fieldType.(*types.Basic) // basic type
// *ast.ArrayType: // *ast.ArrayType:
@ -173,15 +173,15 @@ func (cg *StructParser) ParseField(field *ast.Field) *StructField {
Comment: fieldComment, Comment: fieldComment,
Doc: fieldDoc, Doc: fieldDoc,
NestedType: nestedStruct, NestedType: nestedStruct,
FormatFunc: cg.FieldFormatter.Format, FormatFunc: sp.FieldFormatter.Format,
} }
} }
// ParseStruct parses struct in nested way // ParseStruct parses struct in nested way
func (cg *StructParser) ParseStruct(structName string, s *ast.StructType) *StructNode { func (sp *StructParser) ParseStruct(structName string, s *ast.StructType) *StructNode {
fields := []*StructField{} fields := []*StructField{}
for _, field := range s.Fields.List { for _, field := range s.Fields.List {
parsedField := cg.ParseField(field) parsedField := sp.ParseField(field)
if parsedField != nil { if parsedField != nil {
fields = append(fields, parsedField) fields = append(fields, parsedField)
} }

View File

@ -38,12 +38,12 @@ type StructA struct {
` `
formatter := &sampleFormatter{} formatter := &sampleFormatter{}
cg, err := NewStructParser("src.go", src, "StructA", formatter) sp, err := NewStructParser("src.go", src, "StructA", formatter)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
b, err := cg.ToJSON() b, err := sp.ToJSON()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }