Skip to content

Commit

Permalink
Make setNestedFieldKeepFormatting private
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Apr 28, 2023
1 parent 2117e80 commit 4f8a7b4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions krm-functions/lib/kubeobject/kubeobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func NewFromGoStruct[T1 any](x any) (*KubeObjectExt[T1], error) {
// SetSpec sets the `spec` field of a KubeObjectExt to the value of `newSpec`,
// while trying to keep as much formatting as possible
func (o *KubeObjectExt[T1]) SetSpec(newSpec interface{}) error {
return SetNestedFieldKeepFormatting(&o.KubeObject.SubObject, newSpec, "spec")
return setNestedFieldKeepFormatting(&o.KubeObject.SubObject, newSpec, "spec")
}

// SetStatus sets the `status` field of a KubeObjectExt to the value of `newStatus`,
// while trying to keep as much formatting as possible
func (o *KubeObjectExt[T1]) SetStatus(newStatus interface{}) error {
return SetNestedFieldKeepFormatting(&o.KubeObject.SubObject, newStatus, "status")
return setNestedFieldKeepFormatting(&o.KubeObject.SubObject, newStatus, "status")
}

// NOTE: the following functions are considered as "methods" of KubeObject,
Expand Down Expand Up @@ -108,13 +108,13 @@ func GetStatus[T any](obj *fn.KubeObject) (T, error) {

}

// SetNestedFieldKeepFormatting is similar to KubeObject.SetNestedField(), but keeps the
// setNestedFieldKeepFormatting is similar to KubeObject.SetNestedField(), but keeps the
// comments and the order of fields in the YAML wherever it is possible.
//
// NOTE: This functionality should be solved in the upstream SDK.
// Merging the code below to the upstream SDK is in progress and tracked in this issue:
// https://github.com/GoogleContainerTools/kpt/issues/3923
func SetNestedFieldKeepFormatting(obj *fn.SubObject, value interface{}, field string) error {
func setNestedFieldKeepFormatting(obj *fn.SubObject, value interface{}, field string) error {
oldNode := yamlNodeOf(obj.UpsertMap(field))
err := obj.SetNestedField(value, field)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion krm-functions/lib/kubeobject/kubeobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func TestSetNestedFieldKeepFormatting(t *testing.T) {
deploy.Spec.Replicas = nil // delete Replicas field if present
deploy.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyOnFailure // update field value

err = SetNestedFieldKeepFormatting(&obj.SubObject, deploy.Spec, "spec")
err = setNestedFieldKeepFormatting(&obj.SubObject, deploy.Spec, "spec")
if err != nil {
t.Errorf("unexpected error in SetNestedFieldKeepFormatting: %v", err)
}
Expand Down

0 comments on commit 4f8a7b4

Please sign in to comment.