-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_multiple.go
110 lines (95 loc) · 2.05 KB
/
error_multiple.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
package i18n
var (
multipleDefaultErrorField = "_summary"
)
type I18nMultipleError struct {
code int
locale *string
errors map[string]*baseError
}
func NewMultipleErr(field, section string, key string, values ...M) *I18nMultipleError {
if len(values) == 0 {
return &I18nMultipleError{
errors: map[string]*baseError{
field: {
section: section,
key: key,
},
},
}
} else {
return &I18nMultipleError{
errors: map[string]*baseError{
field: {
section: section,
key: key,
values: values[0],
},
},
}
}
}
func NewMultipleDefaultErr(section string, key string, values ...M) *I18nMultipleError {
if len(values) == 0 {
return &I18nMultipleError{
errors: map[string]*baseError{
multipleDefaultErrorField: {
section: section,
key: key,
},
},
}
} else {
return &I18nMultipleError{
errors: map[string]*baseError{
multipleDefaultErrorField: {
section: section,
key: key,
values: values[0],
},
},
}
}
}
func (e *I18nMultipleError) MarshalJSON() ([]byte, error) {
if e == nil {
return nullJSON, nil
}
result := make([]byte, 0)
if e.locale != nil && *e.locale != "" {
for k, v := range e.errors {
trStr := ""
if len(v.values) == 0 {
trStr = Get(*e.locale).T(v.section, v.key)
} else {
trStr = Get(*e.locale).Tf(v.section, v.key, v.values)
}
result = append(result, []byte(`"`+k+`":"`+trStr+`",`)...)
}
} else {
for k, v := range e.errors {
entry := []byte(`"` + k + `":{"` + v.section + `":"` + v.key + `"},`)
result = append(result, entry...)
}
}
l := len(result)
if l == 0 {
return nullJSON, nil
}
result = result[:len(result)-1]
result = append([]byte{'{'}, result...)
result = append(result, '}')
return result, nil
}
func (e *I18nMultipleError) HasErrors() bool {
return len(e.errors) > 0
}
func (e *I18nMultipleError) Code() int {
return e.code
}
func (e *I18nMultipleError) Locale() *string {
return e.locale
}
func (e *I18nMultipleError) Errors() map[string]*baseError {
return e.errors
}