Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package writers |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "github.com/vvval/go-metadata-scanner/vars" |
||
6 | ) |
||
7 | |||
8 | type JSONWriter struct { |
||
9 | BaseWriter |
||
10 | buf map[string]map[string]interface{} |
||
11 | } |
||
12 | |||
13 | // Headers to be like: Filename, XMP, IPTC, etc... |
||
14 | func (w *JSONWriter) Write(file *vars.File) error { |
||
15 | w.buf[file.RelPath()] = packJSONLine(file, w.headers) |
||
16 | |||
17 | return nil |
||
18 | } |
||
19 | |||
20 | func (w *JSONWriter) Open(filename string, headers []string) error { |
||
21 | w.BaseWriter = NewWriter(filename, headers) |
||
22 | |||
23 | file, err := openFile(w.filename) |
||
24 | if err != nil { |
||
25 | return err |
||
26 | } |
||
27 | |||
28 | w.buf = make(map[string]map[string]interface{}) |
||
29 | w.file = file |
||
30 | |||
31 | return nil |
||
32 | } |
||
33 | |||
34 | func (w *JSONWriter) Close() error { |
||
35 | packed, err := json.MarshalIndent(w.buf, "", " ") |
||
36 | if err != nil { |
||
37 | return err |
||
38 | } |
||
39 | |||
40 | w.file.Write(packed) |
||
41 | closeFile(w.file) |
||
42 | |||
43 | return nil |
||
44 | } |
||
45 | |||
46 | func packJSONLine(file *vars.File, headers []string) map[string]interface{} { |
||
47 | record := map[string]interface{}{} |
||
48 | for k, v := range tagsByGroups(file, headers) { |
||
49 | record[k] = v |
||
50 | } |
||
51 | |||
52 | return record |
||
53 | } |
||
54 |