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

Function fieldToVariable

cel/options.go:995–1025  ·  view source on GitHub ↗
(field protoreflect.FieldDescriptor, jsonFieldNames bool)

Source from the content-addressed store, hash-verified

993}
994
995func fieldToVariable(field protoreflect.FieldDescriptor, jsonFieldNames bool) (*decls.VariableDecl, error) {
996 name := string(field.Name())
997 if jsonFieldNames {
998 name = field.JSONName()
999 }
1000 if field.IsMap() {
1001 mapKey := field.MapKey()
1002 mapValue := field.MapValue()
1003 keyType, err := fieldToCELType(mapKey)
1004 if err != nil {
1005 return nil, err
1006 }
1007 valueType, err := fieldToCELType(mapValue)
1008 if err != nil {
1009 return nil, err
1010 }
1011 return decls.NewVariable(name, MapType(keyType, valueType)), nil
1012 }
1013 if field.IsList() {
1014 elemType, err := fieldToCELType(field)
1015 if err != nil {
1016 return nil, err
1017 }
1018 return decls.NewVariable(name, ListType(elemType)), nil
1019 }
1020 celType, err := fieldToCELType(field)
1021 if err != nil {
1022 return nil, err
1023 }
1024 return decls.NewVariable(name, celType), nil
1025}
1026
1027// DeclareContextProto returns an option to extend CEL environment with declarations from the given context proto.
1028// Each field of the proto defines a variable of the same name in the environment.

Callers 2

ToConfigMethod · 0.85
DeclareContextProtoFunction · 0.85

Calls 6

NewVariableFunction · 0.92
fieldToCELTypeFunction · 0.85
JSONNameMethod · 0.80
NameMethod · 0.65
IsMapMethod · 0.45
IsListMethod · 0.45

Tested by

no test coverage detected