MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestJSONComprehensionVarQualifiers

Function TestJSONComprehensionVarQualifiers

cel/cel_test.go:6952–7054  ·  view source on GitHub ↗

TestJSONComprehensionVarQualifiers is a regression test for https://github.com/cel-expr/cel-go/issues/1585 where comprehension variables bound to raw JSON values (e.g. *structpb.Value elements of a *structpb.ListValue) could not be used as index or map key qualifiers.

(t *testing.T)

Source from the content-addressed store, hash-verified

6950// values (e.g. *structpb.Value elements of a *structpb.ListValue) could not be used as index or
6951// map key qualifiers.
6952func TestJSONComprehensionVarQualifiers(t *testing.T) {
6953 allTwoVar := ReceiverMacro("all", 3,
6954 func(mef MacroExprFactory, target celast.Expr, args []celast.Expr) (celast.Expr, *Error) {
6955 return mef.NewComprehensionTwoVar(
6956 target,
6957 args[0].AsIdent(),
6958 args[1].AsIdent(),
6959 mef.AccuIdentName(),
6960 mef.NewLiteral(types.True),
6961 mef.NewCall(operators.NotStrictlyFalse, mef.NewAccuIdent()),
6962 mef.NewCall(operators.LogicalAnd, mef.NewAccuIdent(), args[2]),
6963 mef.NewAccuIdent(),
6964 ), nil
6965 })
6966 env, err := NewEnv(
6967 OptionalTypes(),
6968 Macros(allTwoVar),
6969 Variable("keys", ListType(StringType)),
6970 Variable("ids", ListType(IntType)),
6971 Variable("obj", MapType(StringType, StringType)),
6972 Variable("names", MapType(StringType, StringType)),
6973 Variable("byID", MapType(IntType, StringType)),
6974 )
6975 if err != nil {
6976 t.Fatalf("NewEnv() failed: %v", err)
6977 }
6978 keys, err := structpb.NewList([]any{"a", "b"})
6979 if err != nil {
6980 t.Fatalf("structpb.NewList() failed: %v", err)
6981 }
6982 ids, err := structpb.NewList([]any{1, 2})
6983 if err != nil {
6984 t.Fatalf("structpb.NewList() failed: %v", err)
6985 }
6986 obj, err := structpb.NewStruct(map[string]any{"x": "a", "y": "b"})
6987 if err != nil {
6988 t.Fatalf("structpb.NewStruct() failed: %v", err)
6989 }
6990 vars := map[string]any{
6991 "keys": keys,
6992 "ids": ids,
6993 "obj": obj,
6994 "names": map[string]string{"a": "A", "b": "B"},
6995 "byID": map[int64]string{1: "A", 2: "B"},
6996 }
6997
6998 tests := []struct {
6999 name string
7000 expr string
7001 partial bool
7002 }{
7003 {
7004 name: "1-var JSON list element as string map key",
7005 expr: `keys.map(k, names[k]) == ['A', 'B']`,
7006 },
7007 {
7008 name: "1-var JSON list element as optional string map key",
7009 expr: `keys.map(k, names[?k].orValue('')) == ['A', 'B']`,

Callers

nothing calls this directly

Calls 15

CompileMethod · 0.95
ProgramMethod · 0.95
ReceiverMacroFunction · 0.85
OptionalTypesFunction · 0.85
EvalOptionsFunction · 0.85
PartialVarsFunction · 0.85
ErrMethod · 0.80
QualStringMethod · 0.80
NewEnvFunction · 0.70
MacrosFunction · 0.70
VariableFunction · 0.70
AttributePatternFunction · 0.70

Tested by

no test coverage detected