pb

package
v0.4.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2020 License: Apache-2.0 Imports: 17 Imported by: 12

Documentation

Overview

Package pb reflects over protocol buffer descriptors to generate objects that simplify type, enum, and field lookup.

Index

Constants

This section is empty.

Variables

View Source
var (
	// CheckedPrimitives map from proto field descriptor type to expr.Type.
	CheckedPrimitives = map[descpb.FieldDescriptorProto_Type]*exprpb.Type{
		descpb.FieldDescriptorProto_TYPE_BOOL:     checkedBool,
		descpb.FieldDescriptorProto_TYPE_BYTES:    checkedBytes,
		descpb.FieldDescriptorProto_TYPE_DOUBLE:   checkedDouble,
		descpb.FieldDescriptorProto_TYPE_FLOAT:    checkedDouble,
		descpb.FieldDescriptorProto_TYPE_INT32:    checkedInt,
		descpb.FieldDescriptorProto_TYPE_INT64:    checkedInt,
		descpb.FieldDescriptorProto_TYPE_SINT32:   checkedInt,
		descpb.FieldDescriptorProto_TYPE_SINT64:   checkedInt,
		descpb.FieldDescriptorProto_TYPE_UINT32:   checkedUint,
		descpb.FieldDescriptorProto_TYPE_UINT64:   checkedUint,
		descpb.FieldDescriptorProto_TYPE_FIXED32:  checkedUint,
		descpb.FieldDescriptorProto_TYPE_FIXED64:  checkedUint,
		descpb.FieldDescriptorProto_TYPE_SFIXED32: checkedInt,
		descpb.FieldDescriptorProto_TYPE_SFIXED64: checkedInt,
		descpb.FieldDescriptorProto_TYPE_STRING:   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,
	}
)
View Source
var (
	// DefaultDb used at evaluation time or unless overridden at check time.
	DefaultDb = &Db{
		revFileDescriptorMap: make(map[string]*FileDescription),
	}
)

Functions

func CollectFileDescriptorSet added in v0.4.0

func CollectFileDescriptorSet(message proto.Message) (*descpb.FileDescriptorSet, error)

CollectFileDescriptorSet builds a file descriptor set associated with the file where the input message is declared.

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.

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) DescribeEnum added in v0.2.0

func (pbdb *Db) DescribeEnum(enumName string) (*EnumDescription, error)

DescribeEnum takes a qualified enum name and returns an `EnumDescription` if it exists in the `pb.Db`.

func (*Db) DescribeFile added in v0.2.0

func (pbdb *Db) DescribeFile(message proto.Message) (*FileDescription, error)

DescribeFile gets the `FileDescription` for the `message` type if it exists in the `pb.Db`.

func (*Db) DescribeType added in v0.2.0

func (pbdb *Db) DescribeType(typeName string) (*TypeDescription, error)

DescribeType returns a `TypeDescription` for the `typeName` if it exists in the `pb.Db`.

func (*Db) RegisterDescriptor added in v0.2.0

func (pbdb *Db) RegisterDescriptor(fileDesc *descpb.FileDescriptorProto) (*FileDescription, error)

RegisterDescriptor produces a `FileDescription` from a `FileDescriptorProto` 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 EnumDescription

type EnumDescription struct {
	// contains filtered or unexported fields
}

EnumDescription maps a qualified enum name to its numeric value.

func (*EnumDescription) Name

func (ed *EnumDescription) Name() string

Name of the enum.

func (*EnumDescription) Value

func (ed *EnumDescription) Value() int32

Value (numeric) of the enum.

type FieldDescription

type FieldDescription struct {
	// 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) GetFrom added in v0.4.0

func (fd *FieldDescription) GetFrom(target interface{}) (interface{}, 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.

The input target may either be a reflect.Value or Go struct type.

func (*FieldDescription) Index

func (fd *FieldDescription) Index() int

Index returns the field index within a reflected value.

func (*FieldDescription) IsEnum

func (fd *FieldDescription) IsEnum() bool

IsEnum returns true if the field type refers to an enum value.

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) IsRepeated

func (fd *FieldDescription) IsRepeated() bool

IsRepeated 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) IsSet added in v0.4.0

func (fd *FieldDescription) IsSet(target interface{}) bool

IsSet returns whether the field is set on the target value, per the proto presence conventions of proto2 or proto3 accordingly.

The input target may either be a reflect.Value or Go struct type.

func (*FieldDescription) IsWrapper added in v0.3.0

func (fd *FieldDescription) IsWrapper() bool

IsWrapper returns true if the field type is a primitive wrapper type.

func (*FieldDescription) Name

func (fd *FieldDescription) Name() string

Name returns the CamelCase name of the field within the proto-based struct.

func (*FieldDescription) OneofType

func (fd *FieldDescription) OneofType() reflect.Type

OneofType returns the reflect.Type value of a oneof field.

Oneof field values are wrapped in a struct which contains one field whose value is a proto.Message.

func (*FieldDescription) OrigName

func (fd *FieldDescription) OrigName() string

OrigName returns the snake_case name of the field as it was declared within the proto. This is the same name format that is expected within expressions.

func (*FieldDescription) String

func (fd *FieldDescription) String() string

String returns a struct-like field definition string.

func (*FieldDescription) SupportsPresence

func (fd *FieldDescription) SupportsPresence() bool

SupportsPresence returns true if the field supports presence detection.

func (*FieldDescription) Type added in v0.3.0

func (fd *FieldDescription) Type() *TypeDescription

Type returns the TypeDescription for the field.

func (*FieldDescription) TypeName

func (fd *FieldDescription) TypeName() string

TypeName returns the type name of the field.

type FileDescription

type FileDescription struct {
	// contains filtered or unexported fields
}

FileDescription holds a map of all types and enums declared within a .proto file.

func (*FileDescription) GetEnumDescription

func (fd *FileDescription) GetEnumDescription(enumName string) (*EnumDescription, error)

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) GetTypeDescription

func (fd *FileDescription) GetTypeDescription(typeName string) (*TypeDescription, error)

GetTypeDescription returns a TypeDescription for a qualified 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.

func (*FileDescription) Package

func (fd *FileDescription) Package() string

Package returns the file's qualified package name.

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) DefaultValue added in v0.3.0

func (td *TypeDescription) DefaultValue() proto.Message

DefaultValue returns an empty instance of the proto message associated with the type, or nil for wrapper types.

func (*TypeDescription) FieldByName

func (td *TypeDescription) FieldByName(name string) (*FieldDescription, bool)

FieldByName returns the FieldDescription associated with a field name.

func (*TypeDescription) FieldCount

func (td *TypeDescription) FieldCount() int

FieldCount returns the number of fields declared within the type.

func (*TypeDescription) Name

func (td *TypeDescription) Name() string

Name of the type.

func (*TypeDescription) ReflectType

func (td *TypeDescription) ReflectType() reflect.Type

ReflectType returns the reflected struct type of the generated proto struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL