@@ -25,62 +25,132 @@ import (
25
25
"github.com/openmfp/golang-commons/logger"
26
26
)
27
27
28
- // convertMapsToArrays transforms labels and annotations from maps to arrays (for GraphQL output)
28
+ // convertMapsToArrays transforms label-like fields from maps to arrays (for GraphQL output)
29
29
// map[string]string → []Label
30
30
func convertMapsToArrays (obj any ) any {
31
31
objMap , ok := obj .(map [string ]interface {})
32
32
if ! ok {
33
33
return obj
34
34
}
35
35
36
- // Check if this object has metadata
37
- metadata , hasMetadata := objMap ["metadata" ]
38
- if ! hasMetadata {
39
- return obj
40
- }
41
-
42
- metadataMap , ok := metadata .(map [string ]interface {})
43
- if ! ok {
44
- return obj
36
+ // Process metadata fields
37
+ if metadata := objMap ["metadata" ]; metadata != nil {
38
+ objMap ["metadata" ] = processMetadataToArrays (metadata )
45
39
}
46
40
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
- }
41
+ // Process spec fields
42
+ if spec := objMap ["spec" ]; spec != nil {
43
+ objMap ["spec" ] = processSpecToArrays (spec )
52
44
}
53
45
54
46
return obj
55
47
}
56
48
57
- // convertArraysToMaps transforms labels and annotations from arrays to maps (for Kubernetes input)
49
+ // convertArraysToMaps transforms label-like fields from arrays to maps (for Kubernetes input)
58
50
// []Label → map[string]string
59
51
func convertArraysToMaps (obj any ) any {
60
52
objMap , ok := obj .(map [string ]interface {})
61
53
if ! ok {
62
54
return obj
63
55
}
64
56
65
- // Check if this object has metadata
66
- metadata , hasMetadata := objMap ["metadata" ]
67
- if ! hasMetadata {
68
- return obj
57
+ // Process metadata fields
58
+ if metadata := objMap ["metadata" ]; metadata != nil {
59
+ objMap ["metadata" ] = processMetadataToMaps (metadata )
60
+ }
61
+
62
+ // Process spec fields
63
+ if spec := objMap ["spec" ]; spec != nil {
64
+ objMap ["spec" ] = processSpecToMaps (spec )
65
+ }
66
+
67
+ return obj
68
+ }
69
+
70
+ // processMetadataToArrays handles metadata.labels and metadata.annotations conversion to arrays
71
+ func processMetadataToArrays (metadata any ) any {
72
+ metadataMap , ok := metadata .(map [string ]interface {})
73
+ if ! ok {
74
+ return metadata
75
+ }
76
+
77
+ if labels := metadataMap ["labels" ]; labels != nil {
78
+ metadataMap ["labels" ] = mapToArray (labels )
79
+ }
80
+ if annotations := metadataMap ["annotations" ]; annotations != nil {
81
+ metadataMap ["annotations" ] = mapToArray (annotations )
69
82
}
70
83
84
+ return metadataMap
85
+ }
86
+
87
+ // processMetadataToMaps handles metadata.labels and metadata.annotations conversion to maps
88
+ func processMetadataToMaps (metadata any ) any {
71
89
metadataMap , ok := metadata .(map [string ]interface {})
72
90
if ! ok {
73
- return obj
91
+ return metadata
92
+ }
93
+
94
+ if labels := metadataMap ["labels" ]; labels != nil {
95
+ metadataMap ["labels" ] = arrayToMap (labels )
96
+ }
97
+ if annotations := metadataMap ["annotations" ]; annotations != nil {
98
+ metadataMap ["annotations" ] = arrayToMap (annotations )
99
+ }
100
+
101
+ return metadataMap
102
+ }
103
+
104
+ // processSpecToArrays handles spec.nodeSelector and spec.selector.matchLabels conversion to arrays
105
+ func processSpecToArrays (spec any ) any {
106
+ specMap , ok := spec .(map [string ]interface {})
107
+ if ! ok {
108
+ return spec
109
+ }
110
+
111
+ if nodeSelector := specMap ["nodeSelector" ]; nodeSelector != nil {
112
+ specMap ["nodeSelector" ] = mapToArray (nodeSelector )
113
+ }
114
+ if selector := specMap ["selector" ]; selector != nil {
115
+ specMap ["selector" ] = processSelector (selector , true )
116
+ }
117
+
118
+ return specMap
119
+ }
120
+
121
+ // processSpecToMaps handles spec.nodeSelector and spec.selector.matchLabels conversion to maps
122
+ func processSpecToMaps (spec any ) any {
123
+ specMap , ok := spec .(map [string ]interface {})
124
+ if ! ok {
125
+ return spec
126
+ }
127
+
128
+ if nodeSelector := specMap ["nodeSelector" ]; nodeSelector != nil {
129
+ specMap ["nodeSelector" ] = arrayToMap (nodeSelector )
130
+ }
131
+ if selector := specMap ["selector" ]; selector != nil {
132
+ specMap ["selector" ] = processSelector (selector , false )
74
133
}
75
134
76
- // Transform labels and annotations to maps (in-place)
77
- for k , v := range metadataMap {
78
- if (k == "labels" || k == "annotations" ) && v != nil {
79
- metadataMap [k ] = arrayToMap (v )
135
+ return specMap
136
+ }
137
+
138
+ // processSelector handles selector.matchLabels conversion
139
+ func processSelector (selector any , toArray bool ) any {
140
+ selectorMap , ok := selector .(map [string ]interface {})
141
+ if ! ok {
142
+ return selector
143
+ }
144
+
145
+ if matchLabels := selectorMap ["matchLabels" ]; matchLabels != nil {
146
+ if toArray {
147
+ selectorMap ["matchLabels" ] = mapToArray (matchLabels )
148
+ } else {
149
+ selectorMap ["matchLabels" ] = arrayToMap (matchLabels )
80
150
}
81
151
}
82
152
83
- return obj
153
+ return selectorMap
84
154
}
85
155
86
156
// mapToArray converts a label map to array format
0 commit comments