@@ -72,7 +72,8 @@ func CheckResourceDuplication(rl *ResourceList) error {
7272 return nil
7373}
7474
75- // ParseResourceList parses a ResourceList from the input byte array.
75+ // ParseResourceList parses a ResourceList from the input byte array. This function can be used to parse either KRM fn input
76+ // or KRM fn output
7677func ParseResourceList (in []byte ) (* ResourceList , error ) {
7778 rl := & ResourceList {}
7879 rlObj , err := ParseKubeObject (in )
@@ -82,6 +83,7 @@ func ParseResourceList(in []byte) (*ResourceList, error) {
8283 if rlObj .GetKind () != kio .ResourceListKind {
8384 return nil , fmt .Errorf ("input was of unexpected kind %q; expected ResourceList" , rlObj .GetKind ())
8485 }
86+ // Parse FunctionConfig. FunctionConfig can be empty, e.g. `kubeval` fn does not require a FunctionConfig.
8587 fc , found , err := rlObj .obj .GetNestedMap ("functionConfig" )
8688 if err != nil {
8789 return nil , fmt .Errorf ("failed when tried to get functionConfig: %w" , err )
@@ -92,21 +94,22 @@ func ParseResourceList(in []byte) (*ResourceList, error) {
9294 rl .FunctionConfig = NewEmptyKubeObject ()
9395 }
9496
97+ // Parse Items. Items can be empty, e.g. an input ResourceList for a generator function may not have items.
9598 items , found , err := rlObj .obj .GetNestedSlice ("items" )
9699 if err != nil {
97100 return nil , fmt .Errorf ("failed when tried to get items: %w" , err )
98101 }
99- if ! found {
100- return rl , nil
101- }
102- objectItems , err := items .Elements ()
103- if err != nil {
104- return nil , fmt .Errorf ("failed to extract objects from items: %w" , err )
105- }
106- for i := range objectItems {
107- rl .Items = append (rl .Items , asKubeObject (objectItems [i ]))
102+ if found {
103+ objectItems , err := items .Elements ()
104+ if err != nil {
105+ return nil , fmt .Errorf ("failed to extract objects from items: %w" , err )
106+ }
107+ for i := range objectItems {
108+ rl .Items = append (rl .Items , asKubeObject (objectItems [i ]))
109+ }
108110 }
109111
112+ // Parse Results. Results can be empty.
110113 res , found , err := rlObj .obj .GetNestedSlice ("results" )
111114 if err != nil {
112115 return nil , fmt .Errorf ("failed when tried to get results: %w" , err )
@@ -119,7 +122,6 @@ func ParseResourceList(in []byte) (*ResourceList, error) {
119122 }
120123 rl .Results = results
121124 }
122-
123125 return rl , nil
124126}
125127
0 commit comments