feat: add fromJSON support (#352)

This commit is contained in:
Owen
2020-09-02 04:55:29 +08:00
committed by GitHub
parent b6f1df4d2f
commit d521fa5bba
2 changed files with 17 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ func (rc *RunContext) newVM() *otto.Otto {
vmFormat,
vmJoin,
vmToJSON,
vmFromJSON,
vmAlways,
rc.vmCancelled(),
rc.vmSuccess(),
@@ -219,6 +220,20 @@ func vmToJSON(vm *otto.Otto) {
_ = vm.Set("toJson", toJSON)
}
func vmFromJSON(vm *otto.Otto) {
fromJSON := func(str string) map[string]interface{} {
var dat map[string]interface{}
err := json.Unmarshal([]byte(str), &dat)
if err != nil {
logrus.Errorf("Unable to unmarshal: %v", err)
return dat
}
return dat
}
_ = vm.Set("fromJSON", fromJSON)
_ = vm.Set("fromJson", fromJSON)
}
func (rc *RunContext) vmHashFiles() func(*otto.Otto) {
return func(vm *otto.Otto) {
_ = vm.Set("hashFiles", func(path string) string {