@@ -37,67 +37,79 @@ type Handler struct {
3737 expand bool
3838}
3939
40+ // NewHandler creates the basic context to expand or collapse references, such
41+ // context requires the Kubernetes object being translated and its dependencies
4042func NewHandler (main client.Object , deps []client.Object ) * Handler {
4143 return & Handler {context : newMapContext (main , deps )}
4244}
4345
44- func (h * Handler ) ExpandReferences (obj , mappings map [string ]any , fields ... string ) error {
46+ // ExpandReferences uses the handler context to expand references on a given
47+ // unstructured value matching the main object being translated, using the
48+ // given reference mappings and acting on a particular path og the value object
49+ func (h * Handler ) ExpandReferences (obj , mappings map [string ]any , path ... string ) error {
4550 h .expand = true
4651
47- props , err := accessMappingPropsAt (mappings , fields )
52+ props , err := accessMappingPropsAt (mappings , path )
4853 if errors .Is (err , unstructured .ErrNotFound ) {
4954 return nil
5055 } else if err != nil {
5156 return fmt .Errorf ("failed to access mappings to expand references: %w" , err )
5257 }
5358
54- field , err := unstructured .AccessField [map [string ]any ](obj , fields ... )
59+ field , err := unstructured .GetField [map [string ]any ](obj , path ... )
5560 if err != nil {
56- return fmt .Errorf ("failed to access object's %v: %w" , fields , err )
61+ return fmt .Errorf ("failed to access object's %v: %w" , path , err )
5762 }
5863 if err := h .scanProperties ([]string {}, props , field ); err != nil {
59- return fmt .Errorf ("failed to expand references at %v: %w" , fields , err )
64+ return fmt .Errorf ("failed to expand references at %v: %w" , path , err )
6065 }
6166 return nil
6267}
6368
64- func (h * Handler ) CollapseReferences (obj , mappings map [string ]any , fields ... string ) error {
69+ // CollapseReferences uses the handler context to collapse references on a given
70+ // unstructured value matching the main object being translated, using the
71+ // given reference mappings and acting on a particular path of the value object
72+ func (h * Handler ) CollapseReferences (obj , mappings map [string ]any , path ... string ) error {
6573 h .expand = false
6674
67- props , err := accessMappingPropsAt (mappings , fields )
75+ props , err := accessMappingPropsAt (mappings , path )
6876 if errors .Is (err , unstructured .ErrNotFound ) {
6977 return nil
7078 } else if err != nil {
7179 return fmt .Errorf ("failed to access mappings to collapse references: %w" , err )
7280 }
7381
74- field , err := unstructured .AccessField [map [string ]any ](obj , fields ... )
82+ field , err := unstructured .GetField [map [string ]any ](obj , path ... )
7583 if err != nil {
76- return fmt .Errorf ("failed to access object's %v: %w" , fields , err )
84+ return fmt .Errorf ("failed to access object's %v: %w" , path , err )
7785 }
7886
7987 if err := h .scanProperties ([]string {}, props , field ); err != nil {
80- return fmt .Errorf ("failed to collapse references at %v: %w" , fields , err )
88+ return fmt .Errorf ("failed to collapse references at %v: %w" , path , err )
8189 }
8290 return nil
8391}
8492
93+ // Added returns any kubernetes objects created as references by ExpandReferences
8594func (h * Handler ) Added () []client.Object {
8695 return h .added
8796}
8897
89- func accessMappingPropsAt (mappings map [string ]any , fields []string ) (map [string ]any , error ) {
98+ // accessMappingPropsAt reads the mappings object at a given path
99+ func accessMappingPropsAt (mappings map [string ]any , path []string ) (map [string ]any , error ) {
90100 expandedPath := []string {"properties" }
91- for _ , field := range fields {
101+ for _ , field := range path {
92102 expandedPath = append (expandedPath , field , "properties" )
93103 }
94- props , err := unstructured .AccessField [map [string ]any ](mappings , expandedPath ... )
104+ props , err := unstructured .GetField [map [string ]any ](mappings , expandedPath ... )
95105 if err != nil {
96106 return nil , fmt .Errorf ("failed to access the API mapping properties for %v: %w" , expandedPath , err )
97107 }
98108 return props , nil
99109}
100110
111+ // scanProperties checks an object value path position holding field properties
112+ // against reference mappings that may apply at that path
101113func (m * Handler ) scanProperties (path []string , props , obj map [string ]any ) error {
102114 for key , prop := range props {
103115 mapping , ok := (prop ).(map [string ]any )
@@ -111,7 +123,7 @@ func (m *Handler) scanProperties(path []string, props, obj map[string]any) error
111123 }
112124 continue
113125 }
114- rawField , err := unstructured .AccessField [any ](obj , key )
126+ rawField , err := unstructured .GetField [any ](obj , key )
115127 if errors .Is (err , unstructured .ErrNotFound ) {
116128 continue
117129 }
@@ -135,8 +147,10 @@ func (m *Handler) scanProperties(path []string, props, obj map[string]any) error
135147 return nil
136148}
137149
150+ // scanArray checks an object value path position holding an array against
151+ // reference mappings that may apply at that path
138152func (m * Handler ) scanArray (path []string , mapping map [string ]any , list []any ) error {
139- mapItems , err := unstructured .AccessField [map [string ]any ](mapping , "items" , "properties" )
153+ mapItems , err := unstructured .GetField [map [string ]any ](mapping , "items" , "properties" )
140154 if err != nil {
141155 return fmt .Errorf ("failed to access %q: %w" , unstructured .Base (path ), err )
142156 }
@@ -160,9 +174,11 @@ func (m *Handler) scanArray(path []string, mapping map[string]any, list []any) e
160174 return nil
161175}
162176
177+ // scanObject checks an object value path position holding an object against
178+ // reference mappings that may apply at that path
163179func (m * Handler ) scanObject (path []string , mapName string , mapping , obj map [string ]any ) error {
164180 if mapping ["properties" ] != nil {
165- props , err := unstructured .AccessField [map [string ]any ](mapping , "properties" )
181+ props , err := unstructured .GetField [map [string ]any ](mapping , "properties" )
166182 if err != nil {
167183 return fmt .Errorf ("failed to access properties at %q: %w" , path , err )
168184 }
@@ -174,6 +190,7 @@ func (m *Handler) scanObject(path []string, mapName string, mapping, obj map[str
174190 return fmt .Errorf ("unsupported extension at %v with fields %v" , path , unstructured .FieldsOf (mapping ))
175191}
176192
193+ // processReference kicks of a reference expansion or collapse
177194func (m * Handler ) processReference (path []string , mappingName string , mapping , obj map [string ]any ) error {
178195 rm := refMapping {}
179196 if err := unstructured .FromUnstructured (& rm , mapping ); err != nil {
@@ -186,6 +203,8 @@ func (m *Handler) processReference(path []string, mappingName string, mapping, o
186203 return ref .Collapse (m .context , path , obj )
187204}
188205
206+ // entryMatchingMapping returns the entry name key and value from an array that
207+ // matches the reference ebing collapsed or expanded
189208func entryMatchingMapping (mapName string , mapping map [string ]any , list []any , expand bool ) (string , map [string ]any , error ) {
190209 key := mapName
191210 if expand {
@@ -200,6 +219,7 @@ func entryMatchingMapping(mapName string, mapping map[string]any, list []any, ex
200219 return key , m , err
201220}
202221
222+ // findByExistingUniqueKey returns the value of an array holding a given key
203223func findByExistingUniqueKey (list []any , key string ) (map [string ]any , error ) {
204224 candidates := []map [string ]any {}
205225 for _ , item := range list {
0 commit comments