-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathIssues.go
More file actions
147 lines (123 loc) · 4.36 KB
/
Copy pathIssues.go
File metadata and controls
147 lines (123 loc) · 4.36 KB
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
package internals
import (
"fmt"
zconst "github.com/Oudwins/zog/zconst"
)
// this is the function that formats the error message given a zog error
type IssueFmtFunc = func(e *ZogIssue, p Ctx)
// ZogIssue represents an issue that occurred during parsing or validation.
// When printed it looks like:
// ZogIssue{Code: coercion_issue, Params: map[], Type: number, Value: not_empty, Message: number is invalid, Error: failed to coerce string int: strconv.Atoi: parsing "not_empty": invalid syntax}
type ZogIssue struct {
// Code is the unique identifier for the issue. Generally also the ID for the Test that caused the issue.
Code zconst.ZogIssueCode
// Path is the path to the field that caused the issue
Path []string
// Value is the data value that caused the issue.
// If using Schema.Parse(data, dest) then this will be the value of data.
Value any
// Dtype is the destination type. i.e The zconst.ZogType of the value that was validated.
// If using Schema.Parse(data, dest) then this will be the type of dest.
Dtype zconst.ZogType
// Params is the params map for the issue. Taken from the Test that caused the issue.
// This may be nil if Test has no params.
Params map[string]any
// Message is the human readable, user-friendly message for the issue.
// This is safe to expose to the user.
Message string
// Err is the wrapped error or nil if none
Err error
}
func NewZogIssue() *ZogIssue {
e := ZogIssuePool.Get().(*ZogIssue)
e.Code = ""
e.Path = nil
e.Value = nil
e.Dtype = ""
e.Params = nil
e.Message = ""
e.Err = nil
return e
}
// SetCode sets the issue code for the issue and returns the issue for chaining
func (i *ZogIssue) SetCode(c zconst.ZogIssueCode) *ZogIssue {
i.Code = c
return i
}
// SetPath sets the path for the issue and returns the issue for chaining
func (i *ZogIssue) SetPath(p []string) *ZogIssue {
i.Path = p
return i
}
// SetValue sets the data value that caused the issue and returns the issue for chaining
func (i *ZogIssue) SetValue(v any) *ZogIssue {
i.Value = v
return i
}
// SetDType sets the destination type for the issue and returns the issue for chaining
func (i *ZogIssue) SetDType(t zconst.ZogType) *ZogIssue {
i.Dtype = t
return i
}
// SetParams sets the params map for the issue and returns the issue for chaining
func (i *ZogIssue) SetParams(p map[string]any) *ZogIssue {
i.Params = p
return i
}
// SetMessage sets the human readable, user-friendly message for the issue and returns the issue for chaining
func (i *ZogIssue) SetMessage(m string) *ZogIssue {
i.Message = m
return i
}
// SetError sets the wrapped error for the issue and returns the issue for chaining
func (i *ZogIssue) SetError(e error) *ZogIssue {
i.Err = e
return i
}
// Unwrap returns the wrapped error or nil if none
func (i *ZogIssue) Unwrap() error {
return i.Err
}
// Error returns the string representation of the ZogIssue (same as String())
func (i *ZogIssue) Error() string {
return i.String()
}
// String returns the string representation of the ZogIssue (same as Error())
func (i *ZogIssue) String() string {
return fmt.Sprintf("ZogIssue{Code: %v, Params: %v, Type: %v, Value: %v, Message: '%v', Error: %v}", SafeString(i.Code), SafeString(i.Params), SafeString(i.Dtype), SafeString(i.Value), SafeString(i.Message), SafeError(i.Err))
}
// Returns a stringified version of the path based on the flatten path string logic
func (i *ZogIssue) PathString() string {
return FlattenPath(i.Path)
}
func FreeIssue(i *ZogIssue) {
ZogIssuePool.Put(i)
}
// ZogIssueList is the unified return type for all schema Parse/Validate operations
type ZogIssueList = []*ZogIssue
// Deprecated: ZogIssueMap is deprecated. All schemas now return ZogIssueList.
// Users should migrate to using ZogIssueList and access paths via issue.Path
type ZogIssueMap = map[string]ZogIssueList
// ErrsList - internal structure for collecting issues during schema execution
type ErrsList struct {
List ZogIssueList
}
// internal only
func NewErrsList() *ErrsList {
l := InternalIssueListPool.Get().(*ErrsList)
l.List = nil
return l
}
func (e *ErrsList) Add(err *ZogIssue) {
if e.List == nil {
e.List = make(ZogIssueList, 0, 4) // Slightly larger initial capacity
}
// Path is already set on the issue by SchemaCtx.Issue() or IssueFrom* methods
e.List = append(e.List, err)
}
func (e *ErrsList) IsEmpty() bool {
return len(e.List) == 0
}
func (e *ErrsList) Free() {
InternalIssueListPool.Put(e)
}