Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support Decimal and ptr slice #113

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ func TestParseSimpleApi(t *testing.T) {
"data": {
"type": "object"
},
"decimal": {
"type": "number"
},
"id": {
"type": "integer",
"format": "int64",
Expand All @@ -446,6 +449,18 @@ func TestParseSimpleApi(t *testing.T) {
"type": "string",
"example": "poti"
},
"pets": {
"type": "array",
"items": {
"$ref": "#/definitions/web.Pet2"
}
},
"pets2": {
"type": "array",
"items": {
"$ref": "#/definitions/web.Pet2"
}
},
"photo_urls": {
"type": "array",
"items": {
Expand Down
28 changes: 23 additions & 5 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type propertyName struct {
}

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"}
Expand All @@ -22,11 +23,16 @@ func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr) propertyName
return propertyName{SchemaType: "string", ArrayType: "string"}
}

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

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

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"}
}
Expand All @@ -51,15 +57,27 @@ func getPropertyName(field *ast.Field) propertyName {
schemeType := TransToValidSchemeType(name)
return propertyName{SchemaType: schemeType, ArrayType: schemeType}
}
}
if _, ok := field.Type.(*ast.MapType); ok { // if map
//TODO: support map
return propertyName{SchemaType: "object", ArrayType: "object"}
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}
}
}
}
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}
}
}
str := fmt.Sprintf("%s", astTypeArray.Elt)
return propertyName{SchemaType: "array", ArrayType: str}
}
if _, ok := field.Type.(*ast.MapType); ok { // if map
//TODO: support map
return propertyName{SchemaType: "object", ArrayType: "object"}
}
if _, ok := field.Type.(*ast.StructType); ok { // if struct
return propertyName{SchemaType: "object", ArrayType: "object"}
}
Expand Down
22 changes: 13 additions & 9 deletions testdata/simple/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/satori/go.uuid"
"github.com/shopspring/decimal"
)

type Pet struct {
Expand All @@ -18,15 +19,18 @@ type Pet struct {
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
} `json:"small_category"`
} `json:"category"`
Name string `json:"name" example:"poti"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
Tags []Tag `json:"tags"`
Status string `json:"status"`
Price float32 `json:"price" example:"3.25"`
IsAlive bool `json:"is_alive" example:"true"`
Data interface{} `json:"data"`
Hidden string `json:"-"`
UUID uuid.UUID `json:"uuid"`
Name string `json:"name" example:"poti"`
PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg"`
Tags []Tag `json:"tags"`
Pets *[]Pet2 `json:"pets"`
Pets2 []*Pet2 `json:"pets2"`
Status string `json:"status"`
Price float32 `json:"price" example:"3.25"`
IsAlive bool `json:"is_alive" example:"true"`
Data interface{} `json:"data"`
Hidden string `json:"-"`
UUID uuid.UUID `json:"uuid"`
Decimal decimal.Decimal `json:"decimal"`
}

type Tag struct {
Expand Down