Skip to content

Commit

Permalink
Don't log error when evaluating aliases array (#104)
Browse files Browse the repository at this point in the history
when parsing aliases we 
 - accept an array of source references
- evaluate each source reference, attempting to parse its result as a
string
- if that fails, evaluate it again, attempting to parse its result as an
array of strings
 
 this flow results in an error being logged.
 this PR prevents that error log
  • Loading branch information
louisheath authored Mar 19, 2024
1 parent 3085569 commit 7858b5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist/
data/
/config.jsonnet
__debug_bin
.idea/
.idea/
*.DS_Store
19 changes: 12 additions & 7 deletions expr/js_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()) {
Expand All @@ -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")
}

0 comments on commit 7858b5c

Please sign in to comment.