Skip to content

Commit

Permalink
fix numeric infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
acim committed Dec 4, 2022
1 parent 4df6131 commit f14f8f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pgtype/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}

if n.NaN {
switch {
case n.InfinityModifier == Infinity:
return []byte(`"infinity"`), nil
case n.InfinityModifier == NegativeInfinity:
return []byte(`"-infinity"`), nil
case n.NaN:
return []byte(`"NaN"`), nil
}

Expand Down
4 changes: 2 additions & 2 deletions pgtype/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,14 @@ func TestRangeNumericMarshalJSON(t *testing.T) {
LowerType: Exclusive,
UpperType: Exclusive,
Valid: true,
}, result: `"(NaN,16)"`},
}, result: `"(-infinity,16)"`},
{src: Range[Numeric]{
Lower: Numeric{Int: big.NewInt(-16), Valid: true},
Upper: Numeric{InfinityModifier: Infinity, NaN: true, Valid: true},
LowerType: Inclusive,
UpperType: Exclusive,
Valid: true,
}, result: `"[-16,NaN)"`},
}, result: `"[-16,infinity)"`},
}

for i, tt := range tests {
Expand Down

0 comments on commit f14f8f6

Please sign in to comment.