Skip to content

Commit d521fa5

Browse files
authored
feat: add fromJSON support (#352)
1 parent b6f1df4 commit d521fa5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pkg/runner/expression.go

+15
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (rc *RunContext) newVM() *otto.Otto {
132132
vmFormat,
133133
vmJoin,
134134
vmToJSON,
135+
vmFromJSON,
135136
vmAlways,
136137
rc.vmCancelled(),
137138
rc.vmSuccess(),
@@ -219,6 +220,20 @@ func vmToJSON(vm *otto.Otto) {
219220
_ = vm.Set("toJson", toJSON)
220221
}
221222

223+
func vmFromJSON(vm *otto.Otto) {
224+
fromJSON := func(str string) map[string]interface{} {
225+
var dat map[string]interface{}
226+
err := json.Unmarshal([]byte(str), &dat)
227+
if err != nil {
228+
logrus.Errorf("Unable to unmarshal: %v", err)
229+
return dat
230+
}
231+
return dat
232+
}
233+
_ = vm.Set("fromJSON", fromJSON)
234+
_ = vm.Set("fromJson", fromJSON)
235+
}
236+
222237
func (rc *RunContext) vmHashFiles() func(*otto.Otto) {
223238
return func(vm *otto.Otto) {
224239
_ = vm.Set("hashFiles", func(path string) string {

pkg/runner/expression_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func TestEvaluate(t *testing.T) {
8080
{"join('hello','mona')", "hello mona", ""},
8181
{"toJSON({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""},
8282
{"toJson({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""},
83+
{"(fromJSON('{\"foo\":\"bar\"}')).foo", "bar", ""},
84+
{"(fromJson('{\"foo\":\"bar\"}')).foo", "bar", ""},
8385
{"hashFiles('**/package-lock.json')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
8486
{"success()", "true", ""},
8587
{"failure()", "false", ""},

0 commit comments

Comments
 (0)