From 02a4eed22daca9a9bca3342396fa1ef6a8427024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Barzowski?= Date: Mon, 2 Oct 2017 13:23:16 -0400 Subject: [PATCH] Support $ in comprehesions, fixes #68 --- desugarer.go | 5 +++++ testdata/object_comp_dollar.golden | 5 +++++ testdata/object_comp_dollar.jsonnet | 1 + testdata/object_comp_dollar2.golden | 14 ++++++++++++++ testdata/object_comp_dollar2.jsonnet | 1 + testdata/object_comp_dollar3.golden | 7 +++++++ testdata/object_comp_dollar3.jsonnet | 3 +++ 7 files changed, 36 insertions(+) create mode 100644 testdata/object_comp_dollar.golden create mode 100644 testdata/object_comp_dollar.jsonnet create mode 100644 testdata/object_comp_dollar2.golden create mode 100644 testdata/object_comp_dollar2.jsonnet create mode 100644 testdata/object_comp_dollar3.golden create mode 100644 testdata/object_comp_dollar3.jsonnet diff --git a/desugarer.go b/desugarer.go index 0fe4351..148b4a2 100644 --- a/desugarer.go +++ b/desugarer.go @@ -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) diff --git a/testdata/object_comp_dollar.golden b/testdata/object_comp_dollar.golden new file mode 100644 index 0000000..a488a32 --- /dev/null +++ b/testdata/object_comp_dollar.golden @@ -0,0 +1,5 @@ +{ + "a": 42, + "b": 43, + "c": 43 +} diff --git a/testdata/object_comp_dollar.jsonnet b/testdata/object_comp_dollar.jsonnet new file mode 100644 index 0000000..44c8fe6 --- /dev/null +++ b/testdata/object_comp_dollar.jsonnet @@ -0,0 +1 @@ +{ [x]: if x == "a" then 42 else $.a + 1 for x in ["a", "b", "c"] } diff --git a/testdata/object_comp_dollar2.golden b/testdata/object_comp_dollar2.golden new file mode 100644 index 0000000..8762a12 --- /dev/null +++ b/testdata/object_comp_dollar2.golden @@ -0,0 +1,14 @@ +{ + "a": { + "a": 1, + "b": 42 + }, + "b": { + "a": 1, + "b": 43 + }, + "c": { + "a": 1, + "b": 43 + } +} diff --git a/testdata/object_comp_dollar2.jsonnet b/testdata/object_comp_dollar2.jsonnet new file mode 100644 index 0000000..361b8ed --- /dev/null +++ b/testdata/object_comp_dollar2.jsonnet @@ -0,0 +1 @@ +{ [x]: { a: 1, b: if x == "a" then 42 else $.a.b + 1 } for x in ["a", "b", "c"] } diff --git a/testdata/object_comp_dollar3.golden b/testdata/object_comp_dollar3.golden new file mode 100644 index 0000000..ec532ea --- /dev/null +++ b/testdata/object_comp_dollar3.golden @@ -0,0 +1,7 @@ +{ + "obj": { + "a": 1, + "b": 2, + "c": 2 + } +} diff --git a/testdata/object_comp_dollar3.jsonnet b/testdata/object_comp_dollar3.jsonnet new file mode 100644 index 0000000..e0a35ca --- /dev/null +++ b/testdata/object_comp_dollar3.jsonnet @@ -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} } +