-
Notifications
You must be signed in to change notification settings - Fork 464
/
information.go
279 lines (250 loc) · 5.19 KB
/
information.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package fate
import (
"bufio"
"encoding/csv"
"encoding/json"
"fmt"
"github.com/babyname/fate/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
// Information ...
type Information interface {
Group(b bool)
Write(names ...Name) error
Head(heads ...string) error
Finish() error
}
type jsonInformation struct {
head []string
path string
file *os.File
}
// Group ...
func (j *jsonInformation) Group(b bool) {
panic("implement me")
}
type logInformation struct {
path string
sugar *zap.SugaredLogger
head []string
}
// Group ...
func (l *logInformation) Group(b bool) {
panic("implement me")
}
type csvInformation struct {
head []string
path string
file *os.File
}
// Group ...
func (c *csvInformation) Group(b bool) {
panic("implement me")
}
// Finish ...
func (l *logInformation) Finish() error {
return l.sugar.Sync()
}
func initOutputWithConfig(output config.FileOutput) Information {
switch output.OutputMode {
//case config.OutputModelJSON:
// return jsonOutput(output.Path)
case config.OutputModeCSV:
return csvOutput(output.Path)
}
return logOutput(output.Path)
}
// Finish ...
func (j *jsonInformation) Finish() error {
return j.file.Close()
}
// Write ...
func (j *jsonInformation) Write(names ...Name) error {
w := bufio.NewWriter(j.file)
for _, n := range names {
out := headNameJSONOutput(j.head, n, nil)
//output json
_, _ = w.Write(out)
_, _ = w.WriteString(",\n")
}
return w.Flush()
}
// Head ...
func (j *jsonInformation) Head(heads ...string) error {
j.head = heads
return nil
}
func jsonOutput(path string) Information {
file, e := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_RDWR, 0755)
if e != nil {
panic(fmt.Errorf("json output failed:%w", e))
}
return &jsonInformation{
path: path,
file: file,
}
}
// Write ...
func (l *logInformation) Write(names ...Name) error {
for _, n := range names {
out := headNameOutput(l.head, n, func(s string) bool {
return s == "姓名"
})
l.sugar.Infow(n.String(), out...)
}
return nil
}
// Head ...
func (l *logInformation) Head(heads ...string) error {
l.head = heads
return nil
}
func logOutput(path string) Information {
if path == "" {
path = "stdout"
}
cfg := zap.NewProductionConfig()
cfg.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "name",
LevelKey: "",
TimeKey: "",
NameKey: "",
CallerKey: "",
StacktraceKey: "",
LineEnding: "",
EncodeLevel: nil,
EncodeTime: nil,
EncodeDuration: nil,
EncodeCaller: nil,
EncodeName: nil,
}
cfg.OutputPaths = []string{
path,
}
cfg.DisableCaller = true
cfg.DisableStacktrace = true
logger, e := cfg.Build(
zap.AddCaller(),
zap.AddCallerSkip(1),
)
if e != nil {
panic(e)
}
return &logInformation{
path: path,
sugar: logger.Sugar(),
}
}
func csvOutput(path string) Information {
file, e := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_RDWR, 0755)
if e != nil {
panic(fmt.Errorf("json output failed:%w", e))
}
return &csvInformation{
path: path,
file: file,
}
}
// Write ...
func (c *csvInformation) Write(names ...Name) error {
w := csv.NewWriter(c.file)
for _, n := range names {
out := nameOutputString(c.head, n)
_ = w.Write(out)
}
w.Flush()
return nil
}
// Finish ...
func (c *csvInformation) Finish() error {
return c.file.Close()
}
// Head ...
func (c *csvInformation) Head(heads ...string) (e error) {
c.head = heads
w := csv.NewWriter(c.file)
e = w.Write(heads)
if e != nil {
return e
}
w.Flush()
return nil
}
func headNameOutput(heads []string, name Name, skip func(string) bool) (out []interface{}) {
for _, h := range heads {
if skip != nil && skip(h) {
continue
}
switch h {
case "姓名":
out = append(out, h, name.String())
case "笔画":
out = append(out, h, name.Strokes())
case "拼音":
out = append(out, h, name.PinYin())
case "喜用神":
out = append(out, h, name.XiYongShen())
case "八字", "生辰八字":
out = append(out, h, name.BaZi())
}
}
return
}
func headNameJSONOutput(heads []string, name Name, skip func(string) bool) (b []byte) {
out := make(map[string]string)
for _, h := range heads {
if skip != nil && skip(h) {
continue
}
switch h {
case "姓名":
out[h] = name.String()
case "笔画":
out[h] = name.Strokes()
case "拼音":
out[h] = name.PinYin()
case "喜用神":
out[h] = name.XiYongShen()
}
}
by, e := json.Marshal(out)
if e != nil {
return nil
}
return by
}
func headNameOutputString(heads []string, name Name, skip func(string) bool) (out []string) {
for _, h := range heads {
if skip != nil && skip(h) {
continue
}
switch h {
case "姓名":
out = append(out, h, name.String())
case "笔画":
out = append(out, h, name.Strokes())
case "拼音":
out = append(out, h, name.PinYin())
case "喜用神":
out = append(out, h, name.XiYongShen())
}
}
return
}
func nameOutputString(heads []string, name Name) (out []string) {
for _, h := range heads {
switch h {
case "姓名":
out = append(out, name.String())
case "笔画":
out = append(out, name.Strokes())
case "拼音":
out = append(out, name.PinYin())
case "喜用神":
out = append(out, name.XiYongShen())
}
}
return
}