Skip to content

Commit

Permalink
Issue #225
Browse files Browse the repository at this point in the history
  • Loading branch information
ring-c committed Feb 19, 2020
1 parent f150c13 commit ec7b993
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ func (parser *Parser) ParseType(astFile *ast.File) {
typeName := fmt.Sprintf("%v", typeSpec.Type)
// check if its a custom primitive type
if IsGolangPrimitiveType(typeName) {
parser.CustomPrimitiveTypes[typeSpec.Name.String()] = TransToValidSchemeType(typeName)
var typeSpecFullName = fmt.Sprintf("%s.%s", astFile.Name.String(), typeSpec.Name.String())
parser.CustomPrimitiveTypes[typeSpecFullName] = TransToValidSchemeType(typeName)
} else {
parser.TypeDefinitions[astFile.Name.String()][typeSpec.Name.String()] = typeSpec
}
Expand Down Expand Up @@ -860,7 +861,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
return properties, nil, nil
}

structField, err := parser.parseField(field)
structField, err := parser.parseField(pkgName, field)
if err != nil {
return properties, nil, err
}
Expand Down Expand Up @@ -1117,8 +1118,8 @@ func getFieldType(field interface{}) (string, error) {
return "", fmt.Errorf("unknown field type %#v", field)
}

func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
prop, err := getPropertyName(field.Type, parser)
func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField, error) {
prop, err := getPropertyName(pkgName, field.Type, parser)
if err != nil {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr, parser *Parse
}
}
}
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[astTypeSelectorExpr.Sel.Name]; isCustomType {
name := fmt.Sprintf("%s.%v", pkgName, astTypeSelectorExpr.Sel.Name)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[name]; isCustomType {
return propertyName{SchemaType: actualPrimitiveType, ArrayType: actualPrimitiveType}
}
}
Expand All @@ -82,13 +83,13 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr, parser *Parse

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

// check if it is a custom type
typeName := fmt.Sprintf("%v", expr)
typeName := fmt.Sprintf("%s.%v", pkgName, expr)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[typeName]; isCustomType {
return propertyName{SchemaType: actualPrimitiveType, ArrayType: actualPrimitiveType}, nil
}
Expand All @@ -100,11 +101,11 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
}

if ptr, ok := expr.(*ast.StarExpr); ok {
return getPropertyName(ptr.X, parser)
return getPropertyName(pkgName, ptr.X, parser)
}

if astTypeArray, ok := expr.(*ast.ArrayType); ok { // if array
return getArrayPropertyName(astTypeArray.Elt, parser), nil
return getArrayPropertyName(pkgName, astTypeArray.Elt, parser), nil
}

if _, ok := expr.(*ast.MapType); ok { // if map
Expand All @@ -126,24 +127,25 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
return propertyName{}, errors.New("not supported" + fmt.Sprint(expr))
}

func getArrayPropertyName(astTypeArrayElt ast.Expr, parser *Parser) propertyName {
func getArrayPropertyName(pkgName string, astTypeArrayElt ast.Expr, parser *Parser) propertyName {
switch elt := astTypeArrayElt.(type) {
case *ast.StructType, *ast.MapType, *ast.InterfaceType:
return propertyName{SchemaType: "array", ArrayType: "object"}
case *ast.ArrayType:
return propertyName{SchemaType: "array", ArrayType: "array"}
case *ast.StarExpr:
return getArrayPropertyName(elt.X, parser)
return getArrayPropertyName(pkgName, elt.X, parser)
case *ast.SelectorExpr:
return parseFieldSelectorExpr(elt, parser, newArrayProperty)
case *ast.Ident:
name := TransToValidSchemeType(elt.Name)
name := fmt.Sprintf("%s.%s", pkgName, TransToValidSchemeType(elt.Name))
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[name]; isCustomType {
name = actualPrimitiveType
}
return propertyName{SchemaType: "array", ArrayType: name}
default:
name := TransToValidSchemeType(fmt.Sprintf("%s", astTypeArrayElt))
name = fmt.Sprintf("%s.%s", pkgName, name)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[name]; isCustomType {
name = actualPrimitiveType
}
Expand Down

0 comments on commit ec7b993

Please sign in to comment.