protoops

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMismatchingType = errors.New("mismatching type")
)

Functions

func AppendListItem

func AppendListItem(li List, new any) error

AppendListItem appends the provided value to end of the provided list. It may panic if the provided list is invalid or is read only. It returns ErrMismatchingType error when value type do not matches any of types that would allow appending.

func AppendMessageFieldListItem

func AppendMessageFieldListItem(parentMessage protoreflect.Message, listField protoreflect.FieldDescriptor, new any) error

AppendMessageFieldListItem appends the provided value to end of the list value from a list field within the given message. It may panic if the provided parent message is invalid, the list field descriptor do not belongs to the message. It returns ErrMismatchingType error when value type do not matches any of types that would allow appending.

func AreProtoFieldsMatch

func AreProtoFieldsMatch(x, y protoreflect.FieldDescriptor) bool

AreProtoFieldsMatch reports if tow provided protoreflect.FieldDescriptors match. If the field descriptors match, any of their value can be assigned to any of the fields using standard proto and protopatch rules for assignment.

func AreValueTypesMatch

func AreValueTypesMatch(x, y reflect.Value) bool

AreValueTypesMatch reports if types of two provided values match or if one can be set from the other using standard proto and protopatch rules for assignment.

func FieldDescriptorInMessageDescriptor

func FieldDescriptorInMessageDescriptor(desc protoreflect.MessageDescriptor, name string) protoreflect.FieldDescriptor

FieldDescriptorInMessageDescriptor parses the provided name as field name. It returns nil field descriptor if the associated field cannot be found.

func InsertListItem

func InsertListItem(li List, index int, new any) error

InsertListItem insert the provided value at specified index into the provided list, by expanding list, moving necessary items and setting value under the specified index according to proto and protopatch assignment rules. It may panic if the provided list is invalid or is read only or the given index is out of bounds of the list. It returns ErrMismatchingType error when value type do not matches any of types that would allow appending. Note thet when the index is exactly equal to the length of the list, insert behaves like append and do not panics due to out of bound index.

func InsertMessageFieldListItem

func InsertMessageFieldListItem(parentMessage protoreflect.Message, listField protoreflect.FieldDescriptor, index int, new any) error

InsertMessageFieldListItem insert the provided value at specified index into the list value inside a list field within the given message, by expanding list, moving necessary items and setting value under the specified index according to proto and protopatch assignment rules. It may panic if the provided parent message is invalid, the list field descriptor do not belongs to the message or the given index is out of bounds of the list. It returns ErrMismatchingType error when value type do not matches any of types that would allow appending. Note thet when the index is exactly equal to the length of the list, insert behaves like append and do not panics due to out of bound index.

func InterfaceOfListItem

func InterfaceOfListItem(listField protoreflect.FieldDescriptor, value protoreflect.Value) any

InterfaceOfListItem returns an interface value associated with the given list item value. It may panic or return invalid result if the field descriptor do not describes the list that the given item value belongs to. For scalar types and messages it returns its value.

func InterfaceOfMapItem

func InterfaceOfMapItem(mapField protoreflect.FieldDescriptor, value protoreflect.Value) any

InterfaceOfMapItem returns an interface value associated with the given map item value. It may panic or return invalid result if the field descriptor do not describes the map that the given item value belongs to. For scalar types and messages it returns its value.

func InterfaceOfMessageField

func InterfaceOfMessageField(field protoreflect.FieldDescriptor, value protoreflect.Value) any

InterfaceOfMessageField returns an interface value associated with the given field value. It may panic or return invalid result if the field descriptor do not describes the provided value. For scalar types and messages it returns its value. For lists and maps it returns List and Map interfaces accordingly.

func InterfaceValue

func InterfaceValue(i any) protoreflect.Value

InterfaceValue returns an protoreflect.Value associated with the given list item interface value. It may panic or return invalid result if the provided value cannot be represented as protoreflect.Value.

