Skip to content

Commit 3165432

Browse files
committed
Explicitly test for attempts to unmarshal to float64 and fail
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
1 parent edb0728 commit 3165432

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

pkg/fftypes/jsonany.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (h *JSONAny) Unmarshal(ctx context.Context, v interface{}) error {
7878
return i18n.NewError(ctx, i18n.MsgNilOrNullObject)
7979
}
8080

81+
if _, ok := v.(*float64); ok {
82+
return i18n.NewError(ctx, i18n.MsgUnmarshalToFloat64NotSupported)
83+
}
84+
8185
d := json.NewDecoder(strings.NewReader(h.String()))
8286
d.UseNumber()
8387
if err := d.Decode(v); err != nil {

pkg/fftypes/jsonany_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,35 @@ func TestUnmarshal(t *testing.T) {
183183

184184
func TestUnmarshalHugeNumber(t *testing.T) {
185185

186+
var myInt64Variable int64
187+
var myFloat64Variable float64
188+
ctx := context.Background()
186189
var h *JSONAny
187190
var myObj struct {
188191
Key1 interface{} `json:"key1"`
192+
Key2 JSONAny `json:"key2"`
193+
Key3 JSONAny `json:"key3"`
189194
}
190195

191-
h = JSONAnyPtr(`{"key1":123456789123456789123456789}`)
192-
err := h.Unmarshal(context.Background(), &myObj)
196+
h = JSONAnyPtr(`{"key1":123456789123456789123456789, "key2":123456789123456789123456789, "key3":1234}`)
197+
err := h.Unmarshal(ctx, &myObj)
193198
assert.NoError(t, err)
194199
assert.Equal(t, json.Number("123456789123456789123456789"), myObj.Key1)
200+
201+
assert.NoError(t, err)
202+
assert.Equal(t, "123456789123456789123456789", myObj.Key2.String())
203+
204+
err = myObj.Key2.Unmarshal(ctx, &myInt64Variable)
205+
assert.Error(t, err)
206+
assert.Regexp(t, "cannot unmarshal number 123456789123456789123456789 into Go value of type int64", err)
207+
208+
err = myObj.Key3.Unmarshal(ctx, &myInt64Variable)
209+
assert.NoError(t, err)
210+
assert.Equal(t, int64(1234), myInt64Variable)
211+
212+
err = myObj.Key2.Unmarshal(ctx, &myFloat64Variable)
213+
assert.Error(t, err)
214+
assert.Regexp(t, "FF00249", err)
195215
}
196216

197217
func TestUnmarshalHugeNumberError(t *testing.T) {

pkg/i18n/en_base_error_messages.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,5 @@ var (
183183
MsgDBExecFailed = ffe("FF00245", "Database update failed")
184184
MsgDBErrorBuildingStatement = ffe("FF00247", "Error building statement: %s")
185185
MsgDBReadInsertTSFailed = ffe("FF00248", "Failed to read timestamp from database optimized upsert: %s")
186+
MsgUnmarshalToFloat64NotSupported = ffe("FF00249", "Unmarshalling to a float64 is not supported due to possible precision loss. Consider unmarshalling to an interface, json.Number or fftypes.JSONAny instead")
186187
)

0 commit comments

Comments
 (0)