Skip to content

Commit

Permalink
refactor for property.go (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
pei0804 authored and easonlin404 committed Mar 24, 2019
1 parent 3335bb4 commit 3320652
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 176 deletions.
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) (map
}

func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
prop := getPropertyName(field, parser)
prop := getPropertyName(field.Type, parser)
if len(prop.ArrayType) == 0 {
CheckSchemaType(prop.SchemaType)
} else {
Expand Down
90 changes: 38 additions & 52 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,77 +66,63 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr, parser *Parse

// getPropertyName returns the string value for the given field if it exists, otherwise it panics.
// allowedValues: array, boolean, integer, null, number, object, string
func getPropertyName(field *ast.Field, parser *Parser) propertyName {
if astTypeSelectorExpr, ok := field.Type.(*ast.SelectorExpr); ok {
func getPropertyName(expr ast.Expr, parser *Parser) propertyName {
if astTypeSelectorExpr, ok := expr.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeSelectorExpr, parser, newProperty)
}

// check if it is a custom type
typeName := fmt.Sprintf("%v", field.Type)
typeName := fmt.Sprintf("%v", expr)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[typeName]; isCustomType {
return propertyName{SchemaType: actualPrimitiveType, ArrayType: actualPrimitiveType}
}

if astTypeIdent, ok := field.Type.(*ast.Ident); ok {
if astTypeIdent, ok := expr.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
}
if ptr, ok := field.Type.(*ast.StarExpr); ok {
if astTypeSelectorExpr, ok := ptr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeSelectorExpr, parser, newProperty)
}
// TODO support custom pointer type?
if _, ok := ptr.X.(*ast.MapType); ok { // if map
//TODO support map
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := ptr.X.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if astTypeIdent, ok := ptr.X.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
}
if astTypeArray, ok := ptr.X.(*ast.ArrayType); ok { // if array
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
}
if astTypeArrayIdent, ok := astTypeArray.Elt.(*ast.Ident); ok {
name := TransToValidSchemeType(astTypeArrayIdent.Name)
return propertyName{SchemaType: "array", ArrayType: name}
}
}

if ptr, ok := expr.(*ast.StarExpr); ok {
return getPropertyName(ptr.X, parser)
}
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
}
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
}
if astTypeArrayIdent, ok := astTypeArrayExpr.X.(*ast.Ident); ok {
name := TransToValidSchemeType(astTypeArrayIdent.Name)
return propertyName{SchemaType: "array", ArrayType: name}
}
}
itemTypeName := TransToValidSchemeType(fmt.Sprintf("%s", astTypeArray.Elt))
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[itemTypeName]; isCustomType {
itemTypeName = actualPrimitiveType
}
return propertyName{SchemaType: "array", ArrayType: itemTypeName}

if astTypeArray, ok := expr.(*ast.ArrayType); ok { // if array
return getArrayPropertyName(astTypeArray, parser)
}
if _, ok := field.Type.(*ast.MapType); ok { // if map

if _, ok := expr.(*ast.MapType); ok { // if map
//TODO: support map
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := field.Type.(*ast.StructType); ok { // if struct

if _, ok := expr.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := field.Type.(*ast.InterfaceType); ok { // if interface{}

if _, ok := expr.(*ast.InterfaceType); ok { // if interface{}
return propertyName{SchemaType: "object", ArrayType: "object"}
}
panic("not supported" + fmt.Sprint(field.Type))

panic("not supported" + fmt.Sprint(expr))
}

func getArrayPropertyName(astTypeArray *ast.ArrayType, parser *Parser) propertyName {
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
}
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
}
if astTypeArrayIdent, ok := astTypeArrayExpr.X.(*ast.Ident); ok {
name := TransToValidSchemeType(astTypeArrayIdent.Name)
return propertyName{SchemaType: "array", ArrayType: name}
}
}
itemTypeName := TransToValidSchemeType(fmt.Sprintf("%s", astTypeArray.Elt))
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[itemTypeName]; isCustomType {
itemTypeName = actualPrimitiveType
}
return propertyName{SchemaType: "array", ArrayType: itemTypeName}
}
Loading

0 comments on commit 3320652

Please sign in to comment.