Skip to content

Commit

Permalink
Merge pull request #94 from k2tzumi/feature/support-json-object-expand
Browse files Browse the repository at this point in the history
Support json object expand
  • Loading branch information
k1LoW authored Aug 12, 2022
2 parents e81c95e + ad8a8dc commit 5c3c090
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/antonmedv/expr"
"github.com/fatih/color"
"github.com/goccy/go-json"
"github.com/goccy/go-yaml"
"github.com/hashicorp/go-multierror"
"github.com/k1LoW/expand"
Expand Down Expand Up @@ -840,6 +841,13 @@ func (o *operator) expand(in interface{}) (interface{}, error) {
s = strconv.Itoa(v)
case bool:
s = strconv.FormatBool(v)
case map[string]interface{}, []interface{}:
bytes, err := json.Marshal(v)
if err != nil {
reperr = fmt.Errorf("json.Marshal error: %w", err)
} else {
s = string(bytes)
}
default:
reperr = fmt.Errorf("invalid format: %v\n%s", o, string(b))
}
Expand Down
12 changes: 12 additions & 0 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func TestExpand(t *testing.T) {
map[string]string{"boolean": "{{ vars.boolean }}"},
map[string]interface{}{"boolean": true},
},
{
[]map[string]interface{}{},
map[string]interface{}{"map": map[string]interface{}{"foo": "test", "bar": 1}},
map[string]string{"map": "{{ vars.map }}"},
map[string]interface{}{"map": map[string]interface{}{"foo": "test", "bar": uint64(1)}},
},
{
[]map[string]interface{}{},
map[string]interface{}{"array": []interface{}{map[string]interface{}{"foo": "test1", "bar": 1}, map[string]interface{}{"foo": "test2", "bar": 2}}},
map[string]string{"array": "{{ vars.array }}"},
map[string]interface{}{"array": []interface{}{map[string]interface{}{"foo": "test1", "bar": uint64(1)}, map[string]interface{}{"foo": "test2", "bar": uint64(2)}}},
},
}
for _, tt := range tests {
o, err := New()
Expand Down

0 comments on commit 5c3c090

Please sign in to comment.