extractStructInfo extracts metadata from a struct type
(name string, structType *types.Struct)
| 82 | |
| 83 | // extractStructInfo extracts metadata from a struct type |
| 84 | func 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 |
| 122 | func parseStructsFromPackage(pkg *packages.Package, targetTypes []string) ([]*StructInfo, error) { |