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
|
package format
const (
// TypeMask when set in argument flags
// indicates that type information of
// the passed value, and all nested types,
// should be included in formatted output.
TypeMask = uint64(1) << 0
// LogfmtMask when set in argument flags
// indicates that strings should be escaped
// and quoted only where necessary. i.e. if
// it contains any unsafe ASCII chars or double
// quotes it will be quoted and escaped, if it
// contains any spaces it will be quoted, and
// all else will be printed as-is. This proves
// particularly well readable in key-value types.
LogfmtMask = uint64(1) << 1
// NumberMask when set in argument flags
// indicates that where possible value
// types should be formatted as numbers,
// i.e. byte or rune types.
NumberMask = uint64(1) << 2
// TextMask when set in argument flags
// indicates that where possible value
// types should be formatted as text,
// i.e. []byte or []rune types.
TextMask = uint64(1) << 3
// QuotedTextMask when set in argument flags
// indicates that text should always be quoted.
QuotedTextMask = uint64(1) << 4
// QuotedAsciiMask when set in argument flags
// indicates that text should always be quoted,
// and escaped as ASCII characters where needed.
QuotedAsciiMask = uint64(1) << 5
// NoMethodMask when set in argument flags
// indicates that where a type supports a
// known method, (e.g. Error() or String()),
// this should not be used for formatting
// instead treating as a method-less type.
// e.g. printing the entire struct value of
// a &url.URL{} without calling String().
NoMethodMask = uint64(1) << 6
)
var (
// default set of Args.
defaultArgs = Args{
Flags: LogfmtMask | TextMask,
Int: IntArgs{Base: 10},
Uint: IntArgs{Base: 10},
Float: FloatArgs{Fmt: 'g', Prec: -1},
Complex: ComplexArgs{
Real: FloatArgs{Fmt: 'g', Prec: -1},
Imag: FloatArgs{Fmt: 'g', Prec: -1},
},
}
// zeroArgs used for checking
// zero value Arg{} fields.
zeroArgs Args
)
// DefaultArgs returns default
// set of formatter arguments.
func DefaultArgs() Args {
return defaultArgs
}
// Args contains arguments
// for a call to a FormatFunc.
type Args struct {
// Boolean argument
// flags as bit-field.
Flags uint64
// Integer
// arguments.
// i.e. for:
// - int
// - int8
// - int16
// - int32 (treated as rune char, number with NumberMask)
// - int64
Int IntArgs
// Unsigned
// integer
// arguments.
// i.e. for:
// - uint
// - uint8 (treated as byte char, number with NumberMask)
// - uint16
// - uint32
// - uint64
Uint IntArgs
// Float
// arguments.
// i.e. for:
// - float32
// - float64
Float FloatArgs
// Complex
// arguments.
// i.e. for:
// - complex64
// - complex128
Complex ComplexArgs
}
// IntArgs provides a set of
// arguments for customizing
// integer number serialization.
type IntArgs struct {
Base int
Pad int
}
// FloatArgs provides a set of
// arguments for customizing
// float number serialization.
type FloatArgs struct {
Fmt byte
Prec int
}
// ComplexArgs provides a set of
// arguments for customizing complex
// number serialization, as real and
// imaginary float number parts.
type ComplexArgs struct {
Real FloatArgs
Imag FloatArgs
}
// WithType returns if TypeMask is set.
func (a *Args) WithType() bool {
return a.Flags&TypeMask != 0
}
// Logfmt returns if LogfmtMask is set.
func (a *Args) Logfmt() bool {
return a.Flags&LogfmtMask != 0
}
// AsNumber returns if NumberMask is set.
func (a *Args) AsNumber() bool {
return a.Flags&NumberMask != 0
}
// AsText returns if TextMask is set.
func (a *Args) AsText() bool {
return a.Flags&TextMask != 0
}
// AsQuotedText returns if QuotedTextMask is set.
func (a *Args) AsQuotedText() bool {
return a.Flags&QuotedTextMask != 0
}
// AsQuotedASCII returns if QuotedAsciiMask is set.
func (a *Args) AsQuotedASCII() bool {
return a.Flags&QuotedAsciiMask != 0
}
// NoMethod returns if NoMethodMask is set.
func (a *Args) NoMethod() bool {
return a.Flags&NoMethodMask != 0
}
// SetWithType sets the TypeMask bit.
func (a *Args) SetWithType() {
a.Flags = a.Flags | TypeMask
}
// SetLogfmt sets the LogfmtMask bit.
func (a *Args) SetLogfmt() {
a.Flags = a.Flags | LogfmtMask
}
// SetAsNumber sets the NumberMask bit.
func (a *Args) SetAsNumber() {
a.Flags = a.Flags | NumberMask
}
// SetAsText sets the TextMask bit.
func (a *Args) SetAsText() {
a.Flags = a.Flags | TextMask
}
// SetAsQuotedText sets the QuotedTextMask bit.
func (a *Args) SetAsQuotedText() {
a.Flags = a.Flags | QuotedTextMask
}
// SetAsQuotedASCII sets the QuotedAsciiMask bit.
func (a *Args) SetAsQuotedASCII() {
a.Flags = a.Flags | QuotedAsciiMask
}
// SetNoMethod sets the NoMethodMask bit.
func (a *Args) SetNoMethod() {
a.Flags = a.Flags | NoMethodMask
}
// UnsetWithType unsets the TypeMask bit.
func (a *Args) UnsetWithType() {
a.Flags = a.Flags & ^TypeMask
}
// UnsetLogfmt unsets the LogfmtMask bit.
func (a *Args) UnsetLogfmt() {
a.Flags = a.Flags & ^LogfmtMask
}
// UnsetAsNumber unsets the NumberMask bit.
func (a *Args) UnsetAsNumber() {
a.Flags = a.Flags & ^NumberMask
}
// UnsetAsText unsets the TextMask bit.
func (a *Args) UnsetAsText() {
a.Flags = a.Flags & ^TextMask
}
// UnsetAsQuotedText unsets the QuotedTextMask bit.
func (a *Args) UnsetAsQuotedText() {
a.Flags = a.Flags & ^QuotedTextMask
}
// UnsetAsQuotedASCII unsets the QuotedAsciiMask bit.
func (a *Args) UnsetAsQuotedASCII() {
a.Flags = a.Flags & ^QuotedAsciiMask
}
// UnsetNoMethod unsets the NoMethodMask bit.
func (a *Args) UnsetNoMethod() {
a.Flags = a.Flags & ^NoMethodMask
}
|