Skip to content

Commit 3255acc

Browse files
authored
fix NestedSubObject (#676)
1 parent e8e9cb3 commit 3255acc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

go/fn/object.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (o *SubObject) NestedSlice(fields ...string) (SliceSubObjects, bool, error)
151151
return val, true, nil
152152
}
153153

154-
// NestedMap returns a map[string]string value of a nested field, false if not found and an error if not a map[string]string type.
154+
// NestedSubObject returns with a SubObject representing the YAML subtree under the path specified by `fields“
155155
func (o *SubObject) NestedSubObject(fields ...string) (SubObject, bool, error) {
156156
var variant SubObject
157157
m, found, err := o.obj.GetNestedMap(fields...)
@@ -161,8 +161,13 @@ func (o *SubObject) NestedSubObject(fields ...string) (SubObject, bool, error) {
161161
if !found {
162162
return variant, found, nil
163163
}
164-
err = m.Node().Decode(variant)
165-
return variant, true, err
164+
165+
var rn yaml.RNode
166+
rn.SetYNode(m.Node())
167+
variant.obj = internal.NewMap(rn.YNode())
168+
variant.parentGVK = o.parentGVK
169+
variant.fieldpath = o.fieldpath + "." + strings.Join(fields, ".")
170+
return variant, true, nil
166171
}
167172

168173
// NestedMap returns a map[string]string value of a nested field, false if not found and an error if not a map[string]string type.

go/fn/object_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,11 @@ func TestGetNestedFields(t *testing.T) {
476476
if intVal := spec.GetInt("replicas"); intVal != 3 {
477477
t.Errorf("deployment .spec.replicas expected to be 3, got %v", intVal)
478478
}
479+
480+
strategy, _, _ := deployment.NestedSubObject("spec", "strategy")
481+
if stringVal := strategy.GetString("type"); stringVal != "Recreate" {
482+
t.Errorf("deployment .spec.strategy.type expected to be `Recreate`, got %v", stringVal)
483+
}
479484
if boolVal := spec.GetBool("paused"); boolVal != true {
480485
t.Errorf("deployment .spec.paused expected to be true, got %v", boolVal)
481486
}

0 commit comments

Comments
 (0)