allow nullable override

This commit is contained in:
James Bardin 2021-10-29 17:01:26 -04:00
parent f0a64eb456
commit 7b7972ac95
4 changed files with 21 additions and 2 deletions

View File

@ -56,6 +56,10 @@ func (v *Variable) merge(ov *Variable) hcl.Diagnostics {
if ov.ParsingMode != 0 {
v.ParsingMode = ov.ParsingMode
}
if ov.NullableSet {
v.Nullable = ov.Nullable
v.NullableSet = ov.NullableSet
}
// If the override file overrode type without default or vice-versa then
// it may have created an invalid situation, which we'll catch now by
@ -100,6 +104,16 @@ func (v *Variable) merge(ov *Variable) hcl.Diagnostics {
} else {
v.Default = val
}
// ensure a null default wasn't merged in when it is not allowed
if !v.Nullable && v.Default.IsNull() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid default value for variable",
Detail: "A null default value is not valid when nullable=false.",
Subject: &ov.DeclRange,
})
}
}
return diags

View File

@ -24,7 +24,8 @@ func TestModuleOverrideVariable(t *testing.T) {
Description: "b_override description",
DescriptionSet: true,
Default: cty.StringVal("b_override"),
Nullable: true,
Nullable: false,
NullableSet: true,
Type: cty.String,
ConstraintType: cty.String,
ParsingMode: VariableParseLiteral,
@ -48,6 +49,7 @@ func TestModuleOverrideVariable(t *testing.T) {
DescriptionSet: true,
Default: cty.StringVal("b_override partial"),
Nullable: true,
NullableSet: false,
Type: cty.String,
ConstraintType: cty.String,
ParsingMode: VariableParseLiteral,

View File

@ -40,6 +40,7 @@ type Variable struct {
// Nullable to false means that the module can expect this variable to
// never be null.
Nullable bool
NullableSet bool
DeclRange hcl.Range
}
@ -118,6 +119,7 @@ func decodeVariableBlock(block *hcl.Block, override bool) (*Variable, hcl.Diagno
if attr, exists := content.Attributes["nullable"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &v.Nullable)
diags = append(diags, valDiags...)
v.NullableSet = true
} else {
// The current default is true, which is subject to change in a future
// language edition.

View File

@ -1,4 +1,5 @@
variable "fully_overridden" {
nullable = false
default = "b_override"
description = "b_override description"
type = string