-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta.go
186 lines (163 loc) · 3.97 KB
/
meta.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
package errors
import (
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"github.com/ccmonky/log"
)
type MetaError interface {
error
App() string
Source() string
Code() string
Message() string
}
// NewMetaError define a new error with meta attached
func NewMetaError(source, code, msg string, opts ...Option) MetaError {
e := MetaAttr.With(empty, newMeta(source, code, msg))
for _, opt := range opts {
e = opt(e)
}
me := e.(MetaError)
err := RegisterMetaError(me)
if err != nil {
log.Panicln(err, "register meta error failed")
return nil
}
return me
}
type Meta struct {
app func() string
source string
code string
msg string
}
// newMeta creates a new Meta
func newMeta(source, code, msg string) *Meta {
m := Meta{
app: AppName,
source: source,
code: code,
msg: msg,
}
return &m
}
func (e *Meta) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"meta.app": e.app(),
"meta.source": e.source,
"meta.code": e.code,
"meta.message": e.msg,
})
}
func (e *Meta) App() string {
return e.app()
}
func (e *Meta) Source() string {
return e.source
}
func (e *Meta) Code() string {
return e.code
}
func (e *Meta) Message() string {
return e.msg
}
func (e *Meta) ID() string {
return fmt.Sprintf("%s:%s:%s", e.app(), e.source, e.code)
}
func (e *Meta) String() string {
return fmt.Sprintf("source=%s;code=%s", pkgName(e.source), e.code)
}
// Format implement fmt.Formatter
func (e *Meta) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, fmt.Sprintf("%s:%s:%s:%s", e.App(), e.Source(), e.Code(), e.Message()))
return
}
fallthrough
case 's', 'q':
io.WriteString(s, e.String())
}
}
func pkgName(source string) string {
parts := strings.Split(source, "/")
return parts[len(parts)-1]
}
// RegisterMetaError register MetaError into metaErrors registry, return error is exists
func RegisterMetaError(me MetaError) error {
if me == nil {
return New("register nil meta error, just ignore")
}
if me.App() != AppName() {
return Errorf("meta error app(%s) != current app name %s", me.App(), AppName())
}
id := MetaID(me.App(), me.Source(), me.Code())
if me.Source() == "" || me.Code() == "" || me.Message() == "" {
return Errorf("meta error(%s): source, code and msg can not be empty", id)
}
metaErrorsLock.Lock()
defer metaErrorsLock.Unlock()
if _, ok := metaErrors[id]; ok {
return Errorf("meta error %s already exists; use with_xxx to rebind", id)
}
metaErrors[id] = me
return nil
}
// GetMetaError get Meta according to app, source and code
func GetMetaError(id string) MetaError {
metaErrorsLock.RLock()
defer metaErrorsLock.RUnlock()
return metaErrors[id]
}
// AllMetaErrors return all registered MetaErrors as a map with key is `app:source:code`
func AllMetaErrors() map[metaID]MetaError {
var result = map[metaID]MetaError{}
metaErrorsLock.RLock()
defer metaErrorsLock.RUnlock()
for id, me := range metaErrors {
result[id] = me
}
return result
}
// AppName return current app name, use `SetAppName` or `inithook.AppName` to set app name
func AppName() string {
appLock.RLock()
defer appLock.RUnlock()
return app
}
// SetAppName set app name and register Meta to new app namespace
func SetAppName(appNew string) error {
appLock.Lock()
appOld := app
app = appNew
appLock.Unlock()
metaErrorsLock.Lock()
if appOld != appNew {
newMetaErrors := make(map[metaID]MetaError, len(metaErrors))
for _, me := range metaErrors {
idNew := MetaID(me.App(), me.Source(), me.Code())
if _, ok := metaErrors[idNew]; ok {
return Errorf("meta error(%s) already exists", idNew)
}
newMetaErrors[idNew] = me
}
for id, me := range newMetaErrors {
metaErrors[id] = me
}
}
metaErrorsLock.Unlock()
return nil
}
// MetaID returns a unique id of `Meta`
func MetaID(app, source, code string) string {
return fmt.Sprintf("%s:%s:%s", app, source, code)
}
var (
metaErrors = make(map[metaID]MetaError)
metaErrorsLock sync.RWMutex
)
type metaID = string