Skip to content

Commit

Permalink
fix NestedSubObject (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma authored Apr 27, 2023
1 parent e8e9cb3 commit 3255acc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions go/fn/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (o *SubObject) NestedSlice(fields ...string) (SliceSubObjects, bool, error)
return val, true, nil
}

// 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.
// NestedSubObject returns with a SubObject representing the YAML subtree under the path specified by `fields“
func (o *SubObject) NestedSubObject(fields ...string) (SubObject, bool, error) {
var variant SubObject
m, found, err := o.obj.GetNestedMap(fields...)
Expand All @@ -161,8 +161,13 @@ func (o *SubObject) NestedSubObject(fields ...string) (SubObject, bool, error) {
if !found {
return variant, found, nil
}
err = m.Node().Decode(variant)
return variant, true, err

var rn yaml.RNode
rn.SetYNode(m.Node())
variant.obj = internal.NewMap(rn.YNode())
variant.parentGVK = o.parentGVK
variant.fieldpath = o.fieldpath + "." + strings.Join(fields, ".")
return variant, true, nil
}

// 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.
Expand Down
5 changes: 5 additions & 0 deletions go/fn/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ func TestGetNestedFields(t *testing.T) {
if intVal := spec.GetInt("replicas"); intVal != 3 {
t.Errorf("deployment .spec.replicas expected to be 3, got %v", intVal)
}

strategy, _, _ := deployment.NestedSubObject("spec", "strategy")
if stringVal := strategy.GetString("type"); stringVal != "Recreate" {
t.Errorf("deployment .spec.strategy.type expected to be `Recreate`, got %v", stringVal)
}
if boolVal := spec.GetBool("paused"); boolVal != true {
t.Errorf("deployment .spec.paused expected to be true, got %v", boolVal)
}
Expand Down

0 comments on commit 3255acc

Please sign in to comment.