mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Support $ in comprehesions, fixes #68
This commit is contained in:
parent
0ec5f40a58
commit
02a4eed22d
@ -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
5
testdata/object_comp_dollar.golden
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"a": 42,
|
||||
"b": 43,
|
||||
"c": 43
|
||||
}
|
1
testdata/object_comp_dollar.jsonnet
vendored
Normal file
1
testdata/object_comp_dollar.jsonnet
vendored
Normal 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
14
testdata/object_comp_dollar2.golden
vendored
Normal 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
1
testdata/object_comp_dollar2.jsonnet
vendored
Normal 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
7
testdata/object_comp_dollar3.golden
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"obj": {
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 2
|
||||
}
|
||||
}
|
3
testdata/object_comp_dollar3.jsonnet
vendored
Normal file
3
testdata/object_comp_dollar3.jsonnet
vendored
Normal 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} }
|
||||
|
Loading…
x
Reference in New Issue
Block a user