Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 0e8984f

Browse files
committed
Adding support for - as well and some more testing around interpolation.
Signed-off-by: GodFather <[email protected]>
1 parent 929680e commit 0e8984f

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

config/interpolation.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func isNum(c uint8) bool {
1515
}
1616

1717
func validVariableDefault(c uint8, line string, pos int) bool {
18-
return c == ':' && line[pos+1] == '-'
18+
return (c == ':' && line[pos+1] == '-') || (c == '-')
1919
}
2020

2121
func validVariableNameChar(c uint8) bool {
@@ -45,6 +45,14 @@ func parseVariable(line string, pos int, mapping func(string) string) (string, i
4545
func parseDefaultValue(line string, pos int) (string, int, bool) {
4646
var buffer bytes.Buffer
4747

48+
// only skip :, :- and - at the beginning
49+
for ; pos < len(line); pos++ {
50+
c := line[pos]
51+
if c == ':' || c == '-' {
52+
continue
53+
}
54+
break
55+
}
4856
for ; pos < len(line); pos++ {
4957
c := line[pos]
5058
if c == '}' {
@@ -76,7 +84,7 @@ func parseVariableWithBraces(line string, pos int, mapping func(string) string)
7684
buffer.WriteByte(c)
7785
case validVariableDefault(c, line, pos):
7886
defaultValue := ""
79-
defaultValue, pos, _ = parseDefaultValue(line, pos+2)
87+
defaultValue, pos, _ = parseDefaultValue(line, pos)
8088
defaultValues[buffer.String()] = defaultValue
8189
default:
8290
return "", 0, false

config/interpolation_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -25,6 +26,14 @@ func testInvalidInterpolatedLine(t *testing.T, line string) {
2526
assert.Equal(t, false, success)
2627
}
2728

29+
func testInterpolatedDefault(t *testing.T, line string, delim string, expectedVar string, expectedVal string) {
30+
envVar, _ := parseLine(line, func(env string) string { return env })
31+
pos := strings.Index(line, delim)
32+
envDefault, _, _ := parseDefaultValue(line, pos)
33+
assert.Equal(t, expectedVal, envDefault)
34+
assert.Equal(t, expectedVar, envVar)
35+
}
36+
2837
func TestParseLine(t *testing.T) {
2938
variables := map[string]string{
3039
"A": "ABC",
@@ -38,11 +47,17 @@ func TestParseLine(t *testing.T) {
3847
"defTest": "WORKED",
3948
}
4049

50+
testInterpolatedDefault(t, "${defVar:-defVal}", ":-", "defVar", "defVal")
51+
testInterpolatedDefault(t, "${defVar2-defVal2}", "-", "defVar2", "defVal2")
52+
testInterpolatedDefault(t, "${defVar:-def:Val}", ":-", "defVar", "def:Val")
53+
testInterpolatedDefault(t, "${defVar:-def-Val}", ":-", "defVar", "def-Val")
54+
4155
testInterpolatedLine(t, "WORKED", "$lower", variables)
4256
testInterpolatedLine(t, "WORKED", "${MiXeD}", variables)
4357
testInterpolatedLine(t, "WORKED", "${split_VaLue}", variables)
4458
// make sure variable name is parsed correctly with default value
4559
testInterpolatedLine(t, "WORKED", "${defTest:-sometest}", variables)
60+
testInterpolatedLine(t, "WORKED", "${defTest-sometest}", variables)
4661
// Starting with a number isn't valid
4762
testInterpolatedLine(t, "", "$9aNumber", variables)
4863
testInterpolatedLine(t, "WORKED", "$a9Number", variables)
@@ -70,6 +85,7 @@ func TestParseLine(t *testing.T) {
7085
testInterpolatedLine(t, "", "$E", variables)
7186
testInterpolatedLine(t, "", "${E}", variables)
7287

88+
testInvalidInterpolatedLine(t, "${df:val}")
7389
testInvalidInterpolatedLine(t, "${")
7490
testInvalidInterpolatedLine(t, "$}")
7591
testInvalidInterpolatedLine(t, "${}")
@@ -213,7 +229,7 @@ func TestInterpolate(t *testing.T) {
213229
214230
# dictionary item value
215231
labels:
216-
mylabel: "myvalue"
232+
mylabel: "my-val:ue"
217233
218234
# unset value
219235
hostname: "host-"
@@ -231,7 +247,7 @@ func TestInterpolate(t *testing.T) {
231247
232248
# dictionary item value
233249
labels:
234-
mylabel: "${LABEL_VALUE:-myvalue}"
250+
mylabel: "${LABEL_VALUE-my-val:ue}"
235251
236252
# unset value
237253
hostname: "host-${UNSET_VALUE}"

0 commit comments

Comments
 (0)