Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 36 additions & 2 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3702,21 +3702,55 @@ func TestJSONFieldNames(t *testing.T) {
expr: `dyn(msg).single_int32 == dyn(msg).singleInt32`,
jsonFieldNames: true,
},
{
name: "proto with extensions",
expr: `google.expr.proto2.test.ExampleType{fooBar: 'value'}.fooBar == 'value'`,
jsonFieldNames: true,
},
{
name: "json opt fields",
expr: "jsonOptMsg.int32_snake_case_json_name == 1 && " +
"jsonOptMsg.int64CamelCaseJsonName == 2 && " +
"jsonOptMsg.uint32DefaultJsonName == 3u && " +
"jsonOptMsg.`uint64-custom-json-name` == 4u && " +
"jsonOptMsg.single_string == 'shadows' && " +
"jsonOptMsg.singleString == 'shadowed'",
jsonFieldNames: true,
},
{
name: "json opt fields fallback",
expr: "dyn(jsonOptMsg).int32_snake_case_json_name == 1 && " +
"dyn(jsonOptMsg).`uint64-custom-json-name` == 4u && " +
"dyn(jsonOptMsg).single_string == 'shadows' && " +
"dyn(jsonOptMsg).string_json_name_shadows == 'shadows' && " +
"dyn(jsonOptMsg).singleString == 'shadowed'",
jsonFieldNames: true,
},
}
msg := &proto3pb.TestAllTypes{
SingleInt32: 1,
MapStringString: map[string]string{
"key": "value",
},
}
jsonOptMsg := &proto3pb.TestJsonNames{
Int32SnakeCaseJsonName: 1,
Int64CamelCaseJsonName: 2,
Uint32DefaultJsonName: 3,
Uint64CustomJsonName: 4,
StringJsonNameShadows: "shadows",
SingleString: "shadowed",
}
for _, tst := range tests {
tc := tst
t.Run(tc.name, func(t *testing.T) {
env, err := NewEnv(
EnableIdentifierEscapeSyntax(),
JSONFieldNames(tc.jsonFieldNames),
Types(msg),
Types(msg, &proto2pb.ExternalMessageType{}, jsonOptMsg),
Container(string(msg.ProtoReflect().Descriptor().ParentFile().Package())),
Variable("msg", ObjectType(string(msg.ProtoReflect().Descriptor().FullName()))),
Variable("jsonOptMsg", ObjectType(string(jsonOptMsg.ProtoReflect().Descriptor().FullName()))),
)
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
Expand All @@ -3729,7 +3763,7 @@ func TestJSONFieldNames(t *testing.T) {
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}
out, _, err := prg.Eval(map[string]any{"msg": msg})
out, _, err := prg.Eval(map[string]any{"msg": msg, "jsonOptMsg": jsonOptMsg})
if err != nil {
t.Fatalf("prg.Eval() failed: %v", err)
}
Expand Down
10 changes: 8 additions & 2 deletions common/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,14 @@ func (o *protoObj) format(sb *strings.Builder) {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%s: ", field.Name()))
formatTo(sb, o.Get(String(field.Name())))
name := String(field.Name())
if field.IsExtension() {
name = String(field.FullName())
fmt.Fprintf(sb, "`%s`: ", name)
} else {
fmt.Fprintf(sb, "%s: ", name)
}
formatTo(sb, o.Get(name))
}
sb.WriteString("}")
}
1 change: 1 addition & 0 deletions common/types/pb/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestFileDescriptionGetTypes(t *testing.T) {
"google.expr.proto3.test.TestAllTypes.NestedMessage",
"google.expr.proto3.test.TestAllTypes.MapStringStringEntry",
"google.expr.proto3.test.TestAllTypes.MapInt64NestedTypeEntry",
"google.expr.proto3.test.TestJsonNames",
"google.expr.proto3.test.NestedTestAllTypes"}
if len(fd.GetTypeNames()) != len(expected) {
t.Errorf("got '%v', wanted '%v'", fd.GetTypeNames(), expected)
Expand Down
13 changes: 9 additions & 4 deletions common/types/pb/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,24 @@ func (td *TypeDescription) FieldMap() map[string]*FieldDescription {

// FieldByName returns (FieldDescription, true) if the field name is declared within the type.
func (td *TypeDescription) FieldByName(name string) (*FieldDescription, bool) {
if td.jsonFieldNames {
fd, found := td.jsonFieldMap[name]
if found {
return fd, true
}
}

fd, found := td.fieldMap[name]
if found {
return fd, true
}

extFieldMap, found := td.extensions[td.typeName]
if found {
fd, found = extFieldMap[name]
return fd, found
}
if td.jsonFieldNames {
fd, found = td.jsonFieldMap[name]
return fd, found
}

return nil, false
}

Expand Down
35 changes: 28 additions & 7 deletions repl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,10 @@ func (o *typeOption) String() string {
return fmt.Sprintf("%%load_descriptors %s '%s'", flags, o.path)
}

func (o *typeOption) Option() cel.EnvOption {
return cel.TypeDescs(o.fds)
}

type backtickOpt struct {
enabled bool
}
Expand All @@ -1009,8 +1013,17 @@ func (o *backtickOpt) String() string {
return "%option --enable_escaped_fields"
}

func (o *typeOption) Option() cel.EnvOption {
return cel.TypeDescs(o.fds)
type jsonOpt struct {
enabled bool
}

func (o *jsonOpt) Option() cel.EnvOption {

return cel.JSONFieldNames(o.enabled)
}

func (o *jsonOpt) String() string {
return "%option --enable_escaped_fields"
}

type containerOption struct {
Expand Down Expand Up @@ -1083,6 +1096,11 @@ func (e *Evaluator) setOption(args []string) error {
if err != nil {
issues = append(issues, fmt.Sprintf("enable_escaped_fields: %v", err))
}
case "--enable_json_field_names":
err := e.AddSerializableOption(&jsonOpt{enabled: true})
if err != nil {
issues = append(issues, fmt.Sprintf("enable_json_field_names: %v", err))
}
case "--enable_partial_eval":
err := e.EnablePartialEval()
if err != nil {
Expand Down Expand Up @@ -1190,14 +1208,17 @@ func deps(d protoreflect.FileDescriptor) []*descpb.FileDescriptorProto {
func (e *Evaluator) loadDescriptorFromPackage(pkg string) error {
switch pkg {
case "cel-spec-test-types":
fdp := (&test2pb.TestAllTypes{}).ProtoReflect().Type().Descriptor().ParentFile()
fdp2 := (&test3pb.TestAllTypes{}).ProtoReflect().Type().Descriptor().ParentFile()
fdp2 := (&test2pb.TestAllTypes{}).ProtoReflect().Type().Descriptor().ParentFile()
fdp2ext := (&test2pb.Proto2ExtensionScopedMessage{}).ProtoReflect().Type().Descriptor().ParentFile()
fdp3 := (&test3pb.TestAllTypes{}).ProtoReflect().Type().Descriptor().ParentFile()

descriptorProtos := deps(fdp)
// We only depend on WKTs.
descriptorProtos := deps(fdp2)

descriptorProtos = append(descriptorProtos,
protodesc.ToFileDescriptorProto(fdp),
protodesc.ToFileDescriptorProto(fdp2))
protodesc.ToFileDescriptorProto(fdp2),
protodesc.ToFileDescriptorProto(fdp2ext),
protodesc.ToFileDescriptorProto(fdp3))

fds := descpb.FileDescriptorSet{
File: descriptorProtos,
Expand Down
31 changes: 31 additions & 0 deletions repl/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,37 @@ func TestProcess(t *testing.T) {
wantExit: false,
wantError: false,
},
{
name: "LoadDescriptorsPackageSpecExtensions",
commands: []Cmder{
&simpleCmd{
cmd: "load_descriptors",
args: []string{
"--pkg",
"cel-spec-test-types",
},
},
&simpleCmd{
cmd: "option",
args: []string{
"--container",
"cel.expr.conformance",
},
},
&simpleCmd{
cmd: "option",
args: []string{
"--enable_escaped_fields",
},
},
&evalCmd{
expr: "proto2.TestAllTypes{`cel.expr.conformance.proto2.int32_ext`: 42}.`cel.expr.conformance.proto2.int32_ext` == 42",
},
},
wantText: `true : bool`,
wantExit: false,
wantError: false,
},
{
name: "LoadDescriptorsPackageRpc",
commands: []Cmder{
Expand Down
2 changes: 2 additions & 0 deletions repl/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/google/cel-go/repl"

Expand All @@ -52,6 +53,7 @@ import (
func main() {
var c readline.Config
c.Prompt = "cel-repl> "
c.HistoryFile = filepath.Join(os.Getenv("HOME"), ".cel-repl.history")

err := c.Init()
if err != nil {
Expand Down
Loading