Merge pull request #19990 from hashicorp/f-cmd-fmt-ignore-empty-str

command/format: Ignore removal of empty strings
This commit is contained in:
Radek Simko 2019-01-14 20:33:43 +00:00 committed by GitHub
commit aae87c7921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) 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 { func ctyGetAttrMaybeNull(val cty.Value, name string) cty.Value {
attrType := val.Type().AttributeType(name)
if val.IsNull() { if val.IsNull() {
ty := val.Type().AttributeType(name) return cty.NullVal(attrType)
return cty.NullVal(ty)
} }
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 { func ctyCollectionValues(val cty.Value) []cty.Value {

View File

@ -48,6 +48,27 @@ func TestResourceChange_primitiveTypes(t *testing.T) {
- resource "test_instance" "example" { - resource "test_instance" "example" {
- id = "i-02ae66f368e8518a9" -> null - 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": { "string in-place update": {