1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package proto
- import (
- "google.golang.org/protobuf/reflect/protoreflect"
- )
- func HasExtension(m Message, xt protoreflect.ExtensionType) bool {
-
- if m == nil {
- return false
- }
-
-
- if xt == nil || m.ProtoReflect().Descriptor() != xt.TypeDescriptor().ContainingMessage() {
- return false
- }
- return m.ProtoReflect().Has(xt.TypeDescriptor())
- }
- func ClearExtension(m Message, xt protoreflect.ExtensionType) {
- m.ProtoReflect().Clear(xt.TypeDescriptor())
- }
- func GetExtension(m Message, xt protoreflect.ExtensionType) interface{} {
-
- if m == nil {
- return xt.InterfaceOf(xt.Zero())
- }
- return xt.InterfaceOf(m.ProtoReflect().Get(xt.TypeDescriptor()))
- }
- func SetExtension(m Message, xt protoreflect.ExtensionType, v interface{}) {
- xd := xt.TypeDescriptor()
- pv := xt.ValueOf(v)
-
- isValid := true
- switch {
- case xd.IsList():
- isValid = pv.List().IsValid()
- case xd.IsMap():
- isValid = pv.Map().IsValid()
- case xd.Message() != nil:
- isValid = pv.Message().IsValid()
- }
- if !isValid {
- m.ProtoReflect().Clear(xd)
- return
- }
- m.ProtoReflect().Set(xd, pv)
- }
- func RangeExtensions(m Message, f func(protoreflect.ExtensionType, interface{}) bool) {
-
- if m == nil {
- return
- }
- m.ProtoReflect().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
- if fd.IsExtension() {
- xt := fd.(protoreflect.ExtensionTypeDescriptor).Type()
- vi := xt.InterfaceOf(v)
- return f(xt, vi)
- }
- return true
- })
- }
|