command/format: Ignore removal of empty strings

This commit is contained in:
Radek Simko 2019-01-13 22:51:05 +00:00
parent 21d65cfa9a
commit d96f4fa77b
No known key found for this signature in database
GPG Key ID: 1F1C84FE689A88D7
2 changed files with 44 additions and 3 deletions

View File

@ -855,13 +855,33 @@ func (p *blockBodyDiffPrinter) pathForcesNewResource(path cty.Path) bool {
return p.requiredReplace.Has(path)
}
func ctyEmptyString(value cty.Value) bool {
if !value.IsNull() && value.IsKnown() {
valueType := value.Type()
if valueType == cty.String && value.AsString() == "" {
return true
}
}
return false
}
func ctyGetAttrMaybeNull(val cty.Value, name string) cty.Value {
attrType := val.Type().AttributeType(name)
if val.IsNull() {
ty := val.Type().AttributeType(name)
return cty.NullVal(ty)
return cty.NullVal(attrType)
}
return val.GetAttr(name)
// We treat "" as null here
// as existing SDK doesn't support null yet.
// This allows us to avoid spurious diffs
// until we introduce null to the SDK.
attrValue := val.GetAttr(name)
if ctyEmptyString(attrValue) {
return cty.NullVal(attrType)
}
return attrValue
}
func ctyCollectionValues(val cty.Value) []cty.Value {

View File

@ -48,6 +48,27 @@ func TestResourceChange_primitiveTypes(t *testing.T) {
- resource "test_instance" "example" {
- id = "i-02ae66f368e8518a9" -> null
}
`,
},
"deletion (empty string)": {
Action: plans.Delete,
Mode: addrs.ManagedResourceMode,
Before: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("i-02ae66f368e8518a9"),
"intentionally_long": cty.StringVal(""),
}),
After: cty.NullVal(cty.EmptyObject),
Schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"intentionally_long": {Type: cty.String, Optional: true},
},
},
RequiredReplace: cty.NewPathSet(),
ExpectedOutput: ` # test_instance.example will be destroyed
- resource "test_instance" "example" {
- id = "i-02ae66f368e8518a9" -> null
}
`,
},
"string in-place update": {