Skip to content

Commit

Permalink
Added delay MarshalJSON (#143)
Browse files Browse the repository at this point in the history
* Added delay MarshalJSON

* Added post mapping into swagger
  • Loading branch information
jmartin82 authored May 10, 2023
1 parent 4f9b372 commit b5a63ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
24 changes: 14 additions & 10 deletions pkg/mock/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"`
Expand Down
38 changes: 37 additions & 1 deletion tmpl/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -446,7 +482,7 @@
},
"delay": {
"description": "",
"type": "string",
"type": "string"
},
"crazy": {
"description": "",
Expand Down

0 comments on commit b5a63ab

Please sign in to comment.