diff --git a/.gitignore b/.gitignore index 3ba8c00..7bb6726 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist/ data/ /config.jsonnet __debug_bin -.idea/ \ No newline at end of file +.idea/ +*.DS_Store diff --git a/expr/js_eval.go b/expr/js_eval.go index fb7c5cf..b665dfc 100644 --- a/expr/js_eval.go +++ b/expr/js_eval.go @@ -94,8 +94,7 @@ func EvaluateArray[ReturnType any](ctx context.Context, source string, subject a evaluatedValues := []otto.Value{} if result.IsObject() { - switch result.Object().Class() { - case "GoSlice", "Array": + if isArray(result) { for _, key := range result.Object().Keys() { // This should always work, as we just asked for the available keys. element, err := result.Object().Get(key) @@ -215,15 +214,16 @@ func EvaluateResultType[ReturnType any](ctx context.Context, source string, resu case result.IsUndefined(): // do nothing, undefined gets skipped + return resultValue, nil + + case isArray(result): + fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an array. Assuming this is handled separately\n", source) + return resultValue, nil default: - fmt.Fprintf(os.Stderr, "\n Unsupported Javascript value type found by expression %s: %s.\n", source, map[string]any{ - "result": result, - }) + fmt.Fprintf(os.Stderr, "\n Unsupported Javascript value type found by expression %s: %+v.\n", source, result) return resultValue, nil } - - return resultValue, nil } func SafelyGo(do func()) { @@ -235,3 +235,8 @@ func SafelyGo(do func()) { do() }() } + +func isArray(value otto.Value) bool { + return value.IsObject() && + (value.Object().Class() == "Array" || value.Object().Class() == "GoSlice") +}