Debian Codegraph @

select.go

raw symbols SCIP
1 package csv
2
3 import (
4 "github.com/wildducktheories/go-csv/utils"
5 )
6
7 // Given a header-prefixed input stream of CSV records select the fields that match the specified key (Key).
8 // If PermuteOnly is is specified, all the fields of the input stream are preserved, but the output stream
9 // is permuted so that the key fields occupy the left-most fields of the output stream. The remaining fields
10 // are preserved in their original order.
11 type SelectProcess struct {
12 Keys []string
13 PermuteOnly bool
14 }
15
16 func (p *SelectProcess) Run(reader Reader, builder WriterBuilder, errCh chan<- error) {
17 errCh <- func() (err error) {
18 defer reader.Close()
19
20 keys := p.Keys
21 permuteOnly := p.PermuteOnly
22
23 // get the data header
24 dataHeader := reader.Header()
25
26 _, _, b := utils.Intersect(keys, dataHeader)
27 if len(b) > 0 && permuteOnly {
28 extend := make([]string, len(keys)+len(b))
29 copy(extend, keys)
30 copy(extend[len(keys):], b)
31 keys = extend
32 }
33
34 // create a new output stream
35 writer := builder(keys)
36 defer writer.Close(err)
37
38 for data := range reader.C() {
39 outputData := writer.Blank()
40 outputData.PutAll(data)
41 if err = writer.Write(outputData); err != nil {
42 return err
43 }
44 }
45
46 return reader.Error()
47 }()
48 }