From e9de6ca2c0e5618f3e6db0cd175ab47ad8020d24 Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Fri, 28 Feb 2020 15:20:31 -0800 Subject: [PATCH] fix #115 - support toJson and toJSON (#116) --- pkg/runner/expression.go | 6 ++++-- pkg/runner/expression_test.go | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/runner/expression.go b/pkg/runner/expression.go index e7b533b..af4a2d1 100644 --- a/pkg/runner/expression.go +++ b/pkg/runner/expression.go @@ -166,14 +166,16 @@ func vmJoin(vm *otto.Otto) { } func vmToJSON(vm *otto.Otto) { - _ = vm.Set("toJSON", func(o interface{}) string { + toJSON := func(o interface{}) string { rtn, err := json.MarshalIndent(o, "", " ") if err != nil { logrus.Errorf("Unable to marsal: %v", err) return "" } return string(rtn) - }) + } + _ = vm.Set("toJSON", toJSON) + _ = vm.Set("toJson", toJSON) } func (rc *RunContext) vmHashFiles() func(*otto.Otto) { diff --git a/pkg/runner/expression_test.go b/pkg/runner/expression_test.go index 70f608f..6c01fe8 100644 --- a/pkg/runner/expression_test.go +++ b/pkg/runner/expression_test.go @@ -64,6 +64,7 @@ func TestEvaluate(t *testing.T) { {"join(['hello','mona','the'],'octocat')", "hello mona the octocat", ""}, {"join('hello','mona')", "hello mona", ""}, {"toJSON({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""}, + {"toJson({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""}, {"hashFiles('**/package-lock.json')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, {"success()", "true", ""}, {"failure()", "false", ""},