5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 03:31:37 +00:00
matterbridge/vendor/github.com/golang/protobuf/proto/properties.go

307 lines
8.9 KiB
Go
Raw Normal View History

2020-08-09 22:29:54 +00:00
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2017-06-21 23:00:27 +00:00
package proto
import (
"fmt"
"reflect"
"strconv"
"strings"
"sync"
2020-08-09 22:29:54 +00:00
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"
2017-06-21 23:00:27 +00:00
)
2020-08-09 22:29:54 +00:00
// StructProperties represents protocol buffer type information for a
// generated protobuf message in the open-struct API.
//
// Deprecated: Do not use.
2017-06-21 23:00:27 +00:00
type StructProperties struct {
2020-08-09 22:29:54 +00:00
// Prop are the properties for each field.
//
// Fields belonging to a oneof are stored in OneofTypes instead, with a
// single Properties representing the parent oneof held here.
//
// The order of Prop matches the order of fields in the Go struct.
// Struct fields that are not related to protobufs have a "XXX_" prefix
// in the Properties.Name and must be ignored by the user.
Prop []*Properties
2017-06-21 23:00:27 +00:00
// OneofTypes contains information about the oneof fields in this message.
2020-08-09 22:29:54 +00:00
// It is keyed by the protobuf field name.
2017-06-21 23:00:27 +00:00
OneofTypes map[string]*OneofProperties
}
2020-08-09 22:29:54 +00:00
// Properties represents the type information for a protobuf message field.
//
// Deprecated: Do not use.
2017-06-21 23:00:27 +00:00
type Properties struct {
2020-08-09 22:29:54 +00:00
// Name is a placeholder name with little meaningful semantic value.
// If the name has an "XXX_" prefix, the entire Properties must be ignored.
Name string
// OrigName is the protobuf field name or oneof name.
OrigName string
// JSONName is the JSON name for the protobuf field.
JSONName string
// Enum is a placeholder name for enums.
// For historical reasons, this is neither the Go name for the enum,
// nor the protobuf name for the enum.
Enum string // Deprecated: Do not use.
// Weak contains the full name of the weakly referenced message.
Weak string
// Wire is a string representation of the wire type.
Wire string
// WireType is the protobuf wire type for the field.
2017-06-21 23:00:27 +00:00
WireType int
2020-08-09 22:29:54 +00:00
// Tag is the protobuf field number.
Tag int
// Required reports whether this is a required field.
2017-06-21 23:00:27 +00:00
Required bool
2020-08-09 22:29:54 +00:00
// Optional reports whether this is a optional field.
2017-06-21 23:00:27 +00:00
Optional bool
2020-08-09 22:29:54 +00:00
// Repeated reports whether this is a repeated field.
2017-06-21 23:00:27 +00:00
Repeated bool
2020-08-09 22:29:54 +00:00
// Packed reports whether this is a packed repeated field of scalars.
Packed bool
// Proto3 reports whether this field operates under the proto3 syntax.
Proto3 bool
// Oneof reports whether this field belongs within a oneof.
Oneof bool
// Default is the default value in string form.
Default string
// HasDefault reports whether the field has a default value.
HasDefault bool
// MapKeyProp is the properties for the key field for a map field.
MapKeyProp *Properties
// MapValProp is the properties for the value field for a map field.
MapValProp *Properties
}
2019-02-21 19:28:13 +00:00
2020-08-09 22:29:54 +00:00
// OneofProperties represents the type information for a protobuf oneof.
//
// Deprecated: Do not use.
type OneofProperties struct {
// Type is a pointer to the generated wrapper type for the field value.
// This is nil for messages that are not in the open-struct API.
Type reflect.Type
// Field is the index into StructProperties.Prop for the containing oneof.
Field int
// Prop is the properties for the field.
Prop *Properties
2017-06-21 23:00:27 +00:00
}
// String formats the properties in the protobuf struct field tag style.
func (p *Properties) String() string {
s := p.Wire
2020-08-09 22:29:54 +00:00
s += "," + strconv.Itoa(p.Tag)
2017-06-21 23:00:27 +00:00
if p.Required {
s += ",req"
}
if p.Optional {
s += ",opt"
}
if p.Repeated {
s += ",rep"
}
if p.Packed {
s += ",packed"
}
s += ",name=" + p.OrigName
2020-08-09 22:29:54 +00:00
if p.JSONName != "" {
2017-06-21 23:00:27 +00:00
s += ",json=" + p.JSONName
}
2020-08-09 22:29:54 +00:00
if len(p.Enum) > 0 {
s += ",enum=" + p.Enum
}
if len(p.Weak) > 0 {
s += ",weak=" + p.Weak
}
if p.Proto3 {
2017-06-21 23:00:27 +00:00
s += ",proto3"
}
2020-08-09 22:29:54 +00:00
if p.Oneof {
2017-06-21 23:00:27 +00:00
s += ",oneof"
}
if p.HasDefault {
s += ",def=" + p.Default
}
return s
}
// Parse populates p by parsing a string in the protobuf struct field tag style.
2020-08-09 22:29:54 +00:00
func (p *Properties) Parse(tag string) {
// For example: "bytes,49,opt,name=foo,def=hello!"
for len(tag) > 0 {
i := strings.IndexByte(tag, ',')
if i < 0 {
i = len(tag)
}
switch s := tag[:i]; {
case strings.HasPrefix(s, "name="):
p.OrigName = s[len("name="):]
case strings.HasPrefix(s, "json="):
p.JSONName = s[len("json="):]
case strings.HasPrefix(s, "enum="):
p.Enum = s[len("enum="):]
case strings.HasPrefix(s, "weak="):
p.Weak = s[len("weak="):]
case strings.Trim(s, "0123456789") == "":
n, _ := strconv.ParseUint(s, 10, 32)
p.Tag = int(n)
case s == "opt":
2017-06-21 23:00:27 +00:00
p.Optional = true
2020-08-09 22:29:54 +00:00
case s == "req":
p.Required = true
case s == "rep":
2017-06-21 23:00:27 +00:00
p.Repeated = true
2020-08-09 22:29:54 +00:00
case s == "varint" || s == "zigzag32" || s == "zigzag64":
p.Wire = s
p.WireType = WireVarint
case s == "fixed32":
p.Wire = s
p.WireType = WireFixed32
case s == "fixed64":
p.Wire = s
p.WireType = WireFixed64
case s == "bytes":
p.Wire = s
p.WireType = WireBytes
case s == "group":
p.Wire = s
p.WireType = WireStartGroup
case s == "packed":
2017-06-21 23:00:27 +00:00
p.Packed = true
2020-08-09 22:29:54 +00:00
case s == "proto3":
p.Proto3 = true
case s == "oneof":
p.Oneof = true
case strings.HasPrefix(s, "def="):
// The default tag is special in that everything afterwards is the
// default regardless of the presence of commas.
2017-06-21 23:00:27 +00:00
p.HasDefault = true
2020-08-09 22:29:54 +00:00
p.Default, i = tag[len("def="):], len(tag)
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
tag = strings.TrimPrefix(tag[i:], ",")
2017-06-21 23:00:27 +00:00
}
}
// Init populates the properties from a protocol buffer struct tag.
2020-08-09 22:29:54 +00:00
//
// Deprecated: Do not use.
2017-06-21 23:00:27 +00:00
func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) {
p.Name = name
p.OrigName = name
if tag == "" {
return
}
p.Parse(tag)
2020-08-09 22:29:54 +00:00
if typ != nil && typ.Kind() == reflect.Map {
p.MapKeyProp = new(Properties)
p.MapKeyProp.Init(nil, "Key", f.Tag.Get("protobuf_key"), nil)
p.MapValProp = new(Properties)
p.MapValProp.Init(nil, "Value", f.Tag.Get("protobuf_val"), nil)
}
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
var propertiesCache sync.Map // map[reflect.Type]*StructProperties
2017-06-21 23:00:27 +00:00
2020-08-09 22:29:54 +00:00
// GetProperties returns the list of properties for the type represented by t,
// which must be a generated protocol buffer message in the open-struct API,
// where protobuf message fields are represented by exported Go struct fields.
//
// Deprecated: Use protobuf reflection instead.
2017-06-21 23:00:27 +00:00
func GetProperties(t reflect.Type) *StructProperties {
2020-08-09 22:29:54 +00:00
if p, ok := propertiesCache.Load(t); ok {
return p.(*StructProperties)
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
p, _ := propertiesCache.LoadOrStore(t, newProperties(t))
return p.(*StructProperties)
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
func newProperties(t reflect.Type) *StructProperties {
if t.Kind() != reflect.Struct {
panic(fmt.Sprintf("%v is not a generated message in the open-struct API", t))
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
var hasOneof bool
2017-06-21 23:00:27 +00:00
prop := new(StructProperties)
2020-08-09 22:29:54 +00:00
// Construct a list of properties for each field in the struct.
2017-06-21 23:00:27 +00:00
for i := 0; i < t.NumField(); i++ {
p := new(Properties)
2020-08-09 22:29:54 +00:00
f := t.Field(i)
tagField := f.Tag.Get("protobuf")
p.Init(f.Type, f.Name, tagField, &f)
2017-06-21 23:00:27 +00:00
2020-08-09 22:29:54 +00:00
tagOneof := f.Tag.Get("protobuf_oneof")
if tagOneof != "" {
hasOneof = true
p.OrigName = tagOneof
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
// Rename unrelated struct fields with the "XXX_" prefix since so much
// user code simply checks for this to exclude special fields.
if tagField == "" && tagOneof == "" && !strings.HasPrefix(p.Name, "XXX_") {
p.Name = "XXX_" + p.Name
p.OrigName = "XXX_" + p.OrigName
} else if p.Weak != "" {
p.Name = p.OrigName // avoid possible "XXX_" prefix on weak field
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
prop.Prop = append(prop.Prop, p)
2017-06-21 23:00:27 +00:00
}
2020-08-09 22:29:54 +00:00
// Construct a mapping of oneof field names to properties.
if hasOneof {
var oneofWrappers []interface{}
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
}
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
}
if m, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(protoreflect.ProtoMessage); ok {
if m, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *protoimpl.MessageInfo }); ok {
oneofWrappers = m.ProtoMessageInfo().OneofWrappers
}
}
2017-06-21 23:00:27 +00:00
prop.OneofTypes = make(map[string]*OneofProperties)
2020-08-09 22:29:54 +00:00
for _, wrapper := range oneofWrappers {
p := &OneofProperties{
Type: reflect.ValueOf(wrapper).Type(), // *T
2017-06-21 23:00:27 +00:00
Prop: new(Properties),
}
2020-08-09 22:29:54 +00:00
f := p.Type.Elem().Field(0)
p.Prop.Name = f.Name
p.Prop.Parse(f.Tag.Get("protobuf"))
// Determine the struct field that contains this oneof.
// Each wrapper is assignable to exactly one parent field.
var foundOneof bool
for i := 0; i < t.NumField() && !foundOneof; i++ {
if p.Type.AssignableTo(t.Field(i).Type) {
p.Field = i
foundOneof = true
2017-06-21 23:00:27 +00:00
}
}
2020-08-09 22:29:54 +00:00
if !foundOneof {
panic(fmt.Sprintf("%v is not a generated message in the open-struct API", t))
}
prop.OneofTypes[p.Prop.OrigName] = p
2017-06-21 23:00:27 +00:00
}
}
return prop
}
2020-08-09 22:29:54 +00:00
func (sp *StructProperties) Len() int { return len(sp.Prop) }
func (sp *StructProperties) Less(i, j int) bool { return false }
func (sp *StructProperties) Swap(i, j int) { return }