|
4 | 4 | "bytes"
|
5 | 5 | "encoding/csv"
|
6 | 6 | "fmt"
|
| 7 | + "strconv" |
7 | 8 | "time"
|
8 | 9 | )
|
9 | 10 |
|
@@ -44,27 +45,49 @@ func (c *Converter) setCSVHeaders() ([]string, int, error) {
|
44 | 45 |
|
45 | 46 | func (c *Converter) stringify(values []interface{}) []string {
|
46 | 47 | row := make([]string, len(values), len(values))
|
47 |
| - var value interface{} |
48 | 48 |
|
49 |
| - for i := range values { |
50 |
| - rawValue := values[i] |
| 49 | + for i, rawValue := range values { |
| 50 | + if rawValue == nil { |
| 51 | + row[i] = "" |
| 52 | + continue |
| 53 | + } |
51 | 54 |
|
52 | 55 | byteArray, ok := rawValue.([]byte)
|
53 | 56 | if ok {
|
54 |
| - value = string(byteArray) |
55 |
| - } else { |
56 |
| - value = rawValue |
| 57 | + rawValue = string(byteArray) |
57 | 58 | }
|
58 | 59 |
|
59 |
| - timeValue, ok := value.(time.Time) |
60 |
| - if ok && c.TimeFormat != "" { |
61 |
| - value = timeValue.Format(c.TimeFormat) |
62 |
| - } |
63 |
| - |
64 |
| - if value == nil { |
65 |
| - row[i] = "" |
66 |
| - } else { |
67 |
| - row[i] = fmt.Sprintf("%v", value) |
| 60 | + switch castValue := rawValue.(type) { |
| 61 | + case time.Time: |
| 62 | + if c.TimeFormat != "" { |
| 63 | + row[i] = castValue.Format(c.TimeFormat) |
| 64 | + } |
| 65 | + case bool: |
| 66 | + row[i] = strconv.FormatBool(castValue) |
| 67 | + case string: |
| 68 | + row[i] = castValue |
| 69 | + case int: |
| 70 | + row[i] = strconv.FormatInt(int64(castValue), 10) |
| 71 | + case int8: |
| 72 | + row[i] = strconv.FormatInt(int64(castValue), 10) |
| 73 | + case int16: |
| 74 | + row[i] = strconv.FormatInt(int64(castValue), 10) |
| 75 | + case int32: |
| 76 | + row[i] = strconv.FormatInt(int64(castValue), 10) |
| 77 | + case int64: |
| 78 | + row[i] = strconv.FormatInt(int64(castValue), 10) |
| 79 | + case uint: |
| 80 | + row[i] = strconv.FormatUint(uint64(castValue), 10) |
| 81 | + case uint8: |
| 82 | + row[i] = strconv.FormatUint(uint64(castValue), 10) |
| 83 | + case uint16: |
| 84 | + row[i] = strconv.FormatUint(uint64(castValue), 10) |
| 85 | + case uint32: |
| 86 | + row[i] = strconv.FormatUint(uint64(castValue), 10) |
| 87 | + case uint64: |
| 88 | + row[i] = strconv.FormatUint(uint64(castValue), 10) |
| 89 | + default: |
| 90 | + row[i] = fmt.Sprintf("%v", castValue) |
68 | 91 | }
|
69 | 92 | }
|
70 | 93 |
|
|
0 commit comments