Return warning for nil primitive types during normalization (#1329)

## Changes

It's not necessary to error out if a configuration field is present but
not set.

For example, the following would error out, but after this change only
produces a warning:
```yaml
workspace:
  # This is a string field, but if not specified, it ends up being a null.
  host:
```

## Tests

Updated the unit tests to match the new behavior.

---------

Co-authored-by: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com>
This commit is contained in:
Pieter Noordhuis 2024-04-02 14:17:29 +02:00 committed by GitHub
parent cddc5f97f8
commit dca81a40f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 8 deletions

View File

@ -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,
@ -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))
}
@ -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))
}
@ -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))
}
@ -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))
}

View File

@ -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])
}
@ -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])
}
@ -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])
}
@ -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])
}