@@ -68,17 +68,8 @@ type SetCustomFieldAction interface {
68
68
platform.CartDiscountSetCustomFieldAction
69
69
}
70
70
71
- func CustomFieldCreateFieldContainer (data map [string ]any ) * platform.FieldContainer {
72
- if raw , ok := data ["fields" ].(map [string ]any ); ok {
73
- fields := platform .FieldContainer (raw )
74
- return & fields
75
- }
76
- return nil
77
- }
78
-
79
71
func customFieldEncodeType (t * platform.Type , name string , value any ) (any , error ) {
80
- // Sub-optimal to do this everytime, however performance is not that
81
- // important here and impact is neglible
72
+ // Suboptimal to do this everytime, however performance is not that important here and impact is negligible
82
73
fieldTypes := map [string ]platform.FieldType {}
83
74
for _ , field := range t .FieldDefinitions {
84
75
fieldTypes [field .Name ] = field .Type
@@ -124,7 +115,18 @@ func customFieldEncodeValue(t platform.FieldType, name string, value any) (any,
124
115
125
116
result := make ([]any , len (values ))
126
117
for i := range values {
127
- itemValue , err := customFieldEncodeValue (v .ElementType , name , values [i ])
118
+ var element = values [i ]
119
+ _ , ok := element .(string )
120
+
121
+ //We need to re-marshal the data here, so we can recursively pass it back to the encoding function
122
+ if ! ok {
123
+ marshalledValue , err := json .Marshal (values [i ])
124
+ if err != nil {
125
+ return nil , err
126
+ }
127
+ element = string (marshalledValue )
128
+ }
129
+ itemValue , err := customFieldEncodeValue (v .ElementType , name , element )
128
130
if err != nil {
129
131
return nil , err
130
132
}
@@ -263,22 +265,22 @@ func CustomFieldUpdateActions[T SetCustomTypeAction, F SetCustomFieldAction](ctx
263
265
return nil , err
264
266
}
265
267
266
- old , new := d .GetChange ("custom" )
267
- old_data := firstElementFromSlice (old .([]any ))
268
- new_data := firstElementFromSlice (new .([]any ))
269
- old_type_id := old_data ["type_id" ]
270
- new_type_id := new_data ["type_id" ]
268
+ oldState , newState := d .GetChange ("custom" )
269
+ oldData := firstElementFromSlice (oldState .([]any ))
270
+ newData := firstElementFromSlice (newState .([]any ))
271
+ oldTypeId := oldData ["type_id" ]
272
+ newTypeId := newData ["type_id" ]
271
273
272
274
// Remove custom field from resource
273
- if new_type_id == nil {
275
+ if newTypeId == nil {
274
276
action := T {
275
277
Type : nil ,
276
278
}
277
279
return []any {action }, nil
278
280
}
279
281
280
- if old_type_id == nil || (old_type_id .(string ) != new_type_id .(string )) {
281
- value , err := CreateCustomFieldDraftRaw (new_data , t )
282
+ if oldTypeId == nil || (oldTypeId .(string ) != newTypeId .(string )) {
283
+ value , err := CreateCustomFieldDraftRaw (newData , t )
282
284
if err != nil {
283
285
return nil , err
284
286
}
@@ -290,10 +292,10 @@ func CustomFieldUpdateActions[T SetCustomTypeAction, F SetCustomFieldAction](ctx
290
292
}
291
293
292
294
changes := diffSlices (
293
- old_data ["fields" ].(map [string ]any ),
294
- new_data ["fields" ].(map [string ]any ))
295
+ oldData ["fields" ].(map [string ]any ),
296
+ newData ["fields" ].(map [string ]any ))
295
297
296
- result := []any {}
298
+ var result []any
297
299
for key := range changes {
298
300
val , err := customFieldEncodeType (t , key , changes [key ])
299
301
if err != nil {
0 commit comments