hcl: support variable interpolations for compat with libucl

This commit is contained in:
Mitchell Hashimoto 2014-08-21 14:38:45 -07:00
parent c6802d3070
commit f65d314d58
5 changed files with 27 additions and 2 deletions

View File

@ -18,6 +18,7 @@ func TestDecode_interface(t *testing.T) {
false,
map[string]interface{}{
"foo": "bar",
"bar": "${file(\"bing/bong.txt\")}",
},
},
{

View File

@ -224,6 +224,8 @@ func (x *hclLex) lexNumber(yylval *hclSymType) int {
// lexString extracts a string from the input
func (x *hclLex) lexString(yylval *hclSymType) int {
braces := 0
var b bytes.Buffer
for {
c := x.next()
@ -232,10 +234,25 @@ func (x *hclLex) lexString(yylval *hclSymType) int {
}
// String end
if c == '"' {
if c == '"' && braces == 0 {
break
}
// If we're starting into variable, mark it
if braces == 0 && c == '$' && x.peek() == '{' {
braces += 1
if _, err := b.WriteRune(c); err != nil {
return lexEOF
}
c = x.next()
} else if braces > 0 && c == '{' {
braces += 1
}
if braces > 0 && c == '}' {
braces -= 1
}
if _, err := b.WriteRune(c); err != nil {
return lexEOF
}

View File

@ -125,6 +125,11 @@ func (x *jsonLex) lexString(yylval *jsonSymType) int {
break
}
// If we're escaping a quote, then escape the quote
if c == '\\' && x.peek() == '"' {
c = x.next()
}
if _, err := b.WriteRune(c); err != nil {
return lexEOF
}

View File

@ -1 +1,2 @@
foo = "bar"
bar = "${file("bing/bong.txt")}"

View File

@ -1,3 +1,4 @@
{
"foo": "bar"
"foo": "bar",
"bar": "${file(\"bing/bong.txt\")}"
}