Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ES6 compact-style multi-select-hash. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/jmespath.net.parser/JmesPathParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ void OnHashWildcardProjection() =>
void AddMultiSelectHashExpression() => generator_.AddMultiSelectHashExpression();
void PopMultiSelectHash() => generator_.PopMultiSelectHash();

void AddCompactHashExpression(Token token)
{
generator_.OnIdentifier((string)token.Value);
generator_.AddMultiSelectHashExpression();
}

#endregion

#region multi_select_list
Expand Down
23 changes: 22 additions & 1 deletion src/jmespath.net.parser/JmesPathParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,28 @@ hash_wildcard : T_STAR
}
;

multi_select_hash : T_LBRACE keyval_expressions T_RBRACE
multi_select_hash : multi_select_hash_key
| multi_select_hash_keyval
;

multi_select_hash_key
: T_LBRACE key_expressions T_RBRACE
{
PopMultiSelectHash();
};

key_expressions : identifier
{
PushMultiSelectHash();
AddCompactHashExpression($1.Token);
}
|key_expressions T_COMMA identifier
{
AddCompactHashExpression($3.Token);
};

multi_select_hash_keyval
: T_LBRACE keyval_expressions T_RBRACE
{
PopMultiSelectHash();
};
Expand Down
6 changes: 6 additions & 0 deletions tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ public void ParseMultiSelectHash_Compliance()
{
Assert("missing.{foo: bar}", "[]", "null");
}
[Fact]
public void ParseMultiSelectHash_Compact()
{
Assert("{foo: foo, baz: baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}");
Assert("{foo, baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}");
}
}
}