MCPcopy Create free account
hub / github.com/compozy/agh / structFields

Method structFields

internal/codegen/sdkts/generate.go:202–242  ·  view source on GitHub ↗
(t reflect.Type)

Source from the content-addressed store, hash-verified

200}
201
202func (g *generator) structFields(t reflect.Type) ([]fieldSpec, error) {
203 if cached, ok := g.fieldSpecs[t]; ok {
204 return cached, nil
205 }
206 fields := make([]fieldSpec, 0, t.NumField())
207 for field := range t.Fields() {
208 if !field.IsExported() {
209 continue
210 }
211 if field.Anonymous && field.Tag.Get("json") == "" {
212 embedded := field.Type
213 for embedded.Kind() == reflect.Pointer {
214 embedded = embedded.Elem()
215 }
216 if embedded.Kind() == reflect.Struct {
217 embeddedFields, err := g.structFields(embedded)
218 if err != nil {
219 return nil, err
220 }
221 fields = append(fields, embeddedFields...)
222 continue
223 }
224 }
225
226 jsonName, optionalJSON := jsonFieldName(field)
227 if jsonName == "" {
228 continue
229 }
230 tsType, err := g.tsType(field.Type)
231 if err != nil {
232 return nil, fmt.Errorf("resolve field %s.%s: %w", t.Name(), field.Name, err)
233 }
234 fields = append(fields, fieldSpec{
235 Name: jsonName,
236 Type: tsType,
237 Optional: optionalJSON || field.Type.Kind() == reflect.Pointer,
238 })
239 }
240 g.fieldSpecs[t] = fields
241 return fields, nil
242}
243
244func jsonFieldName(field reflect.StructField) (string, bool) {
245 tag := field.Tag.Get("json")

Calls 6

tsTypeMethod · 0.95
appendFunction · 0.85
jsonFieldNameFunction · 0.85
GetMethod · 0.65
KindMethod · 0.65
NameMethod · 0.65