Skip to content

Commit

Permalink
Merge pull request #27 from akuracy/patch-1
Browse files Browse the repository at this point in the history
Allow to have numbers not unmarshaled as float
  • Loading branch information
rbeuque74 authored Jul 6, 2020
2 parents 3caf74b + 168c5d2 commit 302dfc0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ovh/ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,7 @@ func (c *Client) UnmarshalResponse(response *http.Response, resType interface{})
return nil
}

return json.Unmarshal(body, &resType)
d := json.NewDecoder(bytes.NewReader(body))
d.UseNumber()
return d.Decode(&resType)
}
46 changes: 46 additions & 0 deletions ovh/ovh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,52 @@ func TestGetResponse(t *testing.T) {
}
}

func TestGetResponseUnmarshalNumber(t *testing.T) {
var err error
var output map[string]interface{}
mockClient := Client{}

// with map[string]interface{} as output
err = mockClient.UnmarshalResponse(&http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)),
}, &output)
if err != nil {
t.Fatalf("Client.UnmarshalResponse should be able to decode the body")
}
if "1234567890" != fmt.Sprint(output["orderId"]) {
t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, stringified incorrectly")
}

var outputInt map[string]int64

// with map[string]int64 as output
err = mockClient.UnmarshalResponse(&http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)),
}, &outputInt)
if err != nil {
t.Fatalf("Client.UnmarshalResponse should be able to decode the body")
}
if int64(1234567890) != outputInt["orderId"] {
t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, incorrectly casted as int64")
}

var outputFloat map[string]float64

// with map[string]int64 as output
err = mockClient.UnmarshalResponse(&http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)),
}, &outputFloat)
if err != nil {
t.Fatalf("Client.UnmarshalResponse should be able to decode the body")
}
if float64(1234567890) != outputFloat["orderId"] {
t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, incorrectly casted as float64")
}
}

func TestConstructors(t *testing.T) {
// Nominal: full constructor
client, err := NewClient("ovh-eu", MockApplicationKey, MockApplicationSecret, MockConsumerKey)
Expand Down

0 comments on commit 302dfc0

Please sign in to comment.