-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_reader.go
119 lines (102 loc) · 2.04 KB
/
json_reader.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package goson
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)
var (
arrayCheck = map[byte]byte{
'[': ']',
}
)
// Reader json string
type Reader interface {
GetField(keys ...string) Field
}
// Field from json reader
type Field interface {
Uint() (uint, error)
Int() (int, error)
String() (string, error)
Float() (float64, error)
Bool() (bool, error)
SetTo(target interface{}) error
}
// Read from json input
func Read(data []byte) (Reader, error) {
var tmp interface{}
err := json.Unmarshal(data, &tmp)
if err != nil {
return nil, err
}
return &reader{
data: tmp,
}, nil
}
type reader struct {
data interface{}
}
func (r *reader) GetField(keys ...string) Field {
var data interface{} = r.data
field := new(field)
func() {
var traces []string
defer func() {
if r := recover(); r != nil {
field.err = fmt.Errorf("key '%s' not found or out of range from JSON data", strings.Join(traces, " => "))
}
}()
for _, k := range keys {
traces = append(traces, k)
if arrayCheck[k[0]] == k[len(k)-1] {
if idx, err := strconv.Atoi(k[1 : len(k)-1]); err == nil {
data = data.([]interface{})[idx]
}
continue
}
tmp, ok := data.(map[string]interface{})[k]
if !ok && tmp == nil {
panic("invalid key")
}
data = tmp
}
}()
field.value = data
return field
}
type field struct {
err error
value interface{}
}
func (f *field) set(v reflect.Value) error {
if f.err != nil {
return f.err
}
return setValue(v, f.value)
}
func (f *field) Uint() (u uint, err error) {
err = f.set(reflect.ValueOf(&u))
return
}
func (f *field) Int() (i int, err error) {
err = f.set(reflect.ValueOf(&i))
return
}
func (f *field) Float() (fl float64, err error) {
err = f.set(reflect.ValueOf(&fl))
return
}
func (f *field) String() (s string, err error) {
err = f.set(reflect.ValueOf(&s))
return
}
func (f *field) Bool() (b bool, err error) {
err = f.set(reflect.ValueOf(&b))
return
}
func (f *field) SetTo(target interface{}) (err error) {
err = f.set(reflect.ValueOf(target))
return
}