MCPcopy
hub / github.com/openai/openai-go / newStructTypeEncoder

Method newStructTypeEncoder

internal/apiquery/encoder.go:125–210  ·  view source on GitHub ↗
(t reflect.Type)

Source from the content-addressed store, hash-verified

123}
124
125func (e *encoder) newStructTypeEncoder(t reflect.Type) encoderFunc {
126 if t.Implements(reflect.TypeOf((*param.Optional)(nil)).Elem()) {
127 return e.newRichFieldTypeEncoder(t)
128 }
129
130 for i := 0; i < t.NumField(); i++ {
131 if t.Field(i).Type == paramUnionType && t.Field(i).Anonymous {
132 return e.newStructUnionTypeEncoder(t)
133 }
134 }
135
136 encoderFields := []encoderField{}
137
138 // This helper allows us to recursively collect field encoders into a flat
139 // array. The parameter `index` keeps track of the access patterns necessary
140 // to get to some field.
141 var collectEncoderFields func(r reflect.Type, index []int)
142 collectEncoderFields = func(r reflect.Type, index []int) {
143 for i := 0; i < r.NumField(); i++ {
144 idx := append(index, i)
145 field := t.FieldByIndex(idx)
146 if !field.IsExported() {
147 continue
148 }
149 // If this is an embedded struct, traverse one level deeper to extract
150 // the field and get their encoders as well.
151 if field.Anonymous {
152 collectEncoderFields(field.Type, idx)
153 continue
154 }
155 // If query tag is not present, then we skip, which is intentionally
156 // different behavior from the stdlib.
157 ptag, ok := parseQueryStructTag(field)
158 if !ok {
159 continue
160 }
161
162 if (ptag.name == "-" || ptag.name == "") && !ptag.inline {
163 continue
164 }
165
166 dateFormat, ok := parseFormatStructTag(field)
167 oldFormat := e.dateFormat
168 if ok {
169 switch dateFormat {
170 case "date-time":
171 e.dateFormat = time.RFC3339
172 case "date":
173 e.dateFormat = "2006-01-02"
174 }
175 }
176 var encoderFn encoderFunc
177 if ptag.omitzero {
178 typeEncoderFn := e.typeEncoder(field.Type)
179 encoderFn = func(key string, value reflect.Value) ([]Pair, error) {
180 if value.IsZero() {
181 return nil, nil
182 }

Callers 1

newTypeEncoderMethod · 0.95

Calls 7

typeEncoderMethod · 0.95
renderKeyPathMethod · 0.95
parseQueryStructTagFunction · 0.85
IsZeroMethod · 0.80
parseFormatStructTagFunction · 0.70

Tested by

no test coverage detected