Skip to content

Commit 2e84645

Browse files
committed
Implement ES6 compact-style multi-select-hash.
1 parent de2a406 commit 2e84645

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/jmespath.net.parser/JmesPathParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ void OnHashWildcardProjection() =>
181181
void AddMultiSelectHashExpression() => generator_.AddMultiSelectHashExpression();
182182
void PopMultiSelectHash() => generator_.PopMultiSelectHash();
183183

184+
void AddCompactHashExpression(Token token)
185+
{
186+
generator_.OnIdentifier((string)token.Value);
187+
generator_.AddMultiSelectHashExpression();
188+
}
189+
184190
#endregion
185191

186192
#region multi_select_list

src/jmespath.net.parser/JmesPathParser.y

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,28 @@ hash_wildcard : T_STAR
246246
}
247247
;
248248

249-
multi_select_hash : T_LBRACE keyval_expressions T_RBRACE
249+
multi_select_hash : multi_select_hash_key
250+
| multi_select_hash_keyval
251+
;
252+
253+
multi_select_hash_key
254+
: T_LBRACE key_expressions T_RBRACE
255+
{
256+
PopMultiSelectHash();
257+
};
258+
259+
key_expressions : identifier
260+
{
261+
PushMultiSelectHash();
262+
AddCompactHashExpression($1.Token);
263+
}
264+
|key_expressions T_COMMA identifier
265+
{
266+
AddCompactHashExpression($3.Token);
267+
};
268+
269+
multi_select_hash_keyval
270+
: T_LBRACE keyval_expressions T_RBRACE
250271
{
251272
PopMultiSelectHash();
252273
};

tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@ public void ParseMultiSelectHash_Compliance()
2929
{
3030
Assert("missing.{foo: bar}", "[]", "null");
3131
}
32+
[Fact]
33+
public void ParseMultiSelectHash_Compact()
34+
{
35+
Assert("{foo: foo, baz: baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}");
36+
Assert("{foo, baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}");
37+
}
3238
}
3339
}

0 commit comments

Comments
 (0)