Skip to content

Commit

Permalink
feat: optimize getArrayPropertyName; support [][]string fixing #562 (#…
Browse files Browse the repository at this point in the history
…588)

* optimize getArrayPropertyName;
support [][]string

* []interface{} is deprecated but can be parsed by one's strong will
  • Loading branch information
sdghchj authored and easonlin404 committed Dec 25, 2019
1 parent 0e5dd7d commit 10630b9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
16 changes: 16 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,22 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
}
}
}
} else if structField.arrayType == "array" {
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok {
schema, _ := parser.parseTypeExpr(pkgName, "", astTypeArray.Elt)
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{structField.schemaType},
Description: structField.desc,
Items: &spec.SchemaOrArray{
Schema: schema,
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
ReadOnly: structField.readOnly,
},
}
}
} else {
// standard type in array
required := make([]string, 0)
Expand Down
10 changes: 10 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,7 @@ package api
type Response struct {
Code int
Table [][]string
Data []struct{
Field1 uint
Field2 string
Expand Down Expand Up @@ -2285,6 +2286,15 @@ func Test(){
}
}
}
},
"table": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
Expand Down
38 changes: 20 additions & 18 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func getPropertyName(expr ast.Expr, parser *Parser) (propertyName, error) {
}

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

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

func getArrayPropertyName(astTypeArray *ast.ArrayType, parser *Parser) propertyName {
if _, ok := astTypeArray.Elt.(*ast.StructType); ok {
func getArrayPropertyName(astTypeArrayElt ast.Expr, parser *Parser) propertyName {
switch elt := astTypeArrayElt.(type) {
case *ast.StructType, *ast.MapType, *ast.InterfaceType:
return propertyName{SchemaType: "array", ArrayType: "object"}
} else if _, ok := astTypeArray.Elt.(*ast.MapType); ok {
return propertyName{SchemaType: "array", ArrayType: "object"}
} else if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArrayExpr, parser, newArrayProperty)
} else if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArraySel, ok := astTypeArrayExpr.X.(*ast.SelectorExpr); ok {
return parseFieldSelectorExpr(astTypeArraySel, parser, newArrayProperty)
case *ast.ArrayType:
return propertyName{SchemaType: "array", ArrayType: "array"}
case *ast.StarExpr:
return getArrayPropertyName(elt.X, parser)
case *ast.SelectorExpr:
return parseFieldSelectorExpr(elt, parser, newArrayProperty)
case *ast.Ident:
name := TransToValidSchemeType(elt.Name)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[name]; isCustomType {
name = actualPrimitiveType
}
if astTypeArrayIdent, ok := astTypeArrayExpr.X.(*ast.Ident); ok {
name := TransToValidSchemeType(astTypeArrayIdent.Name)
return propertyName{SchemaType: "array", ArrayType: name}
return propertyName{SchemaType: "array", ArrayType: name}
default:
name := TransToValidSchemeType(fmt.Sprintf("%s", astTypeArrayElt))
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[name]; isCustomType {
name = actualPrimitiveType
}
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}
}

0 comments on commit 10630b9

Please sign in to comment.