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

Function fieldToVariable

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

Source from the content-addressed store, hash-verified

789}
790
791func fieldToVariable(field protoreflect.FieldDescriptor, jsonFieldNames bool) (*decls.VariableDecl, error) {
792 name := string(field.Name())
793 if jsonFieldNames {
794 name = field.JSONName()
795 }
796 if field.IsMap() {
797 mapKey := field.MapKey()
798 mapValue := field.MapValue()
799 keyType, err := fieldToCELType(mapKey)
800 if err != nil {
801 return nil, err
802 }
803 valueType, err := fieldToCELType(mapValue)
804 if err != nil {
805 return nil, err
806 }
807 return decls.NewVariable(name, MapType(keyType, valueType)), nil
808 }
809 if field.IsList() {
810 elemType, err := fieldToCELType(field)
811 if err != nil {
812 return nil, err
813 }
814 return decls.NewVariable(name, ListType(elemType)), nil
815 }
816 celType, err := fieldToCELType(field)
817 if err != nil {
818 return nil, err
819 }
820 return decls.NewVariable(name, celType), nil
821}
822
823// DeclareContextProto returns an option to extend CEL environment with declarations from the given context proto.
824// 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