2
0

implement json.Marshaler and json.Unmarshaler for Float4, Float8

This commit is contained in:
Kirill Mironov
2023-11-04 14:00:41 +03:00
committed by Jack Christensen
parent cf6ef75f91
commit d3fb6e00da
4 changed files with 129 additions and 7 deletions
+41
View File
@@ -21,3 +21,44 @@ func TestFloat4Codec(t *testing.T) {
{nil, new(*float32), isExpectedEq((*float32)(nil))},
})
}
func TestFloat4MarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Float4
result string
}{
{source: pgtype.Float4{Float32: 0}, result: "null"},
{source: pgtype.Float4{Float32: 1.23, Valid: true}, result: "1.23"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
if string(r) != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
}
}
}
func TestFloat4UnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Float4
}{
{source: "null", result: pgtype.Float4{Float32: 0}},
{source: "1.23", result: pgtype.Float4{Float32: 1.23, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Float4
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}