Skip to content

Commit

Permalink
do not marshal the value if it is a primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Apr 25, 2024
1 parent 9b2de4e commit 527d458
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
36 changes: 18 additions & 18 deletions pkg/utils/subst/subst.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package subst

import (
"reflect"

"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
Expand Down Expand Up @@ -121,25 +123,23 @@ func (f *fileinfo) SubstituteByValue(path string, value interface{}) error {
err error
data []byte
)
if f.json {
data, err = runtime.DefaultJSONEncoding.Marshal(value)
} else {
data, err = runtime.DefaultYAMLEncoding.Marshal(value)
}
if err != nil {
return err
}
return f.substituteByData(path, data)
/*
node, err := yaml.ValueToNode(value)
if err != nil {
return errors.Wrapf(err, "cannot unmarshal value")
}

p, err := yaml.PathString("$." + path)
// Do not marshal the value if it's a primitive type.
switch reflect.ValueOf(value).Kind() {
case reflect.String:
data = []byte(value.(string))
case reflect.Slice:
data = value.([]byte)
default:
if f.json {
data, err = runtime.DefaultJSONEncoding.Marshal(value)
} else {
data, err = runtime.DefaultYAMLEncoding.Marshal(value)
}
if err != nil {
return errors.Wrapf(err, "invalid substitution path")
return err
}
return p.ReplaceWithNode(f.content, node)
*/
}

return f.substituteByData(path, data)
}
55 changes: 23 additions & 32 deletions pkg/utils/subst/subst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package subst

import (
"fmt"
"strings"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -49,34 +48,33 @@ data:
content, err := Parse([]byte(data))
Expect(err).To(Succeed())

Expect(content.SubstituteByValue("data.value1", `-----BEGIN CERTIFICATE-----
MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRG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PMWLSn/pvtsrF9+wX3N3KjITOYFnQoQj8kVnNeyIv/iPsGEMNKSuIEyExtv4
NeF22d+mQrvHRAiGfzZ0JFrabA0UWTW98kndth/Jsw1HKj2ZL7tcu7XUIOGZX1NG
Fdtom/DzMNU+MeKNhJ7jitralj41E6Vf8PlwUHBHQRFXGU7Aj64GxJUTFy8bJZ91
8rGOmaFvE7FBcf6IKshPECBV1/MUReXgRPTqh5Uykw7+U0b6LJ3/iyK5S9kJRaTe
pLiaWN0bfVKfjllDiIGknibVb63dDcY3fe0Dkhvld1927jyNxF1WW6LZZm6zNTfl
MrY=
-----END CERTIFICATE-----`)).To(Succeed())

result, err := content.Content()
Expect(err).To(Succeed())

fmt.Println("========= block result ===========")
fmt.Println(string(result))
expected := `
data:
value1: |
Expand Down Expand Up @@ -104,9 +102,6 @@ data:
-----END CERTIFICATE-----
value2: orig2
`
fmt.Println("========= block expected ===========")
fmt.Println(expected)

Expect(strings.Trim(string(result), "\n")).To(Equal(strings.Trim(expected, "\n")))
})

Expand All @@ -124,7 +119,6 @@ data:
result, err := content.Content()
Expect(err).To(Succeed())

fmt.Printf("\n%s\n", string(result))
Expect(strings.Trim(string(result), "\n")).To(Equal(strings.Trim(`
data:
value1:
Expand Down Expand Up @@ -175,7 +169,6 @@ data:
result, err := content.Content()
Expect(err).To(Succeed())

fmt.Printf("\n%s\n", string(result))
Expect(strings.Trim(string(result), "\n")).To(Equal(strings.Trim(`
data:
value1:
Expand Down Expand Up @@ -206,7 +199,6 @@ value2: ""
result, err := content.Content()
Expect(err).To(Succeed())

fmt.Printf("\n%s\n", string(result))
Expect(strings.Trim(string(result), "\n")).To(Equal(strings.Trim(`
{"data": {"value1": {"value1": "v1", "value2": ""}, "value2": "orig2"}}
`, "\n")))
Expand Down Expand Up @@ -268,7 +260,6 @@ data: {
result, err := content.Content()
Expect(err).To(Succeed())

fmt.Printf("\n%s\n", string(result))
Expect(strings.Trim(string(result), "\n")).To(Equal(strings.Trim(`
data:
value1:
Expand Down

0 comments on commit 527d458

Please sign in to comment.