func IsTypeMapOfProtoMessageInterfaces

func IsTypeMapOfProtoMessageInterfaces(t reflect.Type) bool

IsTypeMapOfProtoMessageInterfaces reports weather the provided type is a map of proto message interfaces, that means it is map[K]proto.Message or map[K]protoreflace.Message where K is any valid map key type.

func IsTypeMatchesProtoScalarKind

func IsTypeMatchesProtoScalarKind(kind protoreflect.Kind, t reflect.Type) bool

IsTypeMatchesProtoScalarKind reports is the provided protoreflect.Kind matches the given type. Check is only valid for scalar protoreflect types, that is: protoreflect.BoolKind, protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind, protoreflect.EnumKind, protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind, protoreflect.Uint32Kind, protoreflect.Fixed32Kind, protoreflect.Uint64Kind, protoreflect.Fixed64Kind, protoreflect.FloatKind, protoreflect.DoubleKind, protoreflect.StringKind, protoreflect.BytesKind.

func IsTypeSliceOfProtoMessageInterfaces

func IsTypeSliceOfProtoMessageInterfaces(t reflect.Type) bool

IsTypeSliceOfProtoMessageInterfaces reports weather the provided type is a slice of proto message interfaces, that means it is []proto.Message or []protoreflace.Message.

func IsValueTypeMatchesProtoField

func IsValueTypeMatchesProtoField(field protoreflect.FieldDescriptor, v reflect.Value) bool

IsValueTypeMatchesProtoField reports if type of the provided value matches the given protoreflect.FieldDescriptor. If the type matches it can be assigned to field using standard proto and protopatch rules for assignment.

func MessageDescriptorFromType

func MessageDescriptorFromType(t reflect.Type) protoreflect.MessageDescriptor

MessageDescriptorFromType retrieves protoreflect.MessageDescriptor if the provided type contains an implementation of proto.Message or protoreflect.Message. Otherwise it returns nil.

func MessageDescriptorFromValue

func MessageDescriptorFromValue(v reflect.Value) protoreflect.MessageDescriptor

MessageDescriptorFromType retrieves protoreflect.MessageDescriptor if the provided value contains an implementation of proto.Message or protoreflect.Message. Otherwise it returns nil.

func MessageDescriptorFromValueOfMapOfProtoMessages

func MessageDescriptorFromValueOfMapOfProtoMessages(v reflect.Value) protoreflect.MessageDescriptor

MessageDescriptorFromValueOfMapOfProtoMessages returns common message descriptor form the provided map of proto message. If the given value does not contain a slice of proto messages or proto messages interfaces, if the there is more than one descriptor or if descriptor cannot be determined, function returns nil.

func MessageDescriptorFromValueOfSliceOfProtoMessages

func MessageDescriptorFromValueOfSliceOfProtoMessages(v reflect.Value) protoreflect.MessageDescriptor

MessageDescriptorFromValueOfSliceOfProtoMessages returns common message descriptor form the provided slice of proto message. If the given value does not contain a slice of proto messages or proto messages interfaces, if the there is more than one descriptor or if descriptor cannot be determined, function returns nil.

func NewProtoreflectOfMessage

func NewProtoreflectOfMessage(m proto.Message) protoreflect.Message

NewProtoreflectOfMessage returns new mutable protoreflect.Message value having a type like the provided proto.Message or nil if the message is invalid.

func ParseListIndex

func ParseListIndex(name string) (int, bool)

ParseListIndex parses the provided name as list index. It returns o and false if the provided name cannot be parsed. Note, the index can be negative, indicating reverse list counting.

func ParseMapKey

func ParseMapKey(keyType protoreflect.FieldDescriptor, name string) protoreflect.MapKey

ParseMapKey parses the provided name as map key. It returns invalid map key if the provided name cannot be parsed.

func ParsedIndexInList

func ParsedIndexInList(len int, name string) int

