Debian Codegraph @

record.go

raw symbols SCIP
1 package csv
2
3 import (
4 "fmt"
5 "github.com/wildducktheories/go-csv/utils"
6 "os"
7 )
8
9 //Record provides keyed access to the fields of data records where each field
10 //of a data record is keyed by the value of the corresponding field in the header record.
11 type Record interface {
12 // Return the header of the record.
13 Header() []string
14 // Gets the value of the field specified by the key. Returns the empty string
15 // if the field does not exist in the record.
16 Get(key string) string
17 // Puts the value into the field specified by the key.
18 Put(key string, value string)
19 // Return the contents of the record as a map. Mutation of the map is not supported.
20 AsMap() map[string]string
21 // Return the contents of the record as a slice. Mutation of the slice is not supported.
22 AsSlice() []string
23 // Puts all the matching values from the specified record into the receiving record
24 PutAll(r Record)
25 // Return true if the receiver and the specified record have the same header.
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 // NewRecordBuilder returns a function that can be used to create new Records
39 // for a CSV stream with the specified header.
40 //
41 // This can be used with raw encoding/csv streams in cases where a CSV stream contains
42 // more than one record type.
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 // Answer the value of the field indexed by the column containing the specified header value.
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 // Puts the specified value into the record at the index determined by the key value.
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 // Return a map containing a copy of the contents of the record.
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 // Return the record values as a slice.
105 func (r *record) AsSlice() []string {
106 return r.fields
107 }
108
109 // Puts all the specified value into the record.
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 // Efficiently check that the receiver and specified records have the same header
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 // two slices with the same address and length have the same contents
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 }