MCPcopy Create free account
hub / github.com/apache/fory / extractStructInfo

Function extractStructInfo

go/fory/codegen/parser.go:84–119  ·  view source on GitHub ↗

extractStructInfo extracts metadata from a struct type

(name string, structType *types.Struct)

Source from the content-addressed store, hash-verified

82
83// extractStructInfo extracts metadata from a struct type
84func extractStructInfo(name string, structType *types.Struct) (*StructInfo, error) {
85 var fields []*FieldInfo
86 usedTagIDs := make(map[int]string)
87
88 for i := 0; i < structType.NumFields(); i++ {
89 field := structType.Field(i)
90 if !field.Exported() {
91 continue // Skip unexported fields
92 }
93
94 fieldInfo, err := analyzeField(field, structType.Tag(i), i)
95 if err != nil {
96 return nil, fmt.Errorf("analyzing field %s: %w", field.Name(), err)
97 }
98
99 if fieldInfo == nil {
100 continue // Skip unsupported fields
101 }
102 if fieldInfo.HasTagID {
103 if existing, ok := usedTagIDs[fieldInfo.TagID]; ok {
104 return nil, fmt.Errorf("duplicate field id %d for fields %s and %s", fieldInfo.TagID, existing, fieldInfo.GoName)
105 }
106 usedTagIDs[fieldInfo.TagID] = fieldInfo.GoName
107 }
108
109 fields = append(fields, fieldInfo)
110 }
111
112 // Sort fields according to Fory protocol
113 sortFields(fields)
114
115 return &StructInfo{
116 Name: name,
117 Fields: fields,
118 }, nil
119}
120
121// parseStructsFromPackage finds and parses structs from a package
122func parseStructsFromPackage(pkg *packages.Package, targetTypes []string) ([]*StructInfo, error) {

Calls 3

analyzeFieldFunction · 0.85
sortFieldsFunction · 0.70
FieldMethod · 0.45