Skip to content
Snippets Groups Projects
Commit ea191ad7 authored by Jessica Yuen's avatar Jessica Yuen
Browse files

Support reading of multi-line jsonnet params


Signed-off-by: default avatarJessica Yuen <im.jessicayuen@gmail.com>
parent e005970c
No related branches found
No related tags found
No related merge requests found
......@@ -152,6 +152,8 @@ func visitParamValue(param ast.Node) (string, error) {
switch n.Kind {
case ast.StringSingle, ast.StringDouble:
return fmt.Sprintf(`"%s"`, n.Value), nil
case ast.StringBlock:
return fmt.Sprintf("|||\n%s|||", n.Value), nil
default:
return "", fmt.Errorf("Found unsupported LiteralString type %T", n)
}
......@@ -176,7 +178,21 @@ func writeParams(indent int, params Params) string {
var buffer bytes.Buffer
buffer.WriteString("\n")
for i, key := range keys {
buffer.WriteString(fmt.Sprintf("%s%s: %s,", indentBuffer.String(), key, params[key]))
param := params[key]
if strings.HasPrefix(param, "|||\n") {
println(param)
// every line in a block string needs to be indented
lines := strings.Split(param, "\n")
buffer.WriteString(fmt.Sprintf("%s%s: %s\n", indentBuffer.String(), key, lines[0]))
for i := 1; i < len(lines)-1; i++ {
buffer.WriteString(fmt.Sprintf(" %s%s\n", indentBuffer.String(), lines[i]))
}
buffer.WriteString(fmt.Sprintf("%s|||,", indentBuffer.String()))
} else {
// other param types
buffer.WriteString(fmt.Sprintf("%s%s: %s,", indentBuffer.String(), key, param))
}
if i < len(keys)-1 {
buffer.WriteString("\n")
}
......
......@@ -296,6 +296,23 @@ func TestGetComponentParams(t *testing.T) {
}`,
Params{"name": `"foo-bar"`, "replicas": "1"},
},
// Test case where one of the param values is a block string
{
"foo",
`
{
components: {
"foo": {
name: |||
name
is
foo
|||,
}
},
}`,
Params{"name": "|||\nname\nis\nfoo\n|||"},
},
}
errors := []struct {
......@@ -336,18 +353,6 @@ func TestGetComponentParams(t *testing.T) {
name: "foo",
}
},
}`,
},
// Test case where one of the param values is a block string
{
"foo",
`
{
components: {
"foo": {
name: |||name is foo|||,
}
},
}`,
},
}
......@@ -566,6 +571,36 @@ local bar = import "bar";
replicas: 5,
},
},
}`,
},
// Test setting parameter for jsonnet file with multi-string field
{
"foo-bar",
`
{
components: {
"foo-bar": {
description: |||
foo
bar
|||,
name: "foo-bar",
},
},
}`,
Params{"replicas": "5"},
`
{
components: {
"foo-bar": {
description: |||
foo
bar
|||,
name: "foo-bar",
replicas: 5,
},
},
}`,
},
}
......@@ -755,6 +790,9 @@ params + {
replicas: 1,
},
foo +: {
description: |||
foo
|||,
name: "foo",
replicas: 1,
},
......@@ -770,6 +808,9 @@ params + {
replicas: 1,
},
foo +: {
description: |||
foo
|||,
name: "foobar",
replicas: 5,
},
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment