normalize empty CheckResults fields in stateV4

Ensure that empty check results are normalized in state serialization to
prevent unexpected state changes from being written.

Because there is no consistent empty, null and omit_empty usage for
state structs, there's no good way to create a test which will fail
for future additions.
This commit is contained in:
James Bardin 2022-10-26 10:33:21 -04:00
parent fa4c652013
commit eae246cfb5

View File

@ -414,9 +414,7 @@ func writeStateV4(file *File, w io.Writer) tfdiags.Diagnostics {
}
}
if file.State.CheckResults != nil {
sV4.CheckResults = encodeCheckResultsV4(file.State.CheckResults)
}
sV4.CheckResults = encodeCheckResultsV4(file.State.CheckResults)
sV4.normalize()
@ -576,6 +574,11 @@ func decodeCheckResultsV4(in []checkResultsV4) (*states.CheckResults, tfdiags.Di
}
func encodeCheckResultsV4(in *states.CheckResults) []checkResultsV4 {
// normalize empty and nil sets in the serialized state
if in == nil || in.ConfigResults.Len() == 0 {
return nil
}
ret := make([]checkResultsV4, 0, in.ConfigResults.Len())
for _, configElem := range in.ConfigResults.Elems {