Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cel/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func interpolate(raw string, eval func(expr string, whole bool) (interface{}, er
"for example %s.join(\",\")", f.Expr, f.Expr)
case map[string]interface{}:
return nil, fmt.Errorf("%q is a map, which text cannot embed; select a value from it", f.Expr)
case nil:
return nil, fmt.Errorf("%q is null, which text cannot embed; guard it, for example has(x) && x != null ? x : \"none\"", f.Expr)
}
fmt.Fprintf(&b, "%v", v)
}
Expand All @@ -148,6 +150,8 @@ func Typed(v interface{}) interface{} { return alreadyTyped{value: normaliseWidt
// requires; a non-string key would not survive JSON anyway.
func native(v ref.Val) interface{} {
switch t := v.(type) {
case types.Null:
return nil
case traits.Lister:
n, ok := t.Size().Value().(int64)
if !ok {
Expand Down
33 changes: 33 additions & 0 deletions cel/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cel

import (
"strings"
"testing"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -221,3 +222,35 @@ func TestEvalPropertyCollapsesEscapesWithoutAnExpression(t *testing.T) {
})
}
}

// A null is nil to the properties document, never protobuf's NULL_VALUE: a whole
// value stays null, and text refuses to embed one, as it refuses a list or a map.
func TestEvalNull(t *testing.T) {
Comment thread
briankane marked this conversation as resolved.
Resolved
env := testEnv(t)
in := map[string]interface{}{
"context": map[string]interface{}{"appName": "checkout"},
}

got, err := fixture.Eval(env, `null`, in)
if err != nil || got != nil {
t.Fatalf("Eval of a null field: got %#v, %v; want nil", got, err)
}

got, err = fixture.EvalProperty(env, `$(null)`, in)
if err != nil || got != nil {
t.Fatalf("whole-value null: got %#v, %v; want nil", got, err)
}

got, err = fixture.EvalProperty(env, `$([context.appName, null])`, in)
if err != nil {
t.Fatalf("list holding a null: %v", err)
}
if l, ok := got.([]interface{}); !ok || len(l) != 2 || l[1] != nil {
t.Fatalf("list holding a null: got %#v, want [checkout <nil>]", got)
}

_, err = fixture.EvalProperty(env, `host=$(null)`, in)
if err == nil || !strings.Contains(err.Error(), `"null" is null`) {
t.Fatalf("null embedded in text: got err %v, want a refusal naming the null", err)
}
}
12 changes: 5 additions & 7 deletions cue/cuex/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ func (e ProviderFnNotFoundErr) Error() string {

// FunctionCallError error for executing provider function
type FunctionCallError struct {
Path string
Path string
// Value is the call as it was made, for a caller that wants it. It is not
// part of Error, whose message is shown in resource status.
Value string
Err error
}

// Error .
// Error names the call and what the provider reported.
func (e FunctionCallError) Error() string {
s := "function call error"
if e.Path != "" {
s += " for " + e.Path
}
s += ": " + e.Err.Error()
if e.Value != "" {
s += " (value: " + e.Value + ")"
}
return s
return s + ": " + e.Err.Error()
}

// NewFunctionCallError create a new error for executing resolved function call
Expand Down
16 changes: 9 additions & 7 deletions cue/cuex/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestErrors(t *testing.T) {

v := cuecontext.New().CompileString(`a: b: "c"`).LookupPath(cue.ParsePath("a.b"))
e := cuex.NewFunctionCallError(v, fmt.Errorf("err"))
require.Equal(t, `function call error for a.b: err (value: "c")`, e.Error())
require.Equal(t, "function call error for a.b: err", e.Error())
require.Equal(t, `"c"`, e.Value, "the value stays on the error for a caller that wants it")

require.Equal(t, "cuex compile resolve timeout", cuex.ResolveTimeoutErr{}.Error())
}
Expand All @@ -45,19 +46,20 @@ func TestFunctionCallErrorMessage(t *testing.T) {
expected string
}{
{
name: "path and value present",
name: "the value is not part of the message",
err: cuex.FunctionCallError{Path: "a.b", Value: `"c"`, Err: fmt.Errorf("err")},
expected: `function call error for a.b: err (value: "c")`,
expected: "function call error for a.b: err",
},
{
name: "empty path drops the for clause",
err: cuex.FunctionCallError{Value: `"c"`, Err: fmt.Errorf("err")},
expected: `function call error: err (value: "c")`,
expected: "function call error: err",
},
{
name: "empty value drops the value clause",
err: cuex.FunctionCallError{Path: "a.b", Err: fmt.Errorf("err")},
expected: "function call error for a.b: err",
name: "a provider call's whole value stays out of a one-line message",
err: cuex.FunctionCallError{Path: "_res", Value: "#do: \"do\"\n#provider: \"http\"\n$params: {\n\turl: \"http://x\"\n}",
Err: fmt.Errorf("Get \"http://x\": connect: connection refused")},
expected: `function call error for _res: Get "http://x": connect: connection refused`,
},
{
name: "empty path and value",
Expand Down
Loading