-
Notifications
You must be signed in to change notification settings - Fork 13
/
json.go
145 lines (129 loc) · 3.11 KB
/
json.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package json
import (
json2 "encoding/json"
"fmt"
"strings"
"github.com/eolinker/eosc"
)
const (
ROOT = "fields"
defaultChildKey = "proxies"
)
var ConfigFormatError = fmt.Errorf("config is not valid")
type filedType string
const (
Variable filedType = "variable"
Constants filedType = "constants"
Object filedType = "object"
Array filedType = "array"
)
type fieldInfo struct {
name string
cname string
t filedType
child map[string]fieldInfo
childKey string
}
type jsonFormat struct {
fields map[string]fieldInfo
}
func NewFormatter(cfg eosc.FormatterConfig) (eosc.IFormatter, error) {
fields, ok := cfg[ROOT]
if !ok {
return nil, ConfigFormatError
}
data, err := ParseConfig(fields, cfg)
if err != nil {
return nil, err
}
return &jsonFormat{fields: data}, nil
}
func (j *jsonFormat) Format(entry eosc.IEntry) []byte {
res := make(map[string]interface{})
if len(j.fields) > 0 {
res = j.getValue(j.fields, entry)
}
b, _ := json2.Marshal(res)
return b
}
func (j *jsonFormat) getValue(fields map[string]fieldInfo, entry eosc.IEntry) map[string]interface{} {
res := make(map[string]interface{})
for key, info := range fields {
switch info.t {
case Constants:
// 常量
res[key] = info.name
case Variable:
res[key] = entry.Read(info.name)
case Array:
res[key] = j.getArray(info.childKey, info.child, entry)
case Object:
res[key] = j.getValue(info.child, entry)
}
}
return res
}
func (j *jsonFormat) getArray(key string, arr map[string]fieldInfo, entry eosc.IEntry) []interface{} {
ens := entry.Children(key)
res := make([]interface{}, 0, len(ens))
for _, en := range ens {
res = append(res, j.getValue(arr, en))
}
return res
}
func ParseConfig(root []string, cfg eosc.FormatterConfig) (map[string]fieldInfo, error) {
data := make(map[string]fieldInfo)
for _, field := range root {
// 键值,别名,类型
info := parse(field)
if info.t != Variable && info.t != Constants {
c, ok := cfg[info.name]
if !ok {
return nil, ConfigFormatError
}
child, err := ParseConfig(c, cfg)
if err != nil {
return nil, err
}
info.child = child
}
data[info.cname] = info
}
return data, nil
}
func parse(filed string) fieldInfo {
filed = strings.Trim(filed, " ")
fs := strings.Split(filed, " ")
key := fs[0]
res := fieldInfo{name: key, t: Constants}
if strings.HasPrefix(key, "$") {
key = strings.TrimLeft(key, "$")
// 常量
res = fieldInfo{name: key, t: Variable}
}
if strings.HasPrefix(key, "@") {
key = strings.TrimLeft(key, "@")
if strings.Contains(key, "#") {
if strings.HasSuffix(key, "#") {
key = strings.TrimRight(key, "#")
// 数组,获取所有字段
res = fieldInfo{name: key, t: Array, childKey: defaultChildKey}
} else {
// 数组,获取检索字段
s := strings.Split(key, "#")
key, childKey := s[0], s[1]
res = fieldInfo{name: key, t: Array, childKey: childKey}
}
} else {
// 对象
res = fieldInfo{name: key, t: Object}
}
}
l := len(fs)
if l == 3 && strings.Contains(filed, "as") {
res.cname = fs[l-1]
} else {
res.cname = res.name
}
return res
}