Skip to content

Commit

Permalink
float: return error when marshaling NaN or Inf
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Dec 14, 2017
1 parent 5d2d1e5 commit 26f8e13
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
)
Expand Down Expand Up @@ -91,6 +92,12 @@ func (f Float) MarshalJSON() ([]byte, error) {
if !f.Valid {
return []byte("null"), nil
}
if math.IsInf(f.Float64, 0) || math.IsNaN(f.Float64) {
return nil, &json.UnsupportedValueError{
Value: reflect.ValueOf(f.Float64),
Str: strconv.FormatFloat(f.Float64, 'g', -1, 64),
}
}
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
}

Expand Down
15 changes: 15 additions & 0 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package null

import (
"encoding/json"
"math"
"testing"
)

Expand Down Expand Up @@ -170,6 +171,20 @@ func TestFloatScan(t *testing.T) {
assertNullFloat(t, null, "scanned null")
}

func TestFloatInfNaN(t *testing.T) {
nan := NewFloat(math.NaN(), true)
_, err := nan.MarshalJSON()
if err == nil {
t.Error("expected error for NaN, got nil")
}

inf := NewFloat(math.Inf(1), true)
_, err = inf.MarshalJSON()
if err == nil {
t.Error("expected error for Inf, got nil")
}
}

func assertFloat(t *testing.T, f Float, from string) {
if f.Float64 != 1.2345 {
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
Expand Down
7 changes: 7 additions & 0 deletions zero/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
)
Expand Down Expand Up @@ -92,6 +93,12 @@ func (f Float) MarshalJSON() ([]byte, error) {
if !f.Valid {
n = 0
}
if math.IsInf(f.Float64, 0) || math.IsNaN(f.Float64) {
return nil, &json.UnsupportedValueError{
Value: reflect.ValueOf(f.Float64),
Str: strconv.FormatFloat(f.Float64, 'g', -1, 64),
}
}
return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
}

Expand Down
15 changes: 15 additions & 0 deletions zero/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zero

import (
"encoding/json"
"math"
"testing"
)

Expand Down Expand Up @@ -176,6 +177,20 @@ func TestFloatScan(t *testing.T) {
assertNullFloat(t, null, "scanned null")
}

func TestFloatInfNaN(t *testing.T) {
nan := NewFloat(math.NaN(), true)
_, err := nan.MarshalJSON()
if err == nil {
t.Error("expected error for NaN, got nil")
}

inf := NewFloat(math.Inf(1), true)
_, err = inf.MarshalJSON()
if err == nil {
t.Error("expected error for Inf, got nil")
}
}

func assertFloat(t *testing.T, f Float, from string) {
if f.Float64 != 1.2345 {
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
Expand Down

0 comments on commit 26f8e13

Please sign in to comment.