MCPcopy Create free account
hub / github.com/google/go-cloud / encodeStructWithFields

Function encodeStructWithFields

docstore/driver/codec.go:274–293  ·  view source on GitHub ↗
(v reflect.Value, fields fields.List, e Encoder)

Source from the content-addressed store, hash-verified

272}
273
274func encodeStructWithFields(v reflect.Value, fields fields.List, e Encoder) error {
275 e2 := e.EncodeMap(len(fields))
276 for _, f := range fields {
277 fv, ok := fieldByIndex(v, f.Index)
278 if !ok {
279 // if !ok, then f is a field in an embedded pointer to struct, and that embedded pointer
280 // is nil in v. In other words, the field exists in the struct type, but not this particular
281 // struct value. So we just ignore it.
282 continue
283 }
284 if f.ParsedTag.(tagOptions).omitEmpty && IsEmptyValue(fv) {
285 continue
286 }
287 if err := encode(fv, e2); err != nil {
288 return err
289 }
290 e2.MapKey(f.Name)
291 }
292 return nil
293}
294
295// fieldByIndex retrieves the field of v at the given index if present.
296// v must be a struct. index must refer to a valid field of v's type.

Callers 2

EncodeMethod · 0.85
encodeFunction · 0.85

Calls 5

fieldByIndexFunction · 0.85
IsEmptyValueFunction · 0.85
encodeFunction · 0.85
EncodeMapMethod · 0.65
MapKeyMethod · 0.65

Tested by

no test coverage detected