Skip to content

Commit

Permalink
Merge pull request #700 from k1LoW/bind-slice
Browse files Browse the repository at this point in the history
Support to bind to slice values
  • Loading branch information
k1LoW authored Nov 28, 2023
2 parents 7649e54 + 15c29bc commit 28cd0a7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
24 changes: 15 additions & 9 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ func evalBindKeyValue(bindVars map[string]any, k string, v any, store map[string
if err != nil {
return nil, err
}
if !strings.Contains(k, "[") || !strings.HasSuffix(k, "]") {
// Override the value of bindVars
bindVars[k] = vv
return bindVars, nil
if strings.HasSuffix(k, "[]") {
// Append to slice
// - foo[]
// - foo[bar][]
kk := strings.TrimSuffix(k, "[]")
return evalBindKeyValue(bindVars, kk, []any{v}, store)
}
// Merge the value of bindVars
// foo[bar]
// foo['bar']
// foo[5]
// foo[bar][baz]
// Merge to map
// - foo
// - foo[bar]
// - foo['bar']
// - foo[5]
// - foo[bar][baz]
tr, err := parser.Parse(k)
if err != nil {
return nil, err
Expand All @@ -80,6 +83,9 @@ func evalBindKeyValue(bindVars map[string]any, k string, v any, store map[string
func nodeToMap(n ast.Node, v any, store map[string]any) (map[string]any, error) {
m := map[string]any{}
switch nn := n.(type) {
case *ast.IdentifierNode:
k := nn.Value
m[k] = v
case *ast.MemberNode:
switch nnn := nn.Node.(type) {
case *ast.IdentifierNode:
Expand Down
62 changes: 62 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestBindRunnerRun(t *testing.T) {
"foo[3]": "'three'",
"foo[vars.key]": "'four'",
"foo[vars.key][vars.key]": "'five'",
"bar[]": "'six'",
},
store{
steps: []map[string]any{
Expand All @@ -203,6 +204,7 @@ func TestBindRunnerRun(t *testing.T) {
"value": "five",
},
},
"bar": []any{"six"},
},
},
map[string]any{
Expand All @@ -219,6 +221,41 @@ func TestBindRunnerRun(t *testing.T) {
"value": "five",
},
},
"bar": []any{"six"},
},
},
{
store{
steps: []map[string]any{},
vars: map[string]any{
"key": "value",
},
bindVars: map[string]any{
"bar": []any{"six"},
},
},
map[string]any{
"bar[]": "'seven'",
},
store{
steps: []map[string]any{
{"run": true},
},
vars: map[string]any{
"key": "value",
},
bindVars: map[string]any{
"bar": []any{"six", "seven"},
},
},
map[string]any{
"steps": []map[string]any{
{"run": true},
},
"vars": map[string]any{
"key": "value",
},
"bar": []any{"six", "seven"},
},
},
}
Expand Down Expand Up @@ -410,6 +447,13 @@ func TestNodeToMap(t *testing.T) {
},
},
},
{
"foo",
map[string]any{},
map[string]any{
"foo": v,
},
},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
Expand Down Expand Up @@ -601,6 +645,24 @@ func TestMergeVars(t *testing.T) {
},
},
},
{
map[string]any{
"parent": []any{
"one",
},
},
map[string]any{
"parent": []any{
"two",
},
},
map[string]any{
"parent": []any{
"one",
"two",
},
},
},
}
for i, tt := range tests {
tt := tt
Expand Down

0 comments on commit 28cd0a7

Please sign in to comment.