From 09693ec5b9f0d2643b0a57826e2c415dfc32de2d Mon Sep 17 00:00:00 2001 From: Mike Beaumont Date: Mon, 15 Nov 2021 15:26:04 +0100 Subject: [PATCH] fix: fromJSON should work with any JSON (#883) * test: fromJSON should work with arrays * fix: fromJSON should work with any JSON --- pkg/runner/expression.go | 4 ++-- pkg/runner/expression_test.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/runner/expression.go b/pkg/runner/expression.go index 5f22a38..272575c 100644 --- a/pkg/runner/expression.go +++ b/pkg/runner/expression.go @@ -327,8 +327,8 @@ func vmToJSON(vm *otto.Otto) { } func vmFromJSON(vm *otto.Otto) { - fromJSON := func(str string) map[string]interface{} { - var dat map[string]interface{} + fromJSON := func(str string) interface{} { + var dat interface{} err := json.Unmarshal([]byte(str), &dat) if err != nil { log.Errorf("Unable to unmarshal: %v", err) diff --git a/pkg/runner/expression_test.go b/pkg/runner/expression_test.go index 6876097..904a20e 100644 --- a/pkg/runner/expression_test.go +++ b/pkg/runner/expression_test.go @@ -93,6 +93,7 @@ func TestEvaluate(t *testing.T) { {"toJson({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""}, {"(fromJSON('{\"foo\":\"bar\"}')).foo", "bar", ""}, {"(fromJson('{\"foo\":\"bar\"}')).foo", "bar", ""}, + {"(fromJson('[\"foo\",\"bar\"]'))[1]", "bar", ""}, {"hashFiles('**/non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, {"hashFiles('**/non-extant-files', '**/more-non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, {"hashFiles('**/non.extant.files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},