| 1 |
package csv |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"github.com/wildducktheories/go-csv/utils" |
| 6 |
"os" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type Record interface { |
| 12 |
|
| 13 |
Header() []string |
| 14 |
|
| 15 |
|
| 16 |
Get(key string) string |
| 17 |
|
| 18 |
Put(key string, value string) |
| 19 |
|
| 20 |
AsMap() map[string]string |
| 21 |
|
| 22 |
AsSlice() []string |
| 23 |
|
| 24 |
PutAll(r Record) |
| 25 |
|
| 26 |
SameHeader(r Record) bool |
| 27 |
} |
| 28 |
|
| 29 |
type record struct { |
| 30 |
header []string |
| 31 |
index map[string]int |
| 32 |
fields []string |
| 33 |
cache map[string]string |
| 34 |
} |
| 35 |
|
| 36 |
type RecordBuilder func(fields []string) Record |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func NewRecordBuilder(header []string) RecordBuilder { |
| 44 |
index := utils.NewIndex(header) |
| 45 |
return func(fields []string) Record { |
| 46 |
if len(header) < len(fields) { |
| 47 |
fmt.Fprintf(os.Stderr, "invariant violated: [%d]fields=%v, [%d]header=%v\n", len(fields), fields, len(header), header) |
| 48 |
} |
| 49 |
tmp := make([]string, len(header), len(header)) |
| 50 |
copy(tmp, fields) |
| 51 |
return &record{ |
| 52 |
header: header, |
| 53 |
index: index, |
| 54 |
fields: tmp, |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (r *record) Header() []string { |
| 60 |
return r.header |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (r *record) Get(key string) string { |
| 65 |
x, ok := r.index[key] |
| 66 |
if ok && x < len(r.fields) { |
| 67 |
return r.fields[x] |
| 68 |
} |
| 69 |
return "" |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (r *record) Put(key string, value string) { |
| 74 |
x, ok := r.index[key] |
| 75 |
if ok && x < cap(r.fields) { |
| 76 |
if x > len(r.fields) { |
| 77 |
r.fields = r.fields[0:x] |
| 78 |
} |
| 79 |
if r.cache != nil { |
| 80 |
r.cache[key] = value |
| 81 |
} |
| 82 |
r.fields[x] = value |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (r *record) AsMap() map[string]string { |
| 88 |
if r.cache != nil { |
| 89 |
return r.cache |
| 90 |
} |
| 91 |
|
| 92 |
result := make(map[string]string) |
| 93 |
for i, h := range r.header { |
| 94 |
if i < len(r.fields) { |
| 95 |
result[h] = r.fields[i] |
| 96 |
} else { |
| 97 |
result[h] = "" |
| 98 |
} |
| 99 |
} |
| 100 |
r.cache = result |
| 101 |
return result |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (r *record) AsSlice() []string { |
| 106 |
return r.fields |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (r *record) PutAll(o Record) { |
| 111 |
if r.SameHeader(o) { |
| 112 |
copy(r.fields, o.AsSlice()) |
| 113 |
r.cache = nil |
| 114 |
} else { |
| 115 |
for i, k := range r.header { |
| 116 |
v := o.Get(k) |
| 117 |
r.fields[i] = v |
| 118 |
if r.cache != nil { |
| 119 |
r.cache[k] = v |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
func (r *record) SameHeader(o Record) bool { |
| 127 |
h := o.Header() |
| 128 |
if len(r.header) != len(h) { |
| 129 |
return false |
| 130 |
} else if len(h) == 0 || &h[0] == &r.header[0] { |
| 131 |
|
| 132 |
return true |
| 133 |
} else { |
| 134 |
for i, k := range r.header { |
| 135 |
if h[i] != k { |
| 136 |
return false |
| 137 |
} |
| 138 |
} |
| 139 |
return true |
| 140 |
} |
| 141 |
} |