MCPcopy Create free account
hub / github.com/segmentio/encoding / structTypeOf

Function structTypeOf

proto/reflect.go:357–424  ·  view source on GitHub ↗
(t reflect.Type, seen map[reflect.Type]Type)

Source from the content-addressed store, hash-verified

355}
356
357func structTypeOf(t reflect.Type, seen map[reflect.Type]Type) *structType {
358 st := &structType{
359 name: t.Name(),
360 fieldsByName: make(map[string]int),
361 fieldsByNumber: make(map[FieldNumber]int),
362 }
363
364 seen[t] = st
365
366 fieldNumber := FieldNumber(0)
367 taggedFields := FieldNumber(0)
368
369 for i := range t.NumField() {
370 f := t.Field(i)
371
372 if f.PkgPath != "" {
373 continue // unexported
374 }
375
376 repeated := false
377 if f.Type.Kind() == reflect.Slice && f.Type.Elem().Kind() != reflect.Uint8 {
378 repeated = true
379 f.Type = f.Type.Elem() // for typeOf
380 }
381
382 fieldName := f.Name
383 fieldType := typeOf(f.Type, seen)
384
385 if tag, ok := f.Tag.Lookup("protobuf"); ok {
386 if fieldNumber != taggedFields {
387 panic("conflicting use of struct tag and naked fields")
388 }
389 t, err := parseStructTag(tag)
390 if err != nil {
391 panic(err)
392 }
393
394 fieldName = t.name
395 fieldNumber = t.fieldNumber
396 taggedFields = t.fieldNumber
397 // Because maps are represented as repeated varlen fields on the
398 // wire, the generated protobuf code sets the `rep` attribute on
399 // the struct fields.
400 repeated = t.repeated && f.Type.Kind() != reflect.Map
401
402 if t.zigzag {
403 fieldType = fieldType.ZigZag()
404 }
405 } else if fieldNumber == 0 && len(st.fields) != 0 {
406 panic("conflicting use of struct tag and naked fields")
407 } else {
408 fieldNumber++
409 }
410
411 index := len(st.fields)
412 st.fields = append(st.fields, Field{
413 Index: index,
414 Number: fieldNumber,

Callers 1

typeOfFunction · 0.85

Calls 9

FieldNumberTypeAlias · 0.85
typeOfFunction · 0.85
parseStructTagFunction · 0.85
NameMethod · 0.65
NumFieldMethod · 0.65
FieldMethod · 0.65
KindMethod · 0.65
ElemMethod · 0.65
ZigZagMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…