MCPcopy
hub / github.com/golang/protobuf / newProperties

Function newProperties

proto/properties.go:227–302  ·  view source on GitHub ↗
(t reflect.Type)

Source from the content-addressed store, hash-verified

225}
226
227func newProperties(t reflect.Type) *StructProperties {
228 if t.Kind() != reflect.Struct {
229 panic(fmt.Sprintf("%v is not a generated message in the open-struct API", t))
230 }
231
232 var hasOneof bool
233 prop := new(StructProperties)
234
235 // Construct a list of properties for each field in the struct.
236 for i := 0; i < t.NumField(); i++ {
237 p := new(Properties)
238 f := t.Field(i)
239 tagField := f.Tag.Get("protobuf")
240 p.Init(f.Type, f.Name, tagField, &f)
241
242 tagOneof := f.Tag.Get("protobuf_oneof")
243 if tagOneof != "" {
244 hasOneof = true
245 p.OrigName = tagOneof
246 }
247
248 // Rename unrelated struct fields with the "XXX_" prefix since so much
249 // user code simply checks for this to exclude special fields.
250 if tagField == "" && tagOneof == "" && !strings.HasPrefix(p.Name, "XXX_") {
251 p.Name = "XXX_" + p.Name
252 p.OrigName = "XXX_" + p.OrigName
253 } else if p.Weak != "" {
254 p.Name = p.OrigName // avoid possible "XXX_" prefix on weak field
255 }
256
257 prop.Prop = append(prop.Prop, p)
258 }
259
260 // Construct a mapping of oneof field names to properties.
261 if hasOneof {
262 var oneofWrappers []interface{}
263 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
264 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
265 }
266 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
267 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
268 }
269 if m, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(protoreflect.ProtoMessage); ok {
270 if m, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *protoimpl.MessageInfo }); ok {
271 oneofWrappers = m.ProtoMessageInfo().OneofWrappers
272 }
273 }
274
275 prop.OneofTypes = make(map[string]*OneofProperties)
276 for _, wrapper := range oneofWrappers {
277 p := &OneofProperties{
278 Type: reflect.ValueOf(wrapper).Type(), // *T
279 Prop: new(Properties),
280 }
281 f := p.Type.Elem().Field(0)
282 p.Prop.Name = f.Name
283 p.Prop.Parse(f.Tag.Get("protobuf"))
284

Callers 1

GetPropertiesFunction · 0.85

Calls 7

InterfaceMethod · 0.80
ZeroMethod · 0.80
InMethod · 0.80
ProtoReflectMethod · 0.80
TypeMethod · 0.80
ParseMethod · 0.80
InitMethod · 0.65

Tested by

no test coverage detected