Skip to content

Commit

Permalink
fix: primitive type of param in body; use const string for type (#734)
Browse files Browse the repository at this point in the history
* fix primitive type of param in body;
const type string;

* fix lint check

* fix lint check

Co-authored-by: Eason Lin <[email protected]>
  • Loading branch information
sdghchj and easonlin404 authored Jun 10, 2020
1 parent 1868a02 commit 4a18bc9
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 104 deletions.
40 changes: 21 additions & 19 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
refType := TransToValidSchemeType(matches[3])

// Detect refType
objectType := "object"
objectType := OBJECT
if strings.HasPrefix(refType, "[]") {
objectType = "array"
objectType = ARRAY
refType = strings.TrimPrefix(refType, "[]")
refType = TransToValidSchemeType(refType)
} else if IsPrimitiveType(refType) ||
paramType == "formData" && refType == "file" {
objectType = "primitive"
objectType = PRIMITIVE
}

requiredText := strings.ToLower(matches[4])
Expand All @@ -165,16 +165,16 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
switch paramType {
case "path", "header", "formData":
switch objectType {
case "array", "object":
case ARRAY, OBJECT:
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
case "query":
switch objectType {
case "array":
case ARRAY:
if !IsPrimitiveType(refType) {
return fmt.Errorf("%s is not supported array type for %s", refType, paramType)
}
param.SimpleSchema.Type = "array"
param.SimpleSchema.Type = objectType
if operation.parser != nil {
param.CollectionFormat = TransToValidCollectionFormat(operation.parser.collectionFormatInQuery)
}
Expand All @@ -183,7 +183,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
Type: refType,
},
}
case "object":
case OBJECT:
refType, typeSpec, err := operation.registerSchemaType(refType, astFile)
if err != nil {
return err
Expand Down Expand Up @@ -218,7 +218,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
if len(prop.Type) == 0 {
continue
}
if prop.Type[0] == "array" &&
if prop.Type[0] == ARRAY &&
prop.Items.Schema != nil &&
len(prop.Items.Schema.Type) > 0 &&
IsSimplePrimitiveType(prop.Items.Schema.Type[0]) {
Expand Down Expand Up @@ -408,7 +408,7 @@ func findAttr(re *regexp.Regexp, commentLine string) (string, error) {
}

func setStringParam(name, schemaType, attr, commentLine string) (int64, error) {
if schemaType != "string" {
if schemaType != STRING {
return 0, fmt.Errorf("%s is attribute to set to a number. comment=%s got=%s", name, commentLine, schemaType)
}
n, err := strconv.ParseInt(attr, 10, 64)
Expand All @@ -419,7 +419,7 @@ func setStringParam(name, schemaType, attr, commentLine string) (int64, error) {
}

func setNumberParam(name, schemaType, attr, commentLine string) (float64, error) {
if schemaType != "integer" && schemaType != "number" {
if schemaType != INTEGER && schemaType != NUMBER {
return 0, fmt.Errorf("%s is attribute to set to a number. comment=%s got=%s", name, commentLine, schemaType)
}
n, err := strconv.ParseFloat(attr, 64)
Expand All @@ -443,7 +443,7 @@ func setEnumParam(attr, schemaType string, param *spec.Parameter) error {
}

func setCollectionFormatParam(name, schemaType, attr, commentLine string) (string, error) {
if schemaType != "array" {
if schemaType != ARRAY {
return "", fmt.Errorf("%s is attribute to set to an array. comment=%s got=%s", name, commentLine, schemaType)
}
return TransToValidCollectionFormat(attr), nil
Expand All @@ -453,21 +453,21 @@ func setCollectionFormatParam(name, schemaType, attr, commentLine string) (strin
func defineType(schemaType string, value string) (interface{}, error) {
schemaType = TransToValidSchemeType(schemaType)
switch schemaType {
case "string":
case STRING:
return value, nil
case "number":
case NUMBER:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, fmt.Errorf("enum value %s can't convert to %s err: %s", value, schemaType, err)
}
return v, nil
case "integer":
case INTEGER:
v, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("enum value %s can't convert to %s err: %s", value, schemaType, err)
}
return v, nil
case "boolean":
case BOOLEAN:
v, err := strconv.ParseBool(value)
if err != nil {
return nil, fmt.Errorf("enum value %s can't convert to %s err: %s", value, schemaType, err)
Expand Down Expand Up @@ -622,7 +622,7 @@ var combinedPattern = regexp.MustCompile(`^([\w\-\.\/\[\]]+)\{(.*)\}$`)
func (operation *Operation) parseObjectSchema(refType string, astFile *ast.File) (*spec.Schema, error) {
switch {
case refType == "interface{}":
return PrimitiveSchema("object"), nil
return PrimitiveSchema(OBJECT), nil
case IsGolangPrimitiveType(refType):
refType = TransToValidSchemeType(refType)
return PrimitiveSchema(refType), nil
Expand Down Expand Up @@ -707,26 +707,28 @@ func (operation *Operation) parseCombinedObjectSchema(refType string, astFile *a
}
return spec.ComposedSchema(*schema, spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Type: []string{OBJECT},
Properties: props,
},
}), nil
}

func (operation *Operation) parseAPIObjectSchema(schemaType, refType string, astFile *ast.File) (*spec.Schema, error) {
switch schemaType {
case "object":
case OBJECT:
if !strings.HasPrefix(refType, "[]") {
return operation.parseObjectSchema(refType, astFile)
}
refType = refType[2:]
fallthrough
case "array":
case ARRAY:
schema, err := operation.parseObjectSchema(refType, astFile)
if err != nil {
return nil, err
}
return spec.ArrayProperty(schema), nil
case PRIMITIVE:
return PrimitiveSchema(refType), nil
default:
return PrimitiveSchema(schemaType), nil
}
Expand Down
52 changes: 26 additions & 26 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)
Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeExpr)
}

return PrimitiveSchema("object"), nil
return PrimitiveSchema(OBJECT), nil
}

func (parser *Parser) parseStruct(pkgName string, fields *ast.FieldList) (*spec.Schema, error) {
Expand Down Expand Up @@ -768,7 +768,7 @@ func (parser *Parser) parseStruct(pkgName string, fields *ast.FieldList) (*spec.

return &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Type: []string{OBJECT},
Properties: properties,
Required: required,
}}, nil
Expand Down Expand Up @@ -860,11 +860,11 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
}

switch schemaType {
case "object":
case OBJECT:
for k, v := range schema.SchemaProps.Properties {
properties[k] = v
}
case "array":
case ARRAY:
properties[typeName] = *schema
default:
Printf("Can't extract properties from a schema of type '%s'", schemaType)
Expand Down Expand Up @@ -921,7 +921,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
}
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"}, // to avoid swagger validation error
Type: []string{OBJECT}, // to avoid swagger validation error
Description: structField.desc,
Required: required,
Ref: spec.Ref{
Expand All @@ -932,7 +932,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
ReadOnly: structField.readOnly,
},
}
} else if structField.schemaType == "array" { // array field type
} else if structField.schemaType == ARRAY { // array field type
// if defined -- ref it
if typeSpec, ok := parser.TypeDefinitions[pkgName][structField.arrayType]; ok { // user type in array
parser.ParseDefinition(pkgName, structField.arrayType,
Expand All @@ -946,7 +946,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
schema.Required = required
schema.ReadOnly = structField.readOnly
properties[structField.name] = *schema
} else if structField.arrayType == "object" {
} else if structField.arrayType == OBJECT {
// Anonymous struct
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
props := make(map[string]spec.Schema)
Expand All @@ -968,7 +968,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Type: []string{OBJECT},
Properties: props,
},
},
Expand All @@ -986,7 +986,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
properties[structField.name] = *schema
}
}
} else if structField.arrayType == "array" {
} else if structField.arrayType == ARRAY {
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok {
schema, _ := parser.parseTypeExpr(pkgName, "", astTypeArray.Elt)
schema = spec.ArrayProperty(schema)
Expand Down Expand Up @@ -1065,7 +1065,7 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
return properties, nil, err
}
for k, v := range p {
if v.SchemaProps.Type[0] != "object" {
if v.SchemaProps.Type[0] != OBJECT {
nestRequired = append(nestRequired, v.SchemaProps.Required...)
v.SchemaProps.Required = make([]string, 0)
}
Expand Down Expand Up @@ -1115,13 +1115,13 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
return nil, err
}
} else {
if err := CheckSchemaType("array"); err != nil {
if err := CheckSchemaType(ARRAY); err != nil {
return nil, err
}
}

// Skip func fields.
if prop.SchemaType == "func" {
if prop.SchemaType == FUNC {
return &structField{name: ""}, nil
}

Expand Down Expand Up @@ -1195,12 +1195,12 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
newSchemaType := parts[0]
newArrayType := structField.arrayType
if len(parts) >= 2 {
if newSchemaType == "array" {
if newSchemaType == ARRAY {
newArrayType = parts[1]
if err := CheckSchemaType(newArrayType); err != nil {
return nil, err
}
} else if newSchemaType == "primitive" {
} else if newSchemaType == PRIMITIVE {
newSchemaType = parts[1]
newArrayType = parts[1]
}
Expand Down Expand Up @@ -1258,7 +1258,7 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
}
if enumsTag := structTag.Get("enums"); enumsTag != "" {
enumType := structField.schemaType
if structField.schemaType == "array" {
if structField.schemaType == ARRAY {
enumType = structField.arrayType
}

Expand Down Expand Up @@ -1291,7 +1291,7 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
}
structField.minimum = minimum
}
if structField.schemaType == "string" || structField.arrayType == "string" {
if structField.schemaType == STRING || structField.arrayType == STRING {
maxLength, err := getIntTag(structTag, "maxLength")
if err != nil {
return nil, err
Expand All @@ -1314,14 +1314,14 @@ func (parser *Parser) parseField(pkgName string, field *ast.Field) (*structField
// @encoding/json: "It applies only to fields of string, floating point, integer, or boolean types."
defaultValues := map[string]string{
// Zero Values as string
"string": "",
"integer": "0",
"boolean": "false",
"number": "0",
STRING: "",
INTEGER: "0",
BOOLEAN: "false",
NUMBER: "0",
}

if defaultValue, ok := defaultValues[structField.schemaType]; ok {
structField.schemaType = "string"
structField.schemaType = STRING

if structField.exampleValue == nil {
// if exampleValue is not defined by the user,
Expand Down Expand Up @@ -1403,27 +1403,27 @@ func toLowerCamelCase(in string) string {
// defineTypeOfExample example value define the type (object and array unsupported)
func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{}, error) {
switch schemaType {
case "string":
case STRING:
return exampleValue, nil
case "number":
case NUMBER:
v, err := strconv.ParseFloat(exampleValue, 64)
if err != nil {
return nil, fmt.Errorf("example value %s can't convert to %s err: %s", exampleValue, schemaType, err)
}
return v, nil
case "integer":
case INTEGER:
v, err := strconv.Atoi(exampleValue)
if err != nil {
return nil, fmt.Errorf("example value %s can't convert to %s err: %s", exampleValue, schemaType, err)
}
return v, nil
case "boolean":
case BOOLEAN:
v, err := strconv.ParseBool(exampleValue)
if err != nil {
return nil, fmt.Errorf("example value %s can't convert to %s err: %s", exampleValue, schemaType, err)
}
return v, nil
case "array":
case ARRAY:
values := strings.Split(exampleValue, ",")
result := make([]interface{}, 0)
for _, value := range values {
Expand Down
18 changes: 9 additions & 9 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type propertyNewFunc func(schemeType string, crossPkg string) propertyName

func newArrayProperty(schemeType string, crossPkg string) propertyName {
return propertyName{
SchemaType: "array",
SchemaType: ARRAY,
ArrayType: schemeType,
CrossPkg: crossPkg,
}
Expand All @@ -38,9 +38,9 @@ func convertFromSpecificToPrimitive(typeName string) (string, error) {
typeName = strings.ToUpper(typeName)
switch typeName {
case "TIME", "OBJECTID", "UUID":
return "string", nil
return STRING, nil
case "DECIMAL":
return "number", nil
return NUMBER, nil
}
return "", ErrFailedConvertPrimitiveType
}
Expand Down Expand Up @@ -92,9 +92,9 @@ func getPropertyName(pkgName string, expr ast.Expr, parser *Parser) (propertyNam
case *ast.ArrayType:
return getArrayPropertyName(pkgName, tp.Elt, parser), nil
case *ast.MapType, *ast.StructType, *ast.InterfaceType:
return propertyName{SchemaType: "object", ArrayType: "object"}, nil
return propertyName{SchemaType: OBJECT, ArrayType: OBJECT}, nil
case *ast.FuncType:
return propertyName{SchemaType: "func", ArrayType: ""}, nil
return propertyName{SchemaType: FUNC, ArrayType: ""}, nil
case *ast.Ident:
name := tp.Name
// check if it is a custom type
Expand All @@ -112,9 +112,9 @@ func getPropertyName(pkgName string, expr ast.Expr, parser *Parser) (propertyNam
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"}
return propertyName{SchemaType: ARRAY, ArrayType: OBJECT}
case *ast.ArrayType:
return propertyName{SchemaType: "array", ArrayType: "array"}
return propertyName{SchemaType: ARRAY, ArrayType: ARRAY}
case *ast.StarExpr:
return getArrayPropertyName(pkgName, elt.X, parser)
case *ast.SelectorExpr:
Expand All @@ -126,14 +126,14 @@ func getArrayPropertyName(pkgName string, astTypeArrayElt ast.Expr, parser *Pars
} else {
name = TransToValidSchemeType(elt.Name)
}
return propertyName{SchemaType: "array", ArrayType: name}
return propertyName{SchemaType: ARRAY, ArrayType: name}
default:
name := fmt.Sprintf("%s", astTypeArrayElt)
if actualPrimitiveType, isCustomType := parser.CustomPrimitiveTypes[fullTypeName(pkgName, name)]; isCustomType {
name = actualPrimitiveType
} else {
name = TransToValidSchemeType(name)
}
return propertyName{SchemaType: "array", ArrayType: name}
return propertyName{SchemaType: ARRAY, ArrayType: name}
}
}
Loading

0 comments on commit 4a18bc9

Please sign in to comment.