(v reflect.Value, fields fields.List, e Encoder)
| 272 | } |
| 273 | |
| 274 | func 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. |
no test coverage detected