44 "context"
55 "encoding/json"
66 "fmt"
7+ "slices"
78 "strings"
89
910 "github.com/hashicorp/terraform-plugin-framework/attr"
@@ -16,6 +17,8 @@ var _ basetypes.StringValuableWithSemanticEquals = (*LossyJson)(nil)
1617
1718type LossyJson struct {
1819 basetypes.StringValue
20+
21+ IgnoreKeys []string
1922}
2023
2124func (v LossyJson ) Type (_ context.Context ) attr.Type {
@@ -48,7 +51,7 @@ func (v LossyJson) StringSemanticEquals(_ context.Context, newValuable basetypes
4851 return false , diags
4952 }
5053
51- result , err := lossyJsonEqual (newValue .ValueString (), v .ValueString ())
54+ result , err := lossyJsonEqual (newValue .ValueString (), v .ValueString (), v . IgnoreKeys )
5255
5356 if err != nil {
5457 diags .AddError (
@@ -64,7 +67,7 @@ func (v LossyJson) StringSemanticEquals(_ context.Context, newValuable basetypes
6467 return result , diags
6568}
6669
67- func lossyJsonEqual (s1 , s2 string ) (bool , error ) {
70+ func lossyJsonEqual (s1 , s2 string , ignoreKeys [] string ) (bool , error ) {
6871 v1 , err := decodeJson (s1 )
6972 if err != nil {
7073 return false , err
@@ -75,7 +78,7 @@ func lossyJsonEqual(s1, s2 string) (bool, error) {
7578 return false , err
7679 }
7780
78- return deepLossyEqual (v1 , v2 ), nil
81+ return deepLossyEqual (v1 , v2 , ignoreKeys ), nil
7982}
8083
8184func decodeJson (s string ) (interface {}, error ) {
@@ -90,7 +93,7 @@ func decodeJson(s string) (interface{}, error) {
9093 return v , nil
9194}
9295
93- func deepLossyEqual (v1 , v2 interface {}) bool {
96+ func deepLossyEqual (v1 , v2 interface {}, ignoreKeys [] string ) bool {
9497 switch v1 := v1 .(type ) {
9598 case bool :
9699 v2 , ok := v2 .(bool )
@@ -127,7 +130,7 @@ func deepLossyEqual(v1, v2 interface{}) bool {
127130 }
128131
129132 for i := 0 ; i < len (v1 ); i ++ {
130- if ! deepLossyEqual (v1 [i ], v2 [i ]) {
133+ if ! deepLossyEqual (v1 [i ], v2 [i ], ignoreKeys ) {
131134 return false
132135 }
133136 }
@@ -145,12 +148,16 @@ func deepLossyEqual(v1, v2 interface{}) bool {
145148
146149 // Check that all keys in v1 are in v2 but not the other way around (lossy)
147150 for k := range v1 {
151+ if slices .Contains (ignoreKeys , k ) {
152+ continue
153+ }
154+
148155 v2Val , ok := v2 [k ]
149156 if ! ok {
150157 return false
151158 }
152159
153- if ! deepLossyEqual (v1 [k ], v2Val ) {
160+ if ! deepLossyEqual (v1 [k ], v2Val , ignoreKeys ) {
154161 return false
155162 }
156163 }
0 commit comments