From b5a63ab09e7fba55af942cb138cfcbb03c13da02 Mon Sep 17 00:00:00 2001 From: Jordi Martin Date: Wed, 10 May 2023 11:53:41 +0200 Subject: [PATCH] Added delay MarshalJSON (#143) * Added delay MarshalJSON * Added post mapping into swagger --- pkg/mock/definition.go | 24 ++++++++++++++---------- tmpl/swagger.json | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/pkg/mock/definition.go b/pkg/mock/definition.go index 3bf02c5..a527835 100644 --- a/pkg/mock/definition.go +++ b/pkg/mock/definition.go @@ -53,30 +53,34 @@ type Delay struct { time.Duration } +func (d Delay) MarshalJSON() (data []byte, err error) { + return json.Marshal(d.Duration.String()) +} + func (d *Delay) UnmarshalJSON(data []byte) (err error) { - var ( - v interface{} - s string - ) - if err = json.Unmarshal(data, &v); err != nil { + + var i interface{} + if err = json.Unmarshal(data, &i); err != nil { return err } - switch v.(type) { + switch v := i.(type) { case float64: - s = fmt.Sprintf("%ds", int(v.(float64))) fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("! DEPRECATION NOTICE: !") fmt.Println("! Please use a time unit (m,s,ms) to define the delay value. !") fmt.Println("! Ex: \"delay\":\"1s\" instead \"delay\":1 !") fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + s := fmt.Sprintf("%ds", int(v)) + d.Duration, err = time.ParseDuration(s) case string: - s = v.(string) + d.Duration, err = time.ParseDuration(v) + case time.Duration: + d.Duration = v default: return fmt.Errorf("invalid value for delay, got: %v", reflect.TypeOf(v)) } - d.Duration, err = time.ParseDuration(s) return err } @@ -89,7 +93,7 @@ type Control struct { WebHookURL string `json:"webHookURL"` } -//Definition contains the user mock config +// Definition contains the user mock config type Definition struct { URI string Description string `json:"description"` diff --git a/tmpl/swagger.json b/tmpl/swagger.json index 7bb7c35..bfd5fa5 100644 --- a/tmpl/swagger.json +++ b/tmpl/swagger.json @@ -277,6 +277,42 @@ } } }, + "post": { + "summary": "Create mock definition", + "tags": [ + "mapping" + ], + "operationId": "Create mock definition", + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "mock_path", + "in": "path", + "description": "Mock path", + "type": "string", + "required": true + }, + { + "name": "Body", + "in": "body", + "required": true, + "description": "", + "schema": { + "$ref": "#/definitions/MockDefinition" + } + } + ], + "responses": { + "200": { + "description": "Action result", + "schema": { + "$ref": "#/definitions/ActionResponse" + } + } + } + }, "delete": { "summary": "Delete mock definition", "tags": [ @@ -446,7 +482,7 @@ }, "delay": { "description": "", - "type": "string", + "type": "string" }, "crazy": { "description": "",