Documentation ¶
Overview ¶
Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.
Index ¶
- Variables
- func CollectFileDescriptorSet(message proto.Message) map[string]protoreflect.FileDescriptor
- func Equal(x, y proto.Message) bool
- func Merge(dstPB, srcPB proto.Message) error
- type Db
- func (pbdb *Db) Copy() *Db
- func (pbdb *Db) DescribeEnum(enumName string) (*EnumValueDescription, bool)
- func (pbdb *Db) DescribeType(typeName string) (*TypeDescription, bool)
- func (pbdb *Db) FileDescriptions() []*FileDescription
- func (pbdb *Db) RegisterDescriptor(fileDesc protoreflect.FileDescriptor) (*FileDescription, error)
- func (pbdb *Db) RegisterMessage(message proto.Message) (*FileDescription, error)
- type EnumValueDescription
- type FieldDescription
- func (fd *FieldDescription) CheckedType() *exprpb.Type
- func (fd *FieldDescription) Descriptor() protoreflect.FieldDescriptor
- func (fd *FieldDescription) GetFrom(target any) (any, error)
- func (fd *FieldDescription) IsEnum() bool
- func (fd *FieldDescription) IsList() bool
- func (fd *FieldDescription) IsMap() bool
- func (fd *FieldDescription) IsMessage() bool
- func (fd *FieldDescription) IsOneof() bool
- func (fd *FieldDescription) IsSet(target any) bool
- func (fd *FieldDescription) MaybeUnwrapDynamic(msg protoreflect.Message) (any, bool, error)
- func (fd *FieldDescription) Name() string
- func (fd *FieldDescription) ProtoKind() protoreflect.Kind
- func (fd *FieldDescription) ReflectType() reflect.Type
- func (fd *FieldDescription) String() string
- func (fd *FieldDescription) Zero() proto.Message
- type FileDescription
- func (fd *FileDescription) Copy(pbdb *Db) *FileDescription
- func (fd *FileDescription) GetEnumDescription(enumName string) (*EnumValueDescription, bool)
- func (fd *FileDescription) GetEnumNames() []string
- func (fd *FileDescription) GetName() string
- func (fd *FileDescription) GetTypeDescription(typeName string) (*TypeDescription, bool)
- func (fd *FileDescription) GetTypeNames() []string
- type Map
- type TypeDescription
- func (td *TypeDescription) Copy(pbdb *Db) *TypeDescription
- func (td *TypeDescription) FieldByName(name string) (*FieldDescription, bool)
- func (td *TypeDescription) FieldMap() map[string]*FieldDescription
- func (td *TypeDescription) MaybeUnwrap(msg proto.Message) (any, bool, error)
- func (td *TypeDescription) Name() string
- func (td *TypeDescription) New() protoreflect.Message
- func (td *TypeDescription) ReflectType() reflect.Type
- func (td *TypeDescription) Zero() proto.Message
Constants ¶
This section is empty.
Variables ¶
var ( // CheckedPrimitives map from proto field descriptor type to expr.Type. CheckedPrimitives = map[protoreflect.Kind]*exprpb.Type{ protoreflect.BoolKind: checkedBool, protoreflect.BytesKind: checkedBytes, protoreflect.DoubleKind: checkedDouble, protoreflect.FloatKind: checkedDouble, protoreflect.Int32Kind: checkedInt, protoreflect.Int64Kind: checkedInt, protoreflect.Sint32Kind: checkedInt, protoreflect.Sint64Kind: checkedInt, protoreflect.Uint32Kind: checkedUint, protoreflect.Uint64Kind: checkedUint, protoreflect.Fixed32Kind: checkedUint, protoreflect.Fixed64Kind: checkedUint, protoreflect.Sfixed32Kind: checkedInt, protoreflect.Sfixed64Kind: checkedInt, protoreflect.StringKind: checkedString} // CheckedWellKnowns map from qualified proto type name to expr.Type for // well-known proto types. CheckedWellKnowns = map[string]*exprpb.Type{ "google.protobuf.BoolValue": checkedWrap(checkedBool), "google.protobuf.BytesValue": checkedWrap(checkedBytes), "google.protobuf.DoubleValue": checkedWrap(checkedDouble), "google.protobuf.FloatValue": checkedWrap(checkedDouble), "google.protobuf.Int64Value": checkedWrap(checkedInt), "google.protobuf.Int32Value": checkedWrap(checkedInt), "google.protobuf.UInt64Value": checkedWrap(checkedUint), "google.protobuf.UInt32Value": checkedWrap(checkedUint), "google.protobuf.StringValue": checkedWrap(checkedString), "google.protobuf.Any": checkedAny, "google.protobuf.Duration": checkedDuration, "google.protobuf.Timestamp": checkedTimestamp, "google.protobuf.ListValue": checkedListDyn, "google.protobuf.NullValue": checkedNull, "google.protobuf.Struct": checkedMapStringDyn, "google.protobuf.Value": checkedDyn, } )
var ( // DefaultDb used at evaluation time or unless overridden at check time. DefaultDb = &Db{ revFileDescriptorMap: make(map[string]*FileDescription), files: []*FileDescription{}, extensions: make(extensionMap), } )
Functions ¶
func CollectFileDescriptorSet ¶ added in v0.4.0
func CollectFileDescriptorSet(message proto.Message) map[string]protoreflect.FileDescriptor
CollectFileDescriptorSet builds a file descriptor set associated with the file where the input message is declared.
func Equal ¶ added in v0.10.0
Equal returns whether two proto.Message instances are equal using the following criteria:
- Messages must share the same instance of the type descriptor
- Known set fields are compared using semantics equality
- Bytes are compared using bytes.Equal
- Scalar values are compared with operator ==
- List and map types are equal if they have the same length and all elements are equal
- Messages are equal if they share the same descriptor and all set fields are equal
- Unknown fields are compared using byte equality
- NaN values are not equal to each other
- google.protobuf.Any values are unpacked before comparison
- If the type descriptor for a protobuf.Any cannot be found, byte equality is used rather than semantic equality.
This method of proto equality mirrors the behavior of the C++ protobuf MessageDifferencer whereas the golang proto.Equal implementation mirrors the Java protobuf equals() methods behaviors which needed to treat NaN values as equal due to Java semantics.
Types ¶
type Db ¶ added in v0.2.0
type Db struct {
// contains filtered or unexported fields
}
Db maps from file / message / enum name to file description.
Each Db is isolated from each other, and while information about protobuf descriptors may be fetched from the global protobuf registry, no descriptors are added to this registry, else the isolation guarantees of the Db object would be violated.
func NewDb ¶ added in v0.2.0
func NewDb() *Db
NewDb creates a new `pb.Db` with an empty type name to file description map.
func (*Db) Copy ¶ added in v0.4.2
Copy creates a copy of the current database with its own internal descriptor mapping.
func (*Db) DescribeEnum ¶ added in v0.2.0
func (pbdb *Db) DescribeEnum(enumName string) (*EnumValueDescription, bool)
DescribeEnum takes a qualified enum name and returns an `EnumDescription` if it exists in the `pb.Db`.
func (*Db) DescribeType ¶ added in v0.2.0
func (pbdb *Db) DescribeType(typeName string) (*TypeDescription, bool)
DescribeType returns a `TypeDescription` for the `typeName` if it exists in the `pb.Db`.
func (*Db) FileDescriptions ¶ added in v0.7.0
func (pbdb *Db) FileDescriptions() []*FileDescription
FileDescriptions returns the set of file descriptions associated with this db.
func (*Db) RegisterDescriptor ¶ added in v0.2.0
func (pbdb *Db) RegisterDescriptor(fileDesc protoreflect.FileDescriptor) (*FileDescription, error)
RegisterDescriptor produces a `FileDescription` from a `FileDescriptor` and registers the message and enum types into the `pb.Db`.
func (*Db) RegisterMessage ¶ added in v0.2.0
func (pbdb *Db) RegisterMessage(message proto.Message) (*FileDescription, error)
RegisterMessage produces a `FileDescription` from a `message` and registers the message and all other definitions within the message file into the `pb.Db`.
type EnumValueDescription ¶ added in v0.4.1
type EnumValueDescription struct {
// contains filtered or unexported fields
}
EnumValueDescription maps a fully-qualified enum value name to its numeric value.
func (*EnumValueDescription) Name ¶ added in v0.4.1
func (ed *EnumValueDescription) Name() string
Name returns the fully-qualified identifier name for the enum value.
func (*EnumValueDescription) Value ¶ added in v0.4.1
func (ed *EnumValueDescription) Value() int32
Value returns the (numeric) value of the enum.
type FieldDescription ¶
type FieldDescription struct { // KeyType holds the key FieldDescription for map fields. KeyType *FieldDescription // ValueType holds the value FieldDescription for map fields. ValueType *FieldDescription // contains filtered or unexported fields }
FieldDescription holds metadata related to fields declared within a type.
func (*FieldDescription) CheckedType ¶
func (fd *FieldDescription) CheckedType() *exprpb.Type
CheckedType returns the type-definition used at type-check time.
func (*FieldDescription) Descriptor ¶ added in v0.7.0
func (fd *FieldDescription) Descriptor() protoreflect.FieldDescriptor
Descriptor returns the protoreflect.FieldDescriptor for this type.
func (*FieldDescription) GetFrom ¶ added in v0.4.0
func (fd *FieldDescription) GetFrom(target any) (any, error)
GetFrom returns the accessor method associated with the field on the proto generated struct.
If the field is not set, the proto default value is returned instead.
This function implements the FieldType.GetFrom function contract which can be used to operate on more than just protobuf field accesses; however, the target here must be a protobuf.Message.
func (*FieldDescription) IsEnum ¶
func (fd *FieldDescription) IsEnum() bool
IsEnum returns true if the field type refers to an enum value.
func (*FieldDescription) IsList ¶ added in v0.7.0
func (fd *FieldDescription) IsList() bool
IsList returns true if the field is a repeated value.
This method will also return true for map values, so check whether the field is also a map.
func (*FieldDescription) IsMap ¶
func (fd *FieldDescription) IsMap() bool
IsMap returns true if the field is of map type.
func (*FieldDescription) IsMessage ¶
func (fd *FieldDescription) IsMessage() bool
IsMessage returns true if the field is of message type.
func (*FieldDescription) IsOneof ¶
func (fd *FieldDescription) IsOneof() bool
IsOneof returns true if the field is declared within a oneof block.
func (*FieldDescription) IsSet ¶ added in v0.4.0
func (fd *FieldDescription) IsSet(target any) bool
IsSet returns whether the field is set on the target value, per the proto presence conventions of proto2 or proto3 accordingly.
This function implements the FieldType.IsSet function contract which can be used to operate on more than just protobuf field accesses; however, the target here must be a protobuf.Message.
func (*FieldDescription) MaybeUnwrapDynamic ¶ added in v0.7.0
func (fd *FieldDescription) MaybeUnwrapDynamic(msg protoreflect.Message) (any, bool, error)
MaybeUnwrapDynamic takes the reflected protoreflect.Message and determines whether the value can be unwrapped to a more primitive CEL type.
This function returns the unwrapped value and 'true' on success, or the original value and 'false' otherwise.
func (*FieldDescription) Name ¶
func (fd *FieldDescription) Name() string
Name returns the CamelCase name of the field within the proto-based struct.
func (*FieldDescription) ProtoKind ¶ added in v0.17.0
func (fd *FieldDescription) ProtoKind() protoreflect.Kind
ProtoKind returns the protobuf reflected kind of the field.
func (*FieldDescription) ReflectType ¶ added in v0.7.0
func (fd *FieldDescription) ReflectType() reflect.Type
ReflectType returns the Golang reflect.Type for this field.
func (*FieldDescription) String ¶
func (fd *FieldDescription) String() string
String returns the fully qualified name of the field within its type as well as whether the field occurs within a oneof.
func (*FieldDescription) Zero ¶ added in v0.7.0
func (fd *FieldDescription) Zero() proto.Message
Zero returns the zero value for the protobuf message represented by this field.
If the field is not a proto.Message type, the zero value is nil.
type FileDescription ¶
type FileDescription struct {
// contains filtered or unexported fields
}
FileDescription holds a map of all types and enum values declared within a proto file.
func (*FileDescription) Copy ¶ added in v0.13.0
func (fd *FileDescription) Copy(pbdb *Db) *FileDescription
Copy creates a copy of the FileDescription with updated Db references within its types.
func (*FileDescription) GetEnumDescription ¶
func (fd *FileDescription) GetEnumDescription(enumName string) (*EnumValueDescription, bool)
GetEnumDescription returns an EnumDescription for a qualified enum value name declared within the .proto file.
func (*FileDescription) GetEnumNames ¶
func (fd *FileDescription) GetEnumNames() []string
GetEnumNames returns the string names of all enum values in the file.
func (*FileDescription) GetName ¶ added in v0.13.0
func (fd *FileDescription) GetName() string
GetName returns the fully qualified file path for the file.
func (*FileDescription) GetTypeDescription ¶
func (fd *FileDescription) GetTypeDescription(typeName string) (*TypeDescription, bool)
GetTypeDescription returns a TypeDescription for a qualified protobuf message type name declared within the .proto file.
func (*FileDescription) GetTypeNames ¶
func (fd *FileDescription) GetTypeNames() []string
GetTypeNames returns the list of all type names contained within the file.
type Map ¶ added in v0.7.0
type Map struct { protoreflect.Map KeyType *FieldDescription ValueType *FieldDescription }
Map wraps the protoreflect.Map object with a key and value FieldDescription for use in retrieving individual elements within CEL value data types.
type TypeDescription ¶
type TypeDescription struct {
// contains filtered or unexported fields
}
TypeDescription is a collection of type metadata relevant to expression checking and evaluation.
func (*TypeDescription) Copy ¶ added in v0.13.0
func (td *TypeDescription) Copy(pbdb *Db) *TypeDescription
Copy copies the type description with updated references to the Db.
func (*TypeDescription) FieldByName ¶
func (td *TypeDescription) FieldByName(name string) (*FieldDescription, bool)
FieldByName returns (FieldDescription, true) if the field name is declared within the type.
func (*TypeDescription) FieldMap ¶ added in v0.7.0
func (td *TypeDescription) FieldMap() map[string]*FieldDescription
FieldMap returns a string field name to FieldDescription map.
func (*TypeDescription) MaybeUnwrap ¶ added in v0.7.0
MaybeUnwrap accepts a proto message as input and unwraps it to a primitive CEL type if possible.
This method returns the unwrapped value and 'true', else the original value and 'false'.
func (*TypeDescription) Name ¶
func (td *TypeDescription) Name() string
Name returns the fully-qualified name of the type.
func (*TypeDescription) New ¶ added in v0.7.0
func (td *TypeDescription) New() protoreflect.Message
New returns a mutable proto message
func (*TypeDescription) ReflectType ¶
func (td *TypeDescription) ReflectType() reflect.Type
ReflectType returns the Golang reflect.Type for this type.
func (*TypeDescription) Zero ¶ added in v0.7.0
func (td *TypeDescription) Zero() proto.Message
Zero returns the zero proto.Message value for this type.