Skip to content

Commit

Permalink
refactor: refactoring property.go more simple (swaggo#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
pei0804 authored and easonlin404 committed May 29, 2018
1 parent dbf857a commit 657cedd
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,44 @@ type propertyName struct {
ArrayType string
}

func newSchemaPropertyName(schemaType string) propertyName {
return propertyName{
SchemaType: schemaType,
ArrayType: "",
}
}

func newArraySchemaPropertyName(arrayType string) propertyName {
return propertyName{
SchemaType: "array",
ArrayType: arrayType,
}
}

func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr) propertyName {
// TODO: In the future, add functions and make them solve for each user
// Support for time.Time as a structure field
if "Time" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "string", ArrayType: "string"}
return newSchemaPropertyName("string")
}

// Support bson.ObjectId type
if "ObjectId" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "string", ArrayType: "string"}
return newSchemaPropertyName("string")
}

// Supprt UUID
if "UUID" == strings.ToUpper(astTypeSelectorExpr.Sel.Name) {
return propertyName{SchemaType: "string", ArrayType: "string"}
return newSchemaPropertyName("string")
}

// Supprt shopspring/decimal
if "Decimal" == astTypeSelectorExpr.Sel.Name {
return propertyName{SchemaType: "number", ArrayType: "string"}
return newSchemaPropertyName("number")
}

fmt.Printf("%s is not supported. but it will be set with string temporary. Please report any problems.", astTypeSelectorExpr.Sel.Name)
return propertyName{SchemaType: "string", ArrayType: "string"}
return newSchemaPropertyName("string")
}

// getPropertyName returns the string value for the given field if it exists, otherwise it panics.
Expand All @@ -46,7 +60,7 @@ func getPropertyName(field *ast.Field) propertyName {
if astTypeIdent, ok := field.Type.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
return newSchemaPropertyName(schemeType)
}
if ptr, ok := field.Type.(*ast.StarExpr); ok {
if astTypeSelectorExpr, ok := ptr.X.(*ast.SelectorExpr); ok {
Expand All @@ -55,34 +69,34 @@ func getPropertyName(field *ast.Field) propertyName {
if astTypeIdent, ok := ptr.X.(*ast.Ident); ok {
name := astTypeIdent.Name
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
return newSchemaPropertyName(schemeType)
}
if astTypeArray, ok := ptr.X.(*ast.ArrayType); ok { // if array
if astTypeArrayIdent := astTypeArray.Elt.(*ast.Ident); ok {
name := astTypeArrayIdent.Name
return propertyName{SchemaType: "array", ArrayType: name}
arrayType := astTypeArrayIdent.Name
return newArraySchemaPropertyName(arrayType)
}
}
}
if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
if astTypeArrayIdent := astTypeArrayExpr.X.(*ast.Ident); ok {
name := astTypeArrayIdent.Name
return propertyName{SchemaType: "array", ArrayType: name}
arrayType := astTypeArrayIdent.Name
return newArraySchemaPropertyName(arrayType)
}
}
str := fmt.Sprintf("%s", astTypeArray.Elt)
return propertyName{SchemaType: "array", ArrayType: str}
arrayType := fmt.Sprintf("%s", astTypeArray.Elt)
return newArraySchemaPropertyName(arrayType)
}
if _, ok := field.Type.(*ast.MapType); ok { // if map
//TODO: support map
return propertyName{SchemaType: "object", ArrayType: "object"}
return newSchemaPropertyName("object")
}
if _, ok := field.Type.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
return newSchemaPropertyName("object")
}
if _, ok := field.Type.(*ast.InterfaceType); ok { // if interface{}
return propertyName{SchemaType: "object", ArrayType: "object"}
return newSchemaPropertyName("object")
}
panic("not supported" + fmt.Sprint(field.Type))
}

0 comments on commit 657cedd

Please sign in to comment.