From b10758fabbdcac46d41fa71c6289390fb9110660 Mon Sep 17 00:00:00 2001 From: Louis Heath Date: Tue, 19 Mar 2024 11:20:00 +0000 Subject: [PATCH 1/4] don't error on slice --- .gitignore | 3 ++- expr/js_eval.go | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) 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..a4ae02d 100644 --- a/expr/js_eval.go +++ b/expr/js_eval.go @@ -215,15 +215,17 @@ func EvaluateResultType[ReturnType any](ctx context.Context, source string, resu case result.IsUndefined(): // do nothing, undefined gets skipped + return resultValue, nil + + case result.IsObject(): + // objects / slices need to be handled explicitly elsewhere + fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an object or array. Falling through\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()) { From 63ef9a8153275786d2f3197dcd196efe29e4f997 Mon Sep 17 00:00:00 2001 From: Louis Heath Date: Tue, 19 Mar 2024 11:22:54 +0000 Subject: [PATCH 2/4] check for array explicitly --- expr/js_eval.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/expr/js_eval.go b/expr/js_eval.go index a4ae02d..c7be941 100644 --- a/expr/js_eval.go +++ b/expr/js_eval.go @@ -78,6 +78,11 @@ func EvaluateJavascript(ctx context.Context, source string, subject any, logger } +func isArray(value otto.Value) bool { + return value.IsObject() && + (value.Object().Class() == "Array" || value.Object().Class() == "GoSlice") +} + func EvaluateArray[ReturnType any](ctx context.Context, source string, subject any, logger kitlog.Logger) ([]ReturnType, error) { result, err := EvaluateJavascript(ctx, source, subject, logger) if err != nil { @@ -94,8 +99,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) @@ -217,9 +221,8 @@ func EvaluateResultType[ReturnType any](ctx context.Context, source string, resu // do nothing, undefined gets skipped return resultValue, nil - case result.IsObject(): - // objects / slices need to be handled explicitly elsewhere - fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an object or array. Falling through\n", source) + case isArray(result): + fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an array. Handling separately\n", source) return resultValue, nil default: From 8947f4600d7b8e9023d31c66591a0541992457f2 Mon Sep 17 00:00:00 2001 From: Louis Heath Date: Tue, 19 Mar 2024 11:30:15 +0000 Subject: [PATCH 3/4] priv func to bottom --- expr/js_eval.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/expr/js_eval.go b/expr/js_eval.go index c7be941..4415caf 100644 --- a/expr/js_eval.go +++ b/expr/js_eval.go @@ -78,11 +78,6 @@ func EvaluateJavascript(ctx context.Context, source string, subject any, logger } -func isArray(value otto.Value) bool { - return value.IsObject() && - (value.Object().Class() == "Array" || value.Object().Class() == "GoSlice") -} - func EvaluateArray[ReturnType any](ctx context.Context, source string, subject any, logger kitlog.Logger) ([]ReturnType, error) { result, err := EvaluateJavascript(ctx, source, subject, logger) if err != nil { @@ -240,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") +} From 6e0084d77ffe272b55b2ed79411d73a7b6cfb998 Mon Sep 17 00:00:00 2001 From: Louis Heath Date: Tue, 19 Mar 2024 11:50:00 +0000 Subject: [PATCH 4/4] slight copy change --- expr/js_eval.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expr/js_eval.go b/expr/js_eval.go index 4415caf..b665dfc 100644 --- a/expr/js_eval.go +++ b/expr/js_eval.go @@ -217,7 +217,7 @@ func EvaluateResultType[ReturnType any](ctx context.Context, source string, resu return resultValue, nil case isArray(result): - fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an array. Handling separately\n", source) + fmt.Fprintf(os.Stdout, "\n Source %s evaluates to an array. Assuming this is handled separately\n", source) return resultValue, nil default: