Skip to content

Commit

Permalink
support string interpolation for object pattern key
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 5, 2020
1 parent b7bfa53 commit 58d34f1
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 127 deletions.
20 changes: 12 additions & 8 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2532,31 +2532,35 @@
- name: binding an object deeply
args:
- '. as {$foo, bar: {baz: {$qux}}} | {$foo, $qux}'
input: '{} {"foo": 10} {"foo": 20, "bar": { "baz": { "qux": 30 } } }'
- '. as {$foo, bar: {"baz": {$qux}, "": $quy}} | {$foo, $qux, $quy}'
input: '{} {"foo": 10} {"foo": 20, "bar": { "baz": { "qux": 30 }, "": -1 } }'
expected: |
{
"foo": null,
"qux": null
"qux": null,
"quy": null
}
{
"foo": 10,
"qux": null
"qux": null,
"quy": null
}
{
"foo": 20,
"qux": 30
"qux": 30,
"quy": -1
}
- name: binding an object with expression
args:
- '. as {as: $foo, ("e"+"x"+"p"): $bar, (.foo): $baz} | [$foo, $bar, $baz]'
input: '{"as": 1, "exp": 2, "foo": "bar", "bar": 3}'
- '. as {as: $foo, ("e"+"x"+"p"): $bar, (.foo): $baz, "\(.qux)": $qux} | [$foo, $bar, $baz, $qux]'
input: '{"as": 1, "exp": 2, "foo": "bar", "bar": 3, "qux": "quux", "quux": 4}'
expected: |
[
1,
2,
3
3,
4
]
- name: binding an object error
Expand Down
8 changes: 5 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,11 @@ func (c *compiler) compilePattern(p *Pattern) ([][2]int, error) {
key, name = key[1:], key
}
c.append(&code{op: oppush, v: key})
} else if kv.KeyString != "" {
key = kv.KeyString[1 : len(kv.KeyString)-1]
c.append(&code{op: oppush, v: key})
} else if kv.KeyString != nil {
c.append(&code{op: opload, v: v})
if err := c.compileString(kv.KeyString, nil); err != nil {
return nil, err
}
} else if kv.Query != nil {
c.append(&code{op: opload, v: v})
if err := c.compileQuery(kv.Query); err != nil {
Expand Down
226 changes: 115 additions & 111 deletions parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parser.go.y
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ objectpattern
{
$$ = &PatternObject{Key: $1, Val: $3.(*Pattern)}
}
| tokString ':' pattern
| string ':' pattern
{
$$ = &PatternObject{KeyString: $1, Val: $3.(*Pattern)}
$$ = &PatternObject{KeyString: $1.(*String), Val: $3.(*Pattern)}
}
| '(' query ')' ':' pattern
{
Expand Down
6 changes: 3 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (e *Pattern) String() string {
// PatternObject ...
type PatternObject struct {
Key string
KeyString string
KeyString *String
Query *Query
Val *Pattern
KeyOnly string
Expand All @@ -362,8 +362,8 @@ func (e *PatternObject) String() string {
var s strings.Builder
if e.Key != "" {
s.WriteString(e.Key)
} else if e.KeyString != "" {
s.WriteString(e.KeyString)
} else if e.KeyString != nil {
fmt.Fprint(&s, e.KeyString)
} else if e.Query != nil {
fmt.Fprintf(&s, "(%s)", e.Query)
}
Expand Down

0 comments on commit 58d34f1

Please sign in to comment.