@@ -25,10 +25,9 @@ import (
25
25
"github.com/openmfp/golang-commons/logger"
26
26
)
27
27
28
- // convertLabels transforms labels and annotations between map and array formats
29
- // toArray=true: map[string]string → []Label (for GraphQL output)
30
- // toArray=false: []Label → map[string]string (for Kubernetes input)
31
- func convertLabels (obj any , toArray bool ) any {
28
+ // convertMapsToArrays transforms labels and annotations from maps to arrays (for GraphQL output)
29
+ // map[string]string → []Label
30
+ func convertMapsToArrays (obj any ) any {
32
31
objMap , ok := obj .(map [string ]interface {})
33
32
if ! ok {
34
33
return obj
@@ -45,69 +44,82 @@ func convertLabels(obj any, toArray bool) any {
45
44
return obj
46
45
}
47
46
48
- // Clone the object
49
- result := make (map [string ]interface {})
50
- for k , v := range objMap {
51
- result [k ] = v
47
+ // Transform labels and annotations to arrays (in-place)
48
+ for k , v := range metadataMap {
49
+ if (k == "labels" || k == "annotations" ) && v != nil {
50
+ metadataMap [k ] = mapToArray (v )
51
+ }
52
+ }
53
+
54
+ return obj
55
+ }
56
+
57
+ // convertArraysToMaps transforms labels and annotations from arrays to maps (for Kubernetes input)
58
+ // []Label → map[string]string
59
+ func convertArraysToMaps (obj any ) any {
60
+ objMap , ok := obj .(map [string ]interface {})
61
+ if ! ok {
62
+ return obj
63
+ }
64
+
65
+ // Check if this object has metadata
66
+ metadata , hasMetadata := objMap ["metadata" ]
67
+ if ! hasMetadata {
68
+ return obj
69
+ }
70
+
71
+ metadataMap , ok := metadata .(map [string ]interface {})
72
+ if ! ok {
73
+ return obj
52
74
}
53
75
54
- // Transform only labels and annotations in metadata
55
- newMetadata := make (map [string ]interface {})
76
+ // Transform labels and annotations to maps (in-place)
56
77
for k , v := range metadataMap {
57
78
if (k == "labels" || k == "annotations" ) && v != nil {
58
- newMetadata [k ] = transformLabelField (v , toArray )
59
- } else {
60
- newMetadata [k ] = v
79
+ metadataMap [k ] = arrayToMap (v )
61
80
}
62
81
}
63
82
64
- result ["metadata" ] = newMetadata
65
- return result
83
+ return obj
66
84
}
67
85
68
- // transformLabelField does the actual conversion between formats
69
- func transformLabelField (value any , toArray bool ) any {
70
- if toArray {
71
- // map[string]string → []Label
72
- labelMap , ok := value .(map [string ]interface {})
73
- if ! ok {
74
- return value
75
- }
76
-
77
- var labels []map [string ]interface {}
78
- for k , v := range labelMap {
79
- if strValue , ok := v .(string ); ok {
80
- labels = append (labels , map [string ]interface {}{
81
- "key" : k ,
82
- "value" : strValue ,
83
- })
84
- }
86
+ // mapToArray converts a label map to array format
87
+ func mapToArray (value any ) any {
88
+ labelMap , ok := value .(map [string ]interface {})
89
+ if ! ok {
90
+ return value
91
+ }
92
+
93
+ var labels []map [string ]interface {}
94
+ for k , v := range labelMap {
95
+ if strValue , ok := v .(string ); ok {
96
+ labels = append (labels , map [string ]interface {}{
97
+ "key" : k ,
98
+ "value" : strValue ,
99
+ })
85
100
}
86
- return labels
87
- } else {
88
- // []Label → map[string]string
89
- labelArray , ok := value .([]interface {})
90
- if ! ok {
91
- return value
92
- }
93
-
94
- labelMap := make (map [string ]string )
95
- for _ , item := range labelArray {
96
- if labelObj , ok := item .(map [string ]interface {}); ok {
97
- if key , keyOk := labelObj ["key" ].(string ); keyOk {
98
- if val , valOk := labelObj ["value" ].(string ); valOk {
99
- labelMap [key ] = val
100
- }
101
+ }
102
+ return labels
103
+ }
104
+
105
+ // arrayToMap converts a label array to map format
106
+ func arrayToMap (value any ) any {
107
+ labelArray , ok := value .([]interface {})
108
+ if ! ok {
109
+ return value
110
+ }
111
+
112
+ labelMap := make (map [string ]string )
113
+ for _ , item := range labelArray {
114
+ if labelObj , ok := item .(map [string ]interface {}); ok {
115
+ if key , keyOk := labelObj ["key" ].(string ); keyOk {
116
+ if val , valOk := labelObj ["value" ].(string ); valOk {
117
+ labelMap [key ] = val
101
118
}
102
119
}
103
120
}
104
- return labelMap
105
121
}
106
- }
107
-
108
- // isLabelField checks if a field is labels or annotations
109
- func isLabelField (fieldName string ) bool {
110
- return fieldName == "labels" || fieldName == "annotations"
122
+ return labelMap
111
123
}
112
124
113
125
type Provider interface {
@@ -215,7 +227,7 @@ func (r *Service) ListItems(gvk schema.GroupVersionKind, scope v1.ResourceScope)
215
227
items := make ([]map [string ]any , len (list .Items ))
216
228
for i , item := range list .Items {
217
229
// Convert maps back to label arrays for GraphQL response
218
- convertedItem := convertLabels (item .Object , true ).(map [string ]interface {})
230
+ convertedItem := convertMapsToArrays (item .Object ).(map [string ]interface {})
219
231
items [i ] = convertedItem
220
232
}
221
233
@@ -273,7 +285,7 @@ func (r *Service) GetItem(gvk schema.GroupVersionKind, scope v1.ResourceScope) g
273
285
}
274
286
275
287
// Convert maps back to label arrays for GraphQL response
276
- convertedResponse := convertLabels (obj .Object , true )
288
+ convertedResponse := convertMapsToArrays (obj .Object )
277
289
return convertedResponse , nil
278
290
}
279
291
}
@@ -310,7 +322,7 @@ func (r *Service) CreateItem(gvk schema.GroupVersionKind, scope v1.ResourceScope
310
322
objectInput := p .Args ["object" ].(map [string ]interface {})
311
323
312
324
// Convert label arrays back to maps for Kubernetes compatibility
313
- convertedInput := convertLabels (objectInput , false ).(map [string ]interface {})
325
+ convertedInput := convertArraysToMaps (objectInput ).(map [string ]interface {})
314
326
315
327
obj := & unstructured.Unstructured {
316
328
Object : convertedInput ,
@@ -344,7 +356,7 @@ func (r *Service) CreateItem(gvk schema.GroupVersionKind, scope v1.ResourceScope
344
356
}
345
357
346
358
// Convert maps back to label arrays for GraphQL response
347
- convertedResponse := convertLabels (obj .Object , true )
359
+ convertedResponse := convertMapsToArrays (obj .Object )
348
360
return convertedResponse , nil
349
361
}
350
362
}
@@ -365,7 +377,7 @@ func (r *Service) UpdateItem(gvk schema.GroupVersionKind, scope v1.ResourceScope
365
377
366
378
objectInput := p .Args ["object" ].(map [string ]interface {})
367
379
// Convert label arrays back to maps for Kubernetes compatibility
368
- convertedInput := convertLabels (objectInput , false ).( map [ string ] interface {} )
380
+ convertedInput := convertArraysToMaps (objectInput )
369
381
// Marshal the converted input object to JSON to create the patch data
370
382
patchData , err := json .Marshal (convertedInput )
371
383
if err != nil {
@@ -409,7 +421,7 @@ func (r *Service) UpdateItem(gvk schema.GroupVersionKind, scope v1.ResourceScope
409
421
}
410
422
411
423
// Convert maps back to label arrays for GraphQL response
412
- convertedResponse := convertLabels (existingObj .Object , true )
424
+ convertedResponse := convertMapsToArrays (existingObj .Object )
413
425
return convertedResponse , nil
414
426
}
415
427
}
0 commit comments