Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 43902b6

Browse files
alexcodelfgitlawr
authored andcommitted
fix: replace variables error
1 parent 700d971 commit 43902b6

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

pkg/apis/resource/helper.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ func replaceAttributes(
6161
outputValues map[string]property.Value,
6262
) (property.Values, error) {
6363
for n, v := range varValues {
64-
escapedValue := strconv.Quote(string(v))
65-
// Remove quotes.
66-
escapedValueWithoutQuotes := escapedValue[1 : len(escapedValue)-1]
67-
attrByte = bytes.ReplaceAll(attrByte, []byte(n), []byte(escapedValueWithoutQuotes))
64+
escapedValue := escapeString(string(v))
65+
attrByte = bytes.ReplaceAll(attrByte, []byte(n), []byte(escapedValue))
6866
}
6967

7068
for n, v := range outputValues {
71-
escapedValue := strconv.Quote(string(v))
72-
// Remove quotes.
73-
escapedValueWithoutQuotes := escapedValue[1 : len(escapedValue)-1]
74-
attrByte = bytes.ReplaceAll(attrByte, []byte(n), []byte(escapedValueWithoutQuotes))
69+
escapedValue := escapeString(string(v))
70+
attrByte = bytes.ReplaceAll(attrByte, []byte(n), []byte(escapedValue))
7571
}
7672

7773
var injectAttrs property.Values
@@ -235,3 +231,11 @@ func toResource(
235231

236232
return r
237233
}
234+
235+
// escapeString escapes the string value.
236+
// It will escape the string value to be a valid JSON string.
237+
func escapeString(v string) string {
238+
escapedValue := strconv.Quote(v)
239+
240+
return fmt.Sprintf(`"%s"`, strings.TrimSuffix(strings.TrimPrefix(escapedValue, `"\"`), `\""`))
241+
}

pkg/apis/resource/helper_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestInjectAttributes(t *testing.T) {
2929
name: "interpolation with variable",
3030
attrByte: []byte(`{"a": "${var.b}"}`),
3131
varValues: map[string]json.RawMessage{
32-
"${var.b}": json.RawMessage(`c`),
32+
`"${var.b}"`: json.RawMessage(`"c"`),
3333
},
3434
expected: property.Values{
3535
"a": json.RawMessage(`"c"`),
@@ -39,7 +39,7 @@ func TestInjectAttributes(t *testing.T) {
3939
name: "interpolation with output",
4040
attrByte: []byte(`{"a": "${res.b.c}"}`),
4141
outputValues: map[string]property.Value{
42-
"${res.b.c}": property.Value(`d`),
42+
`"${res.b.c}"`: property.Value(`"d"`),
4343
},
4444
expected: property.Values{
4545
"a": json.RawMessage(`"d"`),
@@ -49,14 +49,24 @@ func TestInjectAttributes(t *testing.T) {
4949
name: "interpolation with variable have newline",
5050
attrByte: []byte(`{"a": "${var.b}"}`),
5151
varValues: map[string]json.RawMessage{
52-
"${var.b}": json.RawMessage(`-----BEGIN RSA PRIVATE KEY-----
52+
`"${var.b}"`: json.RawMessage(`"-----BEGIN RSA PRIVATE KEY-----
5353
xxx
54-
-----END RSA PRIVATE KEY-----`),
54+
-----END RSA PRIVATE KEY-----"`),
5555
},
5656
expected: property.Values{
5757
"a": json.RawMessage(`"-----BEGIN RSA PRIVATE KEY-----\nxxx\n-----END RSA PRIVATE KEY-----"`),
5858
},
5959
},
60+
{
61+
name: "interpolation with empty variable",
62+
attrByte: []byte(`{"a": "${var.b}"}`),
63+
varValues: map[string]json.RawMessage{
64+
`"${var.b}"`: json.RawMessage(`""`),
65+
},
66+
expected: property.Values{
67+
"a": json.RawMessage(`""`),
68+
},
69+
},
6070
}
6171

6272
for _, c := range cases {

0 commit comments

Comments
 (0)