From dda444c84868b400623a26ec460147c410f522e3 Mon Sep 17 00:00:00 2001 From: pei0804 Date: Wed, 9 May 2018 08:16:13 +0900 Subject: [PATCH] Add support Decimal and ptr slice and ptr struct array. --- parser_test.go | 15 +++++++++++++++ property.go | 28 +++++++++++++++++++++++----- testdata/simple/web/handler.go | 22 +++++++++++++--------- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/parser_test.go b/parser_test.go index a5b4e75c0..abd7efaf6 100644 --- a/parser_test.go +++ b/parser_test.go @@ -433,6 +433,9 @@ func TestParseSimpleApi(t *testing.T) { "data": { "type": "object" }, + "decimal": { + "type": "number" + }, "id": { "type": "integer", "format": "int64", @@ -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": { diff --git a/property.go b/property.go index e35816ded..96c9b09e5 100644 --- a/property.go +++ b/property.go @@ -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"} @@ -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"} } @@ -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"} } diff --git a/testdata/simple/web/handler.go b/testdata/simple/web/handler.go index 0d253ed9b..d1cd54f5e 100644 --- a/testdata/simple/web/handler.go +++ b/testdata/simple/web/handler.go @@ -4,6 +4,7 @@ import ( "time" "github.com/satori/go.uuid" + "github.com/shopspring/decimal" ) type Pet struct { @@ -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 {