xproto

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InternalProtoPackage = "internal"
	MetabookFullName     = "internal.Metabook"
)

Variables

View Source
var DefaultBoolValue pref.Value
View Source
var DefaultBytesValue pref.Value
View Source
var DefaultComparatorValue pref.Value
View Source
var DefaultDurationValue pref.Value
View Source
var DefaultEnumValue pref.Value
View Source
var DefaultFloat32Value pref.Value
View Source
var DefaultFloat64Value pref.Value
View Source
var DefaultFractionValue pref.Value
View Source
var DefaultInt32Value pref.Value
View Source
var DefaultInt64Value pref.Value
View Source
var DefaultStringValue pref.Value
View Source
var DefaultTimestampValue pref.Value
View Source
var DefaultUint32Value pref.Value
View Source
var DefaultUint64Value pref.Value
View Source
var ErrDuplicateKey = fmt.Errorf("duplicate key")

Functions

func CheckMapDuplicateKey

func CheckMapDuplicateKey(dst, src proto.Message) error

CheckMapDuplicateKey checks the map field's duplicate key in message with the same descriptor.

func GetFieldDefaultValue

func GetFieldDefaultValue(fd pref.FieldDescriptor) string

func GetFieldTypeName

func GetFieldTypeName(fd protoreflect.FieldDescriptor) string

GetFieldTypeName parses and returns correct field type name in desired format from field descriptor.

The desired formats are:

  • map<KeyType, ValueType>
  • repeated ElemType
  • MessageType
  • EnumType
  • ScalarType

func GetOneofFieldByNumber

func GetOneofFieldByNumber(od protoreflect.OneofDescriptor, n int32) protoreflect.FieldDescriptor

GetOneofFieldByNumber returns the FieldDescriptor for a field numbered n. It returns nil if not found.

func IsUnion

func IsUnionField

func IsUnionField(fd protoreflect.FieldDescriptor) bool

func Merge

func Merge(dst, src proto.Message) error

Merge merges src into dst, which must be a message with the same descriptor.

NOTE: message should only has two kinds of field:

  1. list
  2. map: src should not has duplicate key in dst

func NewFiles

func NewFiles(protoPaths []string, protoFiles []string, excludeProtoFiles ...string) (*protoregistry.Files, error)

NewFiles creates a new protoregistry.Files from the proto paths and proto Gob filenames.

func ParseFieldValue

func ParseFieldValue(fd pref.FieldDescriptor, rawValue string, locationName string) (v pref.Value, present bool, err error)

ParseFieldValue parses field value by FieldDescriptor. It can parse following basic types:

Scalar types

  • Numbers: int32, uint32, int64, uint64, float, double
  • Booleans: bool
  • Strings: string
  • Bytes: bytes

Enum type

Well-known types

  • "google.protobuf.Timestamp": datetime, date, time
  • "google.protobuf.Duration": duration

func ParseProtos

func ParseProtos(protoPaths []string, protoFiles ...string) (*protoregistry.Files, error)

ParseProtos parses the proto paths and proto files to desc.FileDescriptor slices.

func PatchMessage

func PatchMessage(dst, src proto.Message) error

PatchMessage patches src into dst, which must be a message with the same descriptor.

Default PatchMessage mechanism

  • scalar: Populated scalar fields in src are copied to dst.
  • message: Populated singular messages in src are merged into dst by recursively calling xproto.PatchMessage, or replace dst message if "PATCH_REPLACE" is specified for this field.
  • list: The elements of every list field in src are appended to the corresponded list fields in dst, or replace dst list if "PATCH_REPLACE" is specified for this field.
  • map: The entries of every map field in src are MERGED (different from the behavior of proto.Merge) into the corresponding map field in dst, or replace dst map if "PATCH_REPLACE" is specified for this field.
  • unknown: The unknown fields of src are appended to the unknown fields of dst (TODO: untested).

Types

type EnumCache

type EnumCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*EnumCache) GetValueByAlias

func (ec *EnumCache) GetValueByAlias(ed pref.EnumDescriptor, valueAlias string) (pref.Value, error)

type TypeInfo

type TypeInfo struct {
	FullName       protoreflect.FullName
	ParentFilename string
	Kind           types.Kind

	FirstFieldOptionName string // only for MessageKind
}

type TypeInfos

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

func GetAllTypeInfo

func GetAllTypeInfo(files *protoregistry.Files, protoPackage string) *TypeInfos

func NewTypeInfos

func NewTypeInfos(protoPackage string) *TypeInfos

func (*TypeInfos) Get

func (x *TypeInfos) Get(name string) *TypeInfo

Get retrieves type info by name in proto package.

NOTE: if name is prefixed with ".", then default proto package name will be prepended to generate full name. For example: ".ItemType" will be conveted to "<ProtoPackage>.ItemType"

func (*TypeInfos) GetByFullName

func (x *TypeInfos) GetByFullName(fullName protoreflect.FullName) *TypeInfo

GetByFullName retrieves type info by type's full name.

func (*TypeInfos) Put

func (x *TypeInfos) Put(info *TypeInfo)

Put stores a new type info.

type UnionDescriptor

type UnionDescriptor struct {
	Type  protoreflect.FieldDescriptor
	Value protoreflect.OneofDescriptor
}

func (UnionDescriptor) GetValueByNumber

func (u UnionDescriptor) GetValueByNumber(n int32) protoreflect.FieldDescriptor

GetValueByNumber returns the FieldDescriptor for a field numbered n. It returns nil if not found.

func (UnionDescriptor) TypeName

func (u UnionDescriptor) TypeName() string

TypeName returns the type field name. It returns CameCase style of proto field name if not set explicitly in field extension.

func (UnionDescriptor) ValueFieldName

func (u UnionDescriptor) ValueFieldName() string

ValueFieldName returns the value field name. It returns "Field" if not set explicitly in oneof extension.

Jump to

Keyboard shortcuts

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