From f416878d8362aa9b8af7fcdb5dce66bdf74992e3 Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:05:28 -0400 Subject: [PATCH] chore: avoid unnecessary json marshal Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- pkg/diff/diff.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index 2278222c3..0a672d7f6 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -673,22 +673,6 @@ func ThreeWayDiff(orig, config, live *unstructured.Unstructured) (*DiffResult, e return buildDiffResult(predictedLiveBytes, liveBytes), nil } -// stripTypeInformation strips any type information (e.g. float64 vs. int) from the unstructured -// object by remarshalling the object. This is important for diffing since it will cause godiff -// to report a false difference. -func stripTypeInformation(un *unstructured.Unstructured) *unstructured.Unstructured { - unBytes, err := json.Marshal(un) - if err != nil { - panic(err) - } - var newUn unstructured.Unstructured - err = json.Unmarshal(unBytes, &newUn) - if err != nil { - panic(err) - } - return &newUn -} - // removeNamespaceAnnotation remove the namespace and an empty annotation map from the metadata. // The namespace field is present in live (namespaced) objects, but not necessarily present in // config or last-applied. This results in a diff which we don't care about. We delete the two so @@ -1087,11 +1071,20 @@ func toString(val interface{}) string { // Remarshalling also strips any type information (e.g. float64 vs. int) from the unstructured // object. This is important for diffing since it will cause godiff to report a false difference. func remarshal(obj *unstructured.Unstructured, o options) *unstructured.Unstructured { - obj = stripTypeInformation(obj) data, err := json.Marshal(obj) if err != nil { panic(err) } + + // Unmarshal again to strip type information (e.g. float64 vs. int) from the unstructured + // object. This is important for diffing since it will cause godiff to report a false difference. + var newUn unstructured.Unstructured + err = json.Unmarshal(data, &newUn) + if err != nil { + panic(err) + } + obj = &newUn + gvk := obj.GroupVersionKind() item, err := scheme.Scheme.New(obj.GroupVersionKind()) if err != nil {