Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert between integer and float in normalization #1371

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libs/dyn/convert/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ func (n normalizeOptions) normalizeInt(typ reflect.Type, src dyn.Value, path dyn
switch src.Kind() {
case dyn.KindInt:
out = src.MustInt()
case dyn.KindFloat:
out = int64(src.MustFloat())
if src.MustFloat() != float64(out) {
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf(`cannot accurately represent "%g" as integer due to precision loss`, src.MustFloat()),
Location: src.Location(),
Path: path,
})
}
case dyn.KindString:
var err error
out, err = strconv.ParseInt(src.MustString(), 10, 64)
Expand Down Expand Up @@ -326,6 +336,16 @@ func (n normalizeOptions) normalizeFloat(typ reflect.Type, src dyn.Value, path d
switch src.Kind() {
case dyn.KindFloat:
out = src.MustFloat()
case dyn.KindInt:
out = float64(src.MustInt())
if src.MustInt() != int64(out) {
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf(`cannot accurately represent "%d" as floating point number due to precision loss`, src.MustInt()),
Location: src.Location(),
Path: path,
})
}
case dyn.KindString:
var err error
out, err = strconv.ParseFloat(src.MustString(), 64)
Expand Down
46 changes: 46 additions & 0 deletions libs/dyn/convert/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,27 @@ func TestNormalizeIntNil(t *testing.T) {
}, err[0])
}

func TestNormalizeIntFromFloat(t *testing.T) {
var typ int
vin := dyn.V(float64(1.0))
vout, err := Normalize(&typ, vin)
assert.Empty(t, err)
assert.Equal(t, dyn.V(int64(1)), vout)
}

func TestNormalizeIntFromFloatError(t *testing.T) {
var typ int
vin := dyn.V(1.5)
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Warning,
Summary: `cannot accurately represent "1.5" as integer due to precision loss`,
Location: vin.Location(),
Path: dyn.EmptyPath,
}, err[0])
}

func TestNormalizeIntFromString(t *testing.T) {
var typ int
vin := dyn.V("123")
Expand Down Expand Up @@ -618,6 +639,31 @@ func TestNormalizeFloatNil(t *testing.T) {
}, err[0])
}

func TestNormalizeFloatFromInt(t *testing.T) {
var typ float64

// Maximum safe integer that can be accurately represented as a float.
vin := dyn.V(int64(9007199254740992))
vout, err := Normalize(&typ, vin)
assert.Empty(t, err)
assert.Equal(t, dyn.V(float64(9007199254740992)), vout)
}

func TestNormalizeFloatFromIntError(t *testing.T) {
var typ float64

// Minimum integer that cannot be accurately represented as a float.
vin := dyn.V(9007199254740992 + 1)
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Warning,
Summary: `cannot accurately represent "9007199254740993" as floating point number due to precision loss`,
Location: vin.Location(),
Path: dyn.EmptyPath,
}, err[0])
}

func TestNormalizeFloatFromString(t *testing.T) {
var typ float64
vin := dyn.V("1.2")
Expand Down
Loading