Skip to content
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 @@ -61,6 +61,14 @@ func (n normalizeOptions) normalizeType(typ reflect.Type, src dyn.Value, seen []
return dyn.InvalidValue, diag.Errorf("unsupported type: %s", typ.Kind())
}

func nullWarning(expected dyn.Kind, src dyn.Value) diag.Diagnostic {
return diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("expected a %s value, found null", expected),
Location: src.Location(),
}
}

func typeMismatch(expected dyn.Kind, src dyn.Value) diag.Diagnostic {
return diag.Diagnostic{
Severity: diag.Error,
Expand Down Expand Up @@ -229,6 +237,9 @@ func (n normalizeOptions) normalizeString(typ reflect.Type, src dyn.Value) (dyn.
out = strconv.FormatInt(src.MustInt(), 10)
case dyn.KindFloat:
out = strconv.FormatFloat(src.MustFloat(), 'f', -1, 64)
case dyn.KindNil:
// Return a warning if the field is present but has a null value.
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindString, src))
default:
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindString, src))
}
Expand Down Expand Up @@ -259,6 +270,9 @@ func (n normalizeOptions) normalizeBool(typ reflect.Type, src dyn.Value) (dyn.Va
// Cannot interpret as a boolean.
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindBool, src))
}
case dyn.KindNil:
// Return a warning if the field is present but has a null value.
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindBool, src))
default:
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindBool, src))
}
Expand Down Expand Up @@ -288,6 +302,9 @@ func (n normalizeOptions) normalizeInt(typ reflect.Type, src dyn.Value) (dyn.Val
Location: src.Location(),
})
}
case dyn.KindNil:
// Return a warning if the field is present but has a null value.
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindInt, src))
default:
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindInt, src))
}
Expand Down Expand Up @@ -317,6 +334,9 @@ func (n normalizeOptions) normalizeFloat(typ reflect.Type, src dyn.Value) (dyn.V
Location: src.Location(),
})
}
case dyn.KindNil:
// Return a warning if the field is present but has a null value.
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindFloat, src))
default:
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindFloat, src))
}
Expand Down
16 changes: 8 additions & 8 deletions libs/dyn/convert/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ func TestNormalizeStringNil(t *testing.T) {
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Error,
Summary: `expected string, found nil`,
Severity: diag.Warning,
Summary: `expected a string value, found null`,
Location: vin.Location(),
}, err[0])
}
Expand Down Expand Up @@ -463,8 +463,8 @@ func TestNormalizeBoolNil(t *testing.T) {
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Error,
Summary: `expected bool, found nil`,
Severity: diag.Warning,
Summary: `expected a bool value, found null`,
Location: vin.Location(),
}, err[0])
}
Expand Down Expand Up @@ -536,8 +536,8 @@ func TestNormalizeIntNil(t *testing.T) {
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Error,
Summary: `expected int, found nil`,
Severity: diag.Warning,
Summary: `expected a int value, found null`,
Location: vin.Location(),
}, err[0])
}
Expand Down Expand Up @@ -596,8 +596,8 @@ func TestNormalizeFloatNil(t *testing.T) {
_, err := Normalize(&typ, vin)
assert.Len(t, err, 1)
assert.Equal(t, diag.Diagnostic{
Severity: diag.Error,
Summary: `expected float, found nil`,
Severity: diag.Warning,
Summary: `expected a float value, found null`,
Location: vin.Location(),
}, err[0])
}
Expand Down