From 02f7b79a2d223c1d4d9bd6f2582dfc760b1509a5 Mon Sep 17 00:00:00 2001 From: Arrim Date: Thu, 23 Apr 2020 00:19:50 +0300 Subject: [PATCH 1/5] issue-503: added type map in example value --- parser.go | 19 ++++++++++++++++++- schema.go | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/parser.go b/parser.go index 5d86ab319..c50871b79 100644 --- a/parser.go +++ b/parser.go @@ -1235,7 +1235,7 @@ 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" || newSchemaType == "map" { newArrayType = parts[1] if err := CheckSchemaType(newArrayType); err != nil { return nil, err @@ -1474,6 +1474,23 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{ result = append(result, v) } return result, nil + case "map": + values := strings.Split(exampleValue, ",") + result := map[string]interface{}{} + for _, value := range values { + mapData := strings.Split(value, ":") + + if len(mapData) == 2 { + v, err := defineTypeOfExample(arrayType, "", mapData[1]) + if err != nil { + return nil, err + } + result[mapData[0]] = v + } else { + return nil, fmt.Errorf("example value %s should format: key:value", exampleValue) + } + } + return result, nil default: return nil, fmt.Errorf("%s is unsupported type in example value", schemaType) } diff --git a/schema.go b/schema.go index 539340fae..c1866f269 100644 --- a/schema.go +++ b/schema.go @@ -27,7 +27,7 @@ func IsSimplePrimitiveType(typeName string) bool { // IsPrimitiveType determine whether the type name is a primitive type func IsPrimitiveType(typeName string) bool { switch typeName { - case "string", "number", "integer", "boolean", "array", "object", "func": + case "string", "number", "integer", "boolean", "array", "object", "map", "func": return true default: return false From c1e944a7eda4c72c260c55aa8260bd5f8ca6185c Mon Sep 17 00:00:00 2001 From: Arrim Date: Sat, 17 Oct 2020 17:25:14 +0300 Subject: [PATCH 2/5] Refactor and update example tag for map(object) --- example/object-map-example/controller/api.go | 25 ++++ .../controller/controller.go | 10 ++ .../object-map-example/controller/response.go | 11 ++ example/object-map-example/docs/docs.go | 141 ++++++++++++++++++ example/object-map-example/docs/swagger.json | 78 ++++++++++ example/object-map-example/docs/swagger.yaml | 53 +++++++ example/object-map-example/go.mod | 11 ++ example/object-map-example/main.go | 35 +++++ parser.go | 8 +- schema.go | 13 +- 10 files changed, 380 insertions(+), 5 deletions(-) create mode 100644 example/object-map-example/controller/api.go create mode 100644 example/object-map-example/controller/controller.go create mode 100644 example/object-map-example/controller/response.go create mode 100644 example/object-map-example/docs/docs.go create mode 100644 example/object-map-example/docs/swagger.json create mode 100644 example/object-map-example/docs/swagger.yaml create mode 100644 example/object-map-example/go.mod create mode 100644 example/object-map-example/main.go diff --git a/example/object-map-example/controller/api.go b/example/object-map-example/controller/api.go new file mode 100644 index 000000000..83b5e99fe --- /dev/null +++ b/example/object-map-example/controller/api.go @@ -0,0 +1,25 @@ +package controller + +import "github.com/gin-gonic/gin" + +// GetMap godoc +// @Summary Get Map Example +// @Description get map +// @ID get-map +// @Accept json +// @Produce json +// @Success 200 {object} Response +// @Router /test [get] +func (c *Controller) GetMap(ctx *gin.Context) { + ctx.JSON(200, Response{ + Title: map[string]string{ + "en": "Map", + }, + CustomType: map[string]interface{}{ + "key": "value", + }, + Object: Data{ + Text: "object text", + }, + }) +} diff --git a/example/object-map-example/controller/controller.go b/example/object-map-example/controller/controller.go new file mode 100644 index 000000000..735d745de --- /dev/null +++ b/example/object-map-example/controller/controller.go @@ -0,0 +1,10 @@ +package controller + +// Controller example +type Controller struct { +} + +// NewController example +func NewController() *Controller { + return &Controller{} +} diff --git a/example/object-map-example/controller/response.go b/example/object-map-example/controller/response.go new file mode 100644 index 000000000..d0692f08c --- /dev/null +++ b/example/object-map-example/controller/response.go @@ -0,0 +1,11 @@ +package controller + +type Response struct { + Title map[string]string `json:"title" example:"en:Map,ru:Карта,kk:Карталар"` + CustomType map[string]interface{} `json:"map_data" swaggertype:"object,string" example:"key:value,key2:value2"` + Object Data `json:"object"` +} + +type Data struct { + Text string `json:"title" example:"Object data"` +} diff --git a/example/object-map-example/docs/docs.go b/example/object-map-example/docs/docs.go new file mode 100644 index 000000000..f73e708a4 --- /dev/null +++ b/example/object-map-example/docs/docs.go @@ -0,0 +1,141 @@ +// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT +// This file was generated by swaggo/swag + +package docs + +import ( + "bytes" + "encoding/json" + "strings" + + "github.com/alecthomas/template" + "github.com/swaggo/swag" +) + +var doc = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{.Description}}", + "title": "{{.Title}}", + "termsOfService": "http://swagger.io/terms/", + "contact": {}, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/test": { + "get": { + "description": "get map", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "summary": "Get Map Example", + "operationId": "get-map", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + } + }, + "definitions": { + "controller.Data": { + "type": "object", + "properties": { + "title": { + "type": "string", + "example": "Object data" + } + } + }, + "controller.Response": { + "type": "object", + "properties": { + "map_data": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "example": { + "key": "value", + "key2": "value2" + } + }, + "object": { + "$ref": "#/definitions/controller.Data" + }, + "title": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "example": { + "en": "Map", + "kk": "Карталар", + "ru": "Карта" + } + } + } + } + } +}` + +type swaggerInfo struct { + Version string + Host string + BasePath string + Schemes []string + Title string + Description string +} + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = swaggerInfo{ + Version: "1.0", + Host: "localhost:8080", + BasePath: "/api/v1", + Schemes: []string{}, + Title: "Swagger Map Example API", + Description: "", +} + +type s struct{} + +func (s *s) ReadDoc() string { + sInfo := SwaggerInfo + sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1) + + t, err := template.New("swagger_info").Funcs(template.FuncMap{ + "marshal": func(v interface{}) string { + a, _ := json.Marshal(v) + return string(a) + }, + }).Parse(doc) + if err != nil { + return doc + } + + var tpl bytes.Buffer + if err := t.Execute(&tpl, sInfo); err != nil { + return doc + } + + return tpl.String() +} + +func init() { + swag.Register(swag.Name, &s{}) +} diff --git a/example/object-map-example/docs/swagger.json b/example/object-map-example/docs/swagger.json new file mode 100644 index 000000000..e20754c9d --- /dev/null +++ b/example/object-map-example/docs/swagger.json @@ -0,0 +1,78 @@ +{ + "swagger": "2.0", + "info": { + "title": "Swagger Map Example API", + "termsOfService": "http://swagger.io/terms/", + "contact": {}, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/v1", + "paths": { + "/test": { + "get": { + "description": "get map", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "summary": "Get Map Example", + "operationId": "get-map", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + } + }, + "definitions": { + "controller.Data": { + "type": "object", + "properties": { + "title": { + "type": "string", + "example": "Object data" + } + } + }, + "controller.Response": { + "type": "object", + "properties": { + "map_data": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "example": { + "key": "value", + "key2": "value2" + } + }, + "object": { + "$ref": "#/definitions/controller.Data" + }, + "title": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "example": { + "en": "Map", + "kk": "Карталар", + "ru": "Карта" + } + } + } + } + } +} \ No newline at end of file diff --git a/example/object-map-example/docs/swagger.yaml b/example/object-map-example/docs/swagger.yaml new file mode 100644 index 000000000..d7a65e140 --- /dev/null +++ b/example/object-map-example/docs/swagger.yaml @@ -0,0 +1,53 @@ +basePath: /api/v1 +definitions: + controller.Data: + properties: + title: + example: Object data + type: string + type: object + controller.Response: + properties: + map_data: + additionalProperties: + type: string + example: + key: value + key2: value2 + type: object + object: + $ref: '#/definitions/controller.Data' + title: + additionalProperties: + type: string + example: + en: Map + kk: Карталар + ru: Карта + type: object + type: object +host: localhost:8080 +info: + contact: {} + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + termsOfService: http://swagger.io/terms/ + title: Swagger Map Example API + version: "1.0" +paths: + /test: + get: + consumes: + - application/json + description: get map + operationId: get-map + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controller.Response' + summary: Get Map Example +swagger: "2.0" diff --git a/example/object-map-example/go.mod b/example/object-map-example/go.mod new file mode 100644 index 000000000..a544f4fe5 --- /dev/null +++ b/example/object-map-example/go.mod @@ -0,0 +1,11 @@ +module github.com/swaggo/swag/example/object-map-example + +go 1.14 + +require ( + github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 + github.com/gin-gonic/gin v1.6.3 + github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 + github.com/swaggo/gin-swagger v1.2.0 + github.com/swaggo/swag v1.5.1 +) diff --git a/example/object-map-example/main.go b/example/object-map-example/main.go new file mode 100644 index 000000000..0108f2c9f --- /dev/null +++ b/example/object-map-example/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "github.com/swaggo/swag/example/object-map-example/controller" + _ "github.com/swaggo/swag/example/object-map-example/docs" + + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" +) + +// @title Swagger Map Example API +// @version 1.0 +// @termsOfService http://swagger.io/terms/ + +// @license.name Apache 2.0 +// @license.url http://www.apache.org/licenses/LICENSE-2.0.html + +// @host localhost:8080 +// @BasePath /api/v1 +func main() { + r := gin.Default() + + c := controller.NewController() + + v1 := r.Group("/api/v1") + { + test := v1.Group("/map") + { + test.GET("", c.GetMap) + } + } + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + r.Run(":8080") +} diff --git a/parser.go b/parser.go index dca3c8640..d853e1d6c 100644 --- a/parser.go +++ b/parser.go @@ -992,7 +992,7 @@ func (parser *Parser) parseFieldTag(field *ast.Field, types []string) (*structFi // name: field.Names[0].Name, schemaType: types[0], } - if len(types) > 1 && types[0] == "array" { + if len(types) > 1 && (types[0] == "array" || types[0] == "object") { structField.arrayType = types[1] } @@ -1263,7 +1263,11 @@ func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{ result = append(result, v) } return result, nil - case "map": + case OBJECT: + if arrayType == "" { + return nil, fmt.Errorf("%s is unsupported type in example value", schemaType) + } + values := strings.Split(exampleValue, ",") result := map[string]interface{}{} for _, value := range values { diff --git a/schema.go b/schema.go index 7c4f0da97..b9a0bc084 100644 --- a/schema.go +++ b/schema.go @@ -12,8 +12,6 @@ import ( const ( //ARRAY array ARRAY = "array" - //MAP map - MAP = "map" //OBJECT object OBJECT = "object" //PRIMITIVE primitive @@ -51,7 +49,7 @@ func IsSimplePrimitiveType(typeName string) bool { // IsPrimitiveType determine whether the type name is a primitive type func IsPrimitiveType(typeName string) bool { switch typeName { - case STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, MAP, OBJECT, FUNC: + case STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, OBJECT, FUNC: return true default: return false @@ -171,6 +169,15 @@ func BuildCustomSchema(types []string) (*spec.Schema, error) { return nil, err } return spec.ArrayProperty(schema), nil + case "object": + if len(types) == 1 { + return nil, errors.New("need object item type after object") + } + schema, err := BuildCustomSchema(types[1:]) + if err != nil { + return nil, err + } + return spec.MapProperty(schema), nil default: err := CheckSchemaType(types[0]) if err != nil { From 4fab88b4462fd4695e13a938a1982bff8f4447ac Mon Sep 17 00:00:00 2001 From: Arrim Date: Sat, 17 Oct 2020 17:56:44 +0300 Subject: [PATCH 3/5] Add data in test --- testdata/simple/expected.json | 10 ++++++++++ testdata/simple/web/handler.go | 29 +++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index 19cb8e01f..6b4ec5e49 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -567,6 +567,16 @@ "ill" ] }, + "string_map": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "example": { + "key1": "value", + "key2": "value2" + } + }, "tags": { "type": "array", "items": { diff --git a/testdata/simple/web/handler.go b/testdata/simple/web/handler.go index 457ec30e0..6c4d39e43 100644 --- a/testdata/simple/web/handler.go +++ b/testdata/simple/web/handler.go @@ -20,20 +20,21 @@ 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" binding:"required"` - PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" binding:"required"` - Tags []Tag `json:"tags"` - Pets *[]Pet2 `json:"pets"` - Pets2 []*Pet2 `json:"pets2"` - Status string `json:"status" enums:"healthy,ill"` - Price float32 `json:"price" example:"3.25" minimum:"1.0" maximum:"1000"` - IsAlive bool `json:"is_alive" example:"true" default:"true"` - Data interface{} `json:"data"` - Hidden string `json:"-"` - UUID uuid.UUID `json:"uuid"` - Decimal decimal.Decimal `json:"decimal"` - IntArray []int `json:"int_array" example:"1,2"` - EnumArray []int `json:"enum_array" enums:"1,2,3,5,7"` + Name string `json:"name" example:"poti" binding:"required"` + PhotoUrls []string `json:"photo_urls" example:"http://test/image/1.jpg,http://test/image/2.jpg" binding:"required"` + Tags []Tag `json:"tags"` + Pets *[]Pet2 `json:"pets"` + Pets2 []*Pet2 `json:"pets2"` + Status string `json:"status" enums:"healthy,ill"` + Price float32 `json:"price" example:"3.25" minimum:"1.0" maximum:"1000"` + IsAlive bool `json:"is_alive" example:"true" default:"true"` + Data interface{} `json:"data"` + Hidden string `json:"-"` + UUID uuid.UUID `json:"uuid"` + Decimal decimal.Decimal `json:"decimal"` + IntArray []int `json:"int_array" example:"1,2"` + StringMap map[string]string `json:"string_map" example:"key1:value,key2:value2"` + EnumArray []int `json:"enum_array" enums:"1,2,3,5,7"` } type Tag struct { From 998c9f3b774c309d152090febef4219368dc7b66 Mon Sep 17 00:00:00 2001 From: Arrim Date: Mon, 26 Oct 2020 12:05:38 +0300 Subject: [PATCH 4/5] Add test --- go.mod | 1 + go.sum | 2 ++ schema_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/go.mod b/go.mod index 7007b4061..0bf8092ee 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gin-gonic/gin v1.6.3 github.com/go-openapi/spec v0.19.9 github.com/gofrs/uuid v3.3.0+incompatible + github.com/shopspring/decimal v1.2.0 github.com/stretchr/testify v1.6.1 github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 github.com/swaggo/gin-swagger v1.2.0 diff --git a/go.sum b/go.sum index 8ce1d4877..23959a317 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= +github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/schema_test.go b/schema_test.go index af458bb38..6f1bbcb40 100644 --- a/schema_test.go +++ b/schema_test.go @@ -3,6 +3,7 @@ package swag import ( "testing" + "github.com/go-openapi/spec" "github.com/stretchr/testify/assert" ) @@ -29,6 +30,17 @@ func TestTransToValidSchemeType(t *testing.T) { TransToValidSchemeType("oops") } +func TestTransToValidCollectionFormat(t *testing.T) { + assert.Equal(t, TransToValidCollectionFormat("csv"), "csv") + assert.Equal(t, TransToValidCollectionFormat("multi"), "multi") + assert.Equal(t, TransToValidCollectionFormat("pipes"), "pipes") + assert.Equal(t, TransToValidCollectionFormat("tsv"), "tsv") + assert.Equal(t, TransToValidSchemeType("string"), STRING) + + // should accept any type, due to user defined types + assert.Equal(t, TransToValidCollectionFormat("oops"), "") +} + func TestIsGolangPrimitiveType(t *testing.T) { assert.Equal(t, IsGolangPrimitiveType("uint"), true) @@ -50,6 +62,63 @@ func TestIsGolangPrimitiveType(t *testing.T) { assert.Equal(t, IsGolangPrimitiveType("oops"), false) } +func TestIsSimplePrimitiveType(t *testing.T) { + + assert.Equal(t, IsSimplePrimitiveType("string"), true) + assert.Equal(t, IsSimplePrimitiveType("number"), true) + assert.Equal(t, IsSimplePrimitiveType("integer"), true) + assert.Equal(t, IsSimplePrimitiveType("boolean"), true) + + assert.Equal(t, IsSimplePrimitiveType("oops"), false) +} + +func TestBuildCustomSchema(t *testing.T) { + var schema *spec.Schema + var err error + + schema, err = BuildCustomSchema([]string{}) + assert.NoError(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"primitive"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"primitive", "oops"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"primitive", "string"}) + assert.NoError(t, err) + assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"string"}) + + schema, err = BuildCustomSchema([]string{"array"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"array", "oops"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"array", "string"}) + assert.NoError(t, err) + assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"array"}) + assert.Equal(t, schema.SchemaProps.Items.Schema.SchemaProps.Type, spec.StringOrArray{"string"}) + + schema, err = BuildCustomSchema([]string{"object"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"object", "oops"}) + assert.Error(t, err) + assert.Nil(t, schema) + + schema, err = BuildCustomSchema([]string{"object", "string"}) + assert.NoError(t, err) + assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"object"}) + assert.Equal(t, schema.SchemaProps.AdditionalProperties.Schema.Type, spec.StringOrArray{"string"}) +} + func TestIsNumericType(t *testing.T) { assert.Equal(t, IsNumericType(INTEGER), true) assert.Equal(t, IsNumericType(NUMBER), true) From 7626a0e1bfb1307c6371ba2a5086cb86b0450333 Mon Sep 17 00:00:00 2001 From: Arrim Date: Mon, 26 Oct 2020 13:53:20 +0300 Subject: [PATCH 5/5] Add test --- parser_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/parser_test.go b/parser_test.go index 2e8c936be..edd073f39 100644 --- a/parser_test.go +++ b/parser_test.go @@ -2477,3 +2477,60 @@ func Fun() { assert.False(t, ok) assert.Empty(t, childName) } + +func TestDefineTypeOfExample(t *testing.T) { + var example interface{} + var err error + + example, err = defineTypeOfExample("string", "", "example") + assert.NoError(t, err) + assert.Equal(t, example.(string), "example") + + example, err = defineTypeOfExample("number", "", "12.34") + assert.NoError(t, err) + assert.Equal(t, example.(float64), 12.34) + + example, err = defineTypeOfExample("boolean", "", "true") + assert.NoError(t, err) + assert.Equal(t, example.(bool), true) + + example, err = defineTypeOfExample("array", "", "one,two,three") + assert.Error(t, err) + assert.Nil(t, example) + + example, err = defineTypeOfExample("array", "string", "one,two,three") + assert.NoError(t, err) + arr := []string{} + + for _, v := range example.([]interface{}) { + arr = append(arr, v.(string)) + } + + assert.Equal(t, arr, []string{"one", "two", "three"}) + + example, err = defineTypeOfExample("object", "", "key_one:one,key_two:two,key_three:three") + assert.Error(t, err) + assert.Nil(t, example) + + example, err = defineTypeOfExample("object", "string", "key_one,key_two,key_three") + assert.Error(t, err) + assert.Nil(t, example) + + example, err = defineTypeOfExample("object", "oops", "key_one:one,key_two:two,key_three:three") + assert.Error(t, err) + assert.Nil(t, example) + + example, err = defineTypeOfExample("object", "string", "key_one:one,key_two:two,key_three:three") + assert.NoError(t, err) + obj := map[string]string{} + + for k, v := range example.(map[string]interface{}) { + obj[k] = v.(string) + } + + assert.Equal(t, obj, map[string]string{"key_one": "one", "key_two": "two", "key_three": "three"}) + + example, err = defineTypeOfExample("oops", "", "") + assert.Error(t, err) + assert.Nil(t, example) +}