diff --git a/pkg/utils/subst/subst.go b/pkg/utils/subst/subst.go index b89e7e9685..5ef68e423e 100644 --- a/pkg/utils/subst/subst.go +++ b/pkg/utils/subst/subst.go @@ -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" @@ -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) } diff --git a/pkg/utils/subst/subst_test.go b/pkg/utils/subst/subst_test.go index ba9fa09c7c..f2f7db0e17 100644 --- a/pkg/utils/subst/subst_test.go +++ b/pkg/utils/subst/subst_test.go @@ -5,7 +5,6 @@ package subst import ( - "fmt" "strings" . "github.com/onsi/ginkgo/v2" @@ -49,34 +48,33 @@ data: content, err := Parse([]byte(data)) Expect(err).To(Succeed()) - Expect(content.SubstituteByValue("data.value1", `-----BEGIN CERTIFICATE----- -MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh -MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 -d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ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NTfl -MrY= ------END CERTIFICATE-----`)).To(Succeed()) + Expect(content.SubstituteByValue("data.value1", `| + -----BEGIN CERTIFICATE----- + MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh + MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 + d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH + MjAeFw0xMzA4MDExMjAwMDBaFw0zODAxMTUxMjAwMDBaMGExCzAJBgNVBAYTAlVT + MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j + b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IEcyMIIBIjANBgkqhkiG + 9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuzfNNNx7a8myaJCtSnX/RrohCgiN9RlUyfuI + 2/Ou8jqJkTx65qsGGmvPrC3oXgkkRLpimn7Wo6h+4FR1IAWsULecYxpsMNzaHxmx + 1x7e/dfgy5SDN67sH0NO3Xss0r0upS/kqbitOtSZpLYl6ZtrAGCSYP9PIUkY92eQ + q2EGnI/yuum06ZIya7XzV+hdG82MHauVBJVJ8zUtluNJbd134/tJS7SsVQepj5Wz + tCO7TG1F8PapspUwtP1MVYwnSlcUfIKdzXOS0xZKBgyMUNGPHgm+F6HmIcr9g+UQ + vIOlCsRnKPZzFBQ9RnbDhxSJITRNrw9FDKZJobq7nMWxM4MphQIDAQABo0IwQDAP + BgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUTiJUIBiV + 5uNu5g/6+rkS7QYXjzkwDQYJKoZIhvcNAQELBQADggEBAGBnKJRvDkhj6zHd6mcY + 1Yl9PMWLSn/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: | @@ -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"))) }) @@ -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: @@ -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: @@ -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"))) @@ -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: