Support $ in comprehesions, fixes #68

This commit is contained in:
Stanisław Barzowski 2017-10-02 13:23:16 -04:00 committed by Dave Cunningham
parent 0ec5f40a58
commit 02a4eed22d
7 changed files with 36 additions and 0 deletions

View File

@ -228,6 +228,11 @@ func desugarArrayComp(comp *ast.ArrayComp, objLevel int) (ast.Node, error) {
func desugarObjectComp(comp *ast.ObjectComp, objLevel int) (ast.Node, error) {
if objLevel == 0 {
dollar := ast.Identifier("$")
comp.Fields = append(comp.Fields, ast.ObjectFieldLocalNoMethod(&dollar, &ast.Self{}))
}
// TODO(sbarzowski) find a consistent convention to prevent desugaring the same thing twice
// here we deeply desugar fields and it will happen again
err := desugarFields(*comp.Loc(), &comp.Fields, objLevel+1)

5
testdata/object_comp_dollar.golden vendored Normal file
View File

@ -0,0 +1,5 @@
{
"a": 42,
"b": 43,
"c": 43
}

1
testdata/object_comp_dollar.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
{ [x]: if x == "a" then 42 else $.a + 1 for x in ["a", "b", "c"] }

14
testdata/object_comp_dollar2.golden vendored Normal file
View File

@ -0,0 +1,14 @@
{
"a": {
"a": 1,
"b": 42
},
"b": {
"a": 1,
"b": 43
},
"c": {
"a": 1,
"b": 43
}
}

1
testdata/object_comp_dollar2.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
{ [x]: { a: 1, b: if x == "a" then 42 else $.a.b + 1 } for x in ["a", "b", "c"] }

7
testdata/object_comp_dollar3.golden vendored Normal file
View File

@ -0,0 +1,7 @@
{
"obj": {
"a": 1,
"b": 2,
"c": 2
}
}

3
testdata/object_comp_dollar3.jsonnet vendored Normal file
View File

@ -0,0 +1,3 @@
local obj = { [x]: if x == "a" then 42 else $.a + 1 for x in ["a", "b", "c"] };
{ obj: obj + {a: 1} }