Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions tftypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,37 @@ func (val Value) IsNull() bool {
return val.value == nil
}

func (val Value) IsFullyNull() bool {
switch val.Type().(type) {
case primitive:
return val.IsNull()
case List, Set, Tuple:
sliceVal, ok := val.value.([]Value)
if !ok {
return true
}
for _, v := range sliceVal {
if !v.IsFullyNull() {
return false
}
}
return true
case Map, Object:
mapVal, ok := val.value.(map[string]Value)
if !ok {
return true
}
for _, v := range mapVal {
if !v.IsFullyNull() {
return false
}
}
return true
default:
panic(fmt.Sprintf("unknown type %T", val.Type()))
}
}

// MarshalMsgPack returns a msgpack representation of the Value. This is used
// for constructing tfprotov5.DynamicValues.
//
Expand Down
100 changes: 100 additions & 0 deletions tftypes/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,106 @@ func TestValueIsKnown(t *testing.T) {
}
}

func TestValueIsNull(t *testing.T) {
t.Parallel()
type testCase struct {
value Value
expectedIsNull bool
expectedIsFullyNull bool
}

simpleObjectTyp := Object{
AttributeTypes: map[string]Type{
"capacity": Number,
},
OptionalAttributes: map[string]struct{}{
"capacity": {},
},
}

networkTyp := Object{
AttributeTypes: map[string]Type{
"name": String,
"speed": Number,
},
}
objectTyp := Object{
AttributeTypes: map[string]Type{
"id": String,
"network": networkTyp,
},
}

tests := map[string]testCase{
"nil-object": {
value: NewValue(simpleObjectTyp, nil),
expectedIsNull: true,
expectedIsFullyNull: true,
},
"simple-object-with-empty-attributes-map": {
value: NewValue(simpleObjectTyp, map[string]Value{}),
expectedIsNull: false,
expectedIsFullyNull: true,
},
"simple-object-with-nil-primitive": {
value: NewValue(simpleObjectTyp, map[string]Value{"capacity": NewValue(Number, nil)}),
expectedIsNull: false,
expectedIsFullyNull: true,
},
"simple-object-with-non-nil-primitive": {
value: NewValue(simpleObjectTyp, map[string]Value{"capacity": NewValue(Number, 4096)}),
expectedIsNull: false,
expectedIsFullyNull: false,
},
"object-with-no-nils": {
value: NewValue(objectTyp, map[string]Value{
"id": NewValue(String, "#00decaf"),
"network": NewValue(networkTyp, map[string]Value{
"name": NewValue(String, "eth0"),
"speed": NewValue(Number, 1000000000),
}),
}),
expectedIsNull: false,
expectedIsFullyNull: false,
},
"object-with-shallow-nils": {
value: NewValue(objectTyp, map[string]Value{
"id": NewValue(String, nil),
"network": NewValue(networkTyp, nil),
}),
expectedIsNull: false,
expectedIsFullyNull: true,
},
"object-with-deep-nils": {
value: NewValue(objectTyp, map[string]Value{
"id": NewValue(String, nil),
"network": NewValue(networkTyp, map[string]Value{
"name": NewValue(String, nil),
"speed": NewValue(Number, nil),
}),
}),
expectedIsNull: false,
expectedIsFullyNull: true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
actualIsNull := test.value.IsNull()
actualIsFullyNull := test.value.IsFullyNull()

if test.expectedIsNull != actualIsNull {
t.Errorf("expected IsNull() to be %v; actual: %v", test.expectedIsNull, actualIsNull)
}

if test.expectedIsFullyNull != actualIsFullyNull {
t.Errorf("expected IsFullyNull() to be %v; actual: %v", test.expectedIsNull, actualIsNull)
}
})
}
}

func TestValueEqual(t *testing.T) {
t.Parallel()
type testCase struct {
Expand Down
Loading