| 47 | } |
| 48 | |
| 49 | func flattenSlice(v reflect.Value, prefix string, m map[string]interface{}) error { |
| 50 | n := v.Len() |
| 51 | if n == 0 { |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | first := v.Index(0) |
| 56 | if first.Kind() == reflect.Interface { |
| 57 | if !first.IsNil() { |
| 58 | first = first.Elem() |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | switch first.Kind() { |
| 63 | case reflect.Map, reflect.Slice, reflect.Struct: |
| 64 | // Add the JSON representation of lists with complex types. |
| 65 | // Otherwise the number of CSV headers can grow significantly. |
| 66 | b, err := json.Marshal(v.Interface()) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | m[prefix] = string(b) |
| 71 | default: |
| 72 | values := make([]string, v.Len()) |
| 73 | for i := 0; i < v.Len(); i++ { |
| 74 | val := v.Index(i).Interface() |
| 75 | if val == nil { |
| 76 | values[i] = "null" |
| 77 | } else { |
| 78 | values[i] = fmt.Sprintf("%v", val) |
| 79 | } |
| 80 | } |
| 81 | m[prefix] = strings.Join(values, ",") |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func flattenStruct(v reflect.Value, prefix string, m map[string]interface{}) (err error) { |
| 87 | n := v.NumField() |