Debian Codegraph @

json-to-csv.go

raw symbols SCIP
1 package csv
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "strings"
8 )
9
10 // Given a stream of JSON records, generate a stream of csv records.
11 //
12 // The columns of the output CSV stream are named by the Header parameter. Each column is interprefed
13 // as a path into the JSON input object. If the object at that path is a string, the
14 // string is copied into the specified column of the output stream. Otherwise, the json encoding
15 // of the object is copied into the specified column of the output stream.
16 //
17 // Each object in the input object which is mapped by a CSV column is logically deleted from
18 // the input object. If --base-object-key is specified, a JSON encoding of the remaining input object
19 // is written into the specified column of the CSV output stream.
20 //
21 type JsonToCsvProcess struct {
22 BaseObject string
23 Header []string
24 }
25
26 func (proc *JsonToCsvProcess) readMap(m map[string]interface{}, p []string) interface{} {
27 if len(p) == 1 {
28 v := m[p[0]]
29 delete(m, p[0])
30 if v == nil {
31 return ""
32 } else {
33 return v
34 }
35 } else if len(p) > 1 {
36 var o interface{}
37 var mo map[string]interface{}
38 var ok bool
39 if o, ok = m[p[0]]; ok {
40 if mo, ok = o.(map[string]interface{}); ok {
41 return proc.readMap(mo, p[1:])
42 }
43 }
44 }
45 return ""
46 }
47
48 func (p *JsonToCsvProcess) asString(v interface{}) (string, error) {
49 if s, ok := v.(string); ok {
50 return s, nil
51 } else {
52 if b, err := json.Marshal(v); err != nil {
53 return "", fmt.Errorf("unable to marshal as string: %v", err)
54 } else {
55 return string(b), nil
56 }
57 }
58 }
59
60 func (p *JsonToCsvProcess) Run(decoder *json.Decoder, builder WriterBuilder, errCh chan<- error) {
61 errCh <- func() (err error) {
62 decoder.UseNumber()
63 header := p.Header
64 baseObject := p.BaseObject
65
66 line := 0
67 paths := map[string][]string{}
68 for _, k := range header {
69 paths[k] = strings.Split(k, ".")
70 }
71 if baseObject != "" {
72 if _, ok := paths[baseObject]; !ok {
73 header = append(header, baseObject)
74 }
75 }
76
77 // open the decoder
78 encoder := builder(header)
79 defer encoder.Close(err)
80
81 for {
82 line++
83 m := map[string]interface{}{}
84 if err := decoder.Decode(&m); err != nil {
85 if err == io.EOF {
86 break
87 }
88 return fmt.Errorf("warning: %d: unable to decode object: %s\n", line, err)
89 }
90 r := encoder.Blank()
91 for _, k := range header {
92 v := p.readMap(m, paths[k])
93 if s, err := p.asString(v); err != nil {
94 return fmt.Errorf("warning: %d: unable to encode object: %s\n", line, err)
95 } else {
96 r.Put(k, s)
97 }
98 }
99
100 if baseObject != "" {
101 if b, err := json.Marshal(m); err != nil {
102 return fmt.Errorf("warning: %d: unable to marshal object: %s\n", line, err)
103 } else {
104 r.Put(baseObject, string(b))
105 }
106 }
107
108 if err := encoder.Write(r); err != nil {
109 return fmt.Errorf("fatal: %d: failed to write: %s", line, err)
110 }
111 }
112 return nil
113 }()
114 }