MCPcopy Index your code
hub / github.com/coder/coder / extractFields

Function extractFields

scripts/modeloptionsgen/main.go:89–164  ·  view source on GitHub ↗

extractFields walks the struct fields of t and returns a FieldGroup. prefix is used to build dot-separated json_name values for nested structs. skip lists Go field names to exclude from output.

(t reflect.Type, prefix string, skip map[string]bool)

Source from the content-addressed store, hash-verified

87// prefix is used to build dot-separated json_name values for nested
88// structs. skip lists Go field names to exclude from output.
89func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGroup {
90 var fields []SchemaField
91
92 for i := range t.NumField() {
93 f := t.Field(i)
94
95 if skip != nil && skip[f.Name] {
96 continue
97 }
98
99 jsonTag := f.Tag.Get("json")
100 if jsonTag == "" || jsonTag == "-" {
101 continue
102 }
103 jsonName := strings.Split(jsonTag, ",")[0]
104 if jsonName == "" {
105 continue
106 }
107
108 fullJSONName := jsonName
109 if prefix != "" {
110 fullJSONName = prefix + "." + jsonName
111 }
112
113 // Determine the underlying type, dereferencing pointers.
114 ft := f.Type
115 if ft.Kind() == reflect.Ptr {
116 ft = ft.Elem()
117 }
118
119 // Check the hidden tag before recursing into nested structs
120 // so that entire sub-objects can be marked hidden.
121 hidden := f.Tag.Get("hidden") == "true"
122
123 // decimal.Decimal is an opaque numeric type used for pricing
124 // precision; do not recurse into its internal struct fields.
125 isDecimal := ft == reflect.TypeOf(decimal.Decimal{})
126
127 // If the field is a struct (not a map), recurse to flatten
128 // its children using dot-separated names — unless the
129 // entire struct is marked hidden, in which case emit it
130 // as a single opaque field.
131 if ft.Kind() == reflect.Struct && !hidden && !isDecimal {
132 nested := extractFields(ft, fullJSONName, nil)
133 fields = append(fields, nested.Fields...)
134 continue
135 }
136
137 typeName := goTypeToSchemaType(f.Type)
138 description := f.Tag.Get("description")
139 label := f.Tag.Get("label")
140 enumTag := f.Tag.Get("enum")
141
142 var enumValues []string
143 if enumTag != "" {
144 enumValues = strings.Split(enumTag, ",")
145 }
146

Callers 1

mainFunction · 0.85

Calls 5

goTypeToSchemaTypeFunction · 0.85
inferInputTypeFunction · 0.85
goFieldPathFunction · 0.85
GetMethod · 0.65
ContainsMethod · 0.45

Tested by

no test coverage detected