-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
60 lines (52 loc) · 1.22 KB
/
setup.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
package jsonRespKit
import (
"github.com/richelieu-yang/chimera/v2/src/core/interfaceKit"
"github.com/richelieu-yang/chimera/v2/src/json/jsonKit"
"github.com/richelieu-yang/chimera/v2/src/log/logrusKit"
"github.com/sirupsen/logrus"
)
type (
// RespProvider
/*
@return 建议是个加上json tag的结构体实例指针
*/
RespProvider func(code, msg string, data interface{}) interface{}
)
var (
provider RespProvider = nil
// api 用于序列化json
api jsonKit.API = nil
)
func MustSetUp(respProvider RespProvider, options ...Option) {
if err := SetUp(respProvider, options...); err != nil {
logrusKit.DisableQuote(nil)
logrus.Fatalf("%+v", err)
}
}
func SetUp(respProvider RespProvider, options ...Option) (err error) {
defer func() {
if err != nil {
api = nil
provider = nil
msgMap = make(map[string]string)
}
}()
err = interfaceKit.AssertNotNil(respProvider, "respProvider")
if err != nil {
return err
}
provider = respProvider
opts := loadOptions(options...)
for _, path := range opts.filePathSlice {
if err := readFile(path); err != nil {
return err
}
}
for _, fd := range opts.fileDataSlice {
if err := readFileData(fd); err != nil {
return err
}
}
api = opts.api
return nil
}