// 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.package protoimport ()// StructProperties represents protocol buffer type information for a// generated protobuf message in the open-struct API.//// Deprecated: Do not use.typeStructPropertiesstruct {// 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// OneofTypes contains information about the oneof fields in this message. // It is keyed by the protobuf field name. OneofTypes map[string]*OneofProperties}// Properties represents the type information for a protobuf message field.//// Deprecated: Do not use.typePropertiesstruct {// 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. WireType int// Tag is the protobuf field number. Tag int// Required reports whether this is a required field. Required bool// Optional reports whether this is a optional field. Optional bool// Repeated reports whether this is a repeated field. Repeated bool// 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}// OneofProperties represents the type information for a protobuf oneof.//// Deprecated: Do not use.typeOneofPropertiesstruct {// 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}// String formats the properties in the protobuf struct field tag style.func ( *Properties) () string { := .Wire += "," + strconv.Itoa(.Tag)if .Required { += ",req" }if .Optional { += ",opt" }if .Repeated { += ",rep" }if .Packed { += ",packed" } += ",name=" + .OrigNameif .JSONName != "" { += ",json=" + .JSONName }iflen(.Enum) > 0 { += ",enum=" + .Enum }iflen(.Weak) > 0 { += ",weak=" + .Weak }if .Proto3 { += ",proto3" }if .Oneof { += ",oneof" }if .HasDefault { += ",def=" + .Default }return}// Parse populates p by parsing a string in the protobuf struct field tag style.func ( *Properties) ( string) {// For example: "bytes,49,opt,name=foo,def=hello!"forlen() > 0 { := strings.IndexByte(, ',')if < 0 { = len() }switch := [:]; {casestrings.HasPrefix(, "name="): .OrigName = [len("name="):]casestrings.HasPrefix(, "json="): .JSONName = [len("json="):]casestrings.HasPrefix(, "enum="): .Enum = [len("enum="):]casestrings.HasPrefix(, "weak="): .Weak = [len("weak="):]casestrings.Trim(, "0123456789") == "": , := strconv.ParseUint(, 10, 32) .Tag = int()case == "opt": .Optional = truecase == "req": .Required = truecase == "rep": .Repeated = truecase == "varint" || == "zigzag32" || == "zigzag64": .Wire = .WireType = WireVarintcase == "fixed32": .Wire = .WireType = WireFixed32case == "fixed64": .Wire = .WireType = WireFixed64case == "bytes": .Wire = .WireType = WireBytescase == "group": .Wire = .WireType = WireStartGroupcase == "packed": .Packed = truecase == "proto3": .Proto3 = truecase == "oneof": .Oneof = truecasestrings.HasPrefix(, "def="):// The default tag is special in that everything afterwards is the // default regardless of the presence of commas. .HasDefault = true .Default, = [len("def="):], len() } = strings.TrimPrefix([:], ",") }}// Init populates the properties from a protocol buffer struct tag.//// Deprecated: Do not use.func ( *Properties) ( reflect.Type, , string, *reflect.StructField) { .Name = .OrigName = if == "" {return } .Parse()if != nil && .Kind() == reflect.Map { .MapKeyProp = new(Properties) .MapKeyProp.(nil, "Key", .Tag.Get("protobuf_key"), nil) .MapValProp = new(Properties) .MapValProp.(nil, "Value", .Tag.Get("protobuf_val"), nil) }}var propertiesCache sync.Map// map[reflect.Type]*StructProperties// 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.func ( reflect.Type) *StructProperties {if , := propertiesCache.Load(); {return .(*StructProperties) } , := propertiesCache.LoadOrStore(, newProperties())return .(*StructProperties)}func newProperties( reflect.Type) *StructProperties {if .Kind() != reflect.Struct {panic(fmt.Sprintf("%v is not a generated message in the open-struct API", )) }varbool := new(StructProperties)// Construct a list of properties for each field in the struct.for := 0; < .NumField(); ++ { := new(Properties) := .Field() := .Tag.Get("protobuf") .Init(.Type, .Name, , &) := .Tag.Get("protobuf_oneof")if != "" { = true .OrigName = }// Rename unrelated struct fields with the "XXX_" prefix since so much // user code simply checks for this to exclude special fields.if == "" && == "" && !strings.HasPrefix(.Name, "XXX_") { .Name = "XXX_" + .Name .OrigName = "XXX_" + .OrigName } elseif .Weak != "" { .Name = .OrigName// avoid possible "XXX_" prefix on weak field } .Prop = append(.Prop, ) }// Construct a mapping of oneof field names to properties.if {var []interface{}if , := reflect.PtrTo().MethodByName("XXX_OneofFuncs"); { = .Func.Call([]reflect.Value{reflect.Zero(.Type.In(0))})[3].Interface().([]interface{}) }if , := reflect.PtrTo().MethodByName("XXX_OneofWrappers"); { = .Func.Call([]reflect.Value{reflect.Zero(.Type.In(0))})[0].Interface().([]interface{}) }if , := reflect.Zero(reflect.PtrTo()).Interface().(protoreflect.ProtoMessage); {if , := .ProtoReflect().(interface{ () *protoimpl.MessageInfo }); { = .().OneofWrappers } } .OneofTypes = make(map[string]*OneofProperties)for , := range { := &OneofProperties{Type: reflect.ValueOf().Type(), // *TProp: new(Properties), } := .Type.Elem().Field(0) .Prop.Name = .Name .Prop.Parse(.Tag.Get("protobuf"))// Determine the struct field that contains this oneof. // Each wrapper is assignable to exactly one parent field.varboolfor := 0; < .NumField() && !; ++ {if .Type.AssignableTo(.Field().Type) { .Field = = true } }if ! {panic(fmt.Sprintf("%v is not a generated message in the open-struct API", )) } .OneofTypes[.Prop.OrigName] = } }return}func ( *StructProperties) () int { returnlen(.Prop) }func ( *StructProperties) (, int) bool { returnfalse }func ( *StructProperties) (, int) { return }
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.