-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow.go
66 lines (60 loc) · 1.51 KB
/
row.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package json2excel
import (
"errors"
"github.com/tidwall/gjson"
)
var ErrInconsistentJSON = errors.New("all JSON objects in the array must be of the same type and have consistent field names")
var ErrInvalidJSON = errors.New("please check the JSON data for errors")
var ErrInvalidJSONType = errors.New("only type array or object are allowed")
type exceldata struct {
Columns []string
RowsValues [][]any
}
func (*exceldata) getKeys(results []gjson.Result) []string {
var keys = make([]string, len(results))
for i, r := range results {
keys[i] = r.String()
}
return keys
}
func (excel *exceldata) ensureEquals(keys []string) error {
if len(excel.Columns) != len(keys) {
return ErrInconsistentJSON
}
for i := range excel.Columns {
if keys[i] != excel.Columns[i] {
return ErrInconsistentJSON
}
}
return nil
}
func (excel *exceldata) UnmarshalJSON(data []byte) error {
if !gjson.ValidBytes(data) {
return ErrInvalidJSON
}
result := gjson.ParseBytes(data)
if !result.IsArray() && !result.IsObject() {
return ErrInvalidJSONType
}
for i, r := range result.Array() {
if !r.IsObject() {
return ErrInvalidJSONType
}
keys := excel.getKeys(r.Get("@keys").Array())
if i == 0 {
if len(keys) == 0 {
return nil
}
excel.Columns = append(excel.Columns, keys...)
}
if err := excel.ensureEquals(keys); err != nil {
return err
}
var row = make([]any, len(keys))
for i, k := range keys {
row[i] = r.Get(k).Value()
}
excel.RowsValues = append(excel.RowsValues, row)
}
return nil
}