ParsedIndexInList parses the provided name as list index based on the given list length. It returns -1 if the provided name cannot be parsed or index is outside the bounds of list. Otherwise the function never returns negative indexes as negative indexes are normalized.

func ParsedKeyInMap

func ParsedKeyInMap(m protoreflect.Map, keyType protoreflect.FieldDescriptor, name string) protoreflect.MapKey

ParsedKeyInMap parses the provided name as map key. It returns invalid map key if the provided name cannot be parsed or was not found in map.

func ProtoreflectOfAny

func ProtoreflectOfAny(v any) protoreflect.Message

ProtoreflectOfAny returns protoreflect.Message value of the provided proto.Message or protoreflect.Message. If the provided value does not contains a valid proto.Message or protoreflect.Message function returns nil.

func ProtoreflectOfMessage

func ProtoreflectOfMessage(m proto.Message) protoreflect.Message

ProtoreflectOfMessage returns protoreflect.Message value of the provided proto.Message or nil if the message is invalid.

func SetListItem

func SetListItem(li List, index int, to any) error

SetListItem assigns the provided value to the list index inside the given list, by setting value under the specified index according to proto and protopatch assignment rules. It may panic if the provided list is invalid or is read only, the given index is out of bounds of the list. It returns ErrMismatchingType error when value type do not matches any of types that allows assignment.

func SetMapItem

func SetMapItem(ma Map, key protoreflect.MapKey, to any) error

SetMapItem assigns the provided value to the map key inside the given map, by setting value under the specified map key according to proto and protopatch assignment rules. It may panic if the provided map is invalid or read only, map key is invalid or its type do not matches map key type. It returns ErrMismatchingType error when value type do not matches any of types that allows assignment.

func SetMessageField

func SetMessageField(m protoreflect.Message, field protoreflect.FieldDescriptor, to any) error

SetMessageField assigns the provided value to message field, by setting field value according to proto and protopatch assignment rules. It may panic if the provided message is invalid or the field descriptor do not belongs to the message. It returns ErrMismatchingType error when value type do not matches any of types that allows assignment.

func SetMessageFieldListItem

func SetMessageFieldListItem(parentMessage protoreflect.Message, listField protoreflect.FieldDescriptor, index int, to any) error

SetMessageFieldListItem assigns the provided value to the list index inside a list field within the given message, by setting value under the specified index according to proto and protopatch assignment rules. It may panic if the provided parent message is invalid, the list field descriptor do not belongs to the message, list is not mutable or the given index is out of bounds of the list. It returns ErrMismatchingType error when value type do not matches any of types that allows assignment.

func SetMessageFieldMapItem

func SetMessageFieldMapItem(parentMessage protoreflect.Message, mapField protoreflect.FieldDescriptor, key protoreflect.MapKey, to any) error

SetMessageFieldMapItem assigns the provided value to the map key inside a map field within the given message, by setting value under the specified map key according to proto and protopatch assignment rules. It may panic if the provided parent message is invalid, the map field descriptor does not describes a map field or does not belongs to the message, map key is invalid or its type do not matches map key type. It returns ErrMismatchingType error when value type do not matches any of types that allows assignment.

Types

type List

type List interface {
	protoreflect.List
	Iter() iter.Seq2[int, protoreflect.Value]
	ParentFieldDescriptor() protoreflect.FieldDescriptor
	AsGoSlice() any
}

List represents a protocol buffer list value.

func NewList

func NewList(parentField protoreflect.FieldDescriptor, li protoreflect.List) List

type Map

type Map interface {
	protoreflect.Map
	Iter() iter.Seq2[protoreflect.MapKey, protoreflect.Value]
	ParentFieldDescriptor() protoreflect.FieldDescriptor
	AsGoMap() any
}

Map represents a protocol buffer map value.

func NewMap

func NewMap(parentField protoreflect.FieldDescriptor, ma protoreflect.Map) Map

Jump to

Keyboard shortcuts

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