Repository navigation
Expand file tree
/
Copy pathfield.go
More file actions
135 lines (116 loc) · 3.66 KB
/
Copy pathfield.go
File metadata and controls
135 lines (116 loc) · 3.66 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
package logger
import (
"time"
)
// kind enumerates how a Field's value is stored so encoders never reflect on
// common types (the zero-alloc path). Reflection is reserved for kindAny.
type kind uint8
const (
kindAny kind = iota
kindString
kindInt64
kindUint64
kindFloat64
kindBool
kindDuration
kindTime
kindError
)
// Field is one structured key/value pair. Scalars are stored unboxed (num/str)
// so the typed API allocates nothing; only kindAny escapes to interface{}.
type Field struct {
Key string
knd kind
num uint64
str string
any any
}
// --- Type-safe generic constructors ----------------------------------------
// Integer/Float constraints kept local so the core module stays zero-dep.
type integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type float interface{ ~float32 | ~float64 }
// String adds a string field.
func String(key, val string) Field {
return Field{Key: key, knd: kindString, str: val}
}
// Int adds any integer-typed value without boxing.
func Int[T integer](key string, v T) Field {
if T(0)-1 < T(0) { // signed type: 0-1 == -1 < 0
return Field{Key: key, knd: kindInt64, num: uint64(int64(v))}
}
return Field{Key: key, knd: kindUint64, num: uint64(v)}
}
// Float adds any float-typed value without boxing.
func Float[T float](key string, v T) Field {
return Field{Key: key, knd: kindFloat64, num: float64bits(float64(v))}
}
// Bool adds a boolean field.
func Bool(key string, v bool) Field {
var n uint64
if v {
n = 1
}
return Field{Key: key, knd: kindBool, num: n}
}
// Dur adds a time.Duration field.
func Dur(key string, d time.Duration) Field {
return Field{Key: key, knd: kindDuration, num: uint64(d)}
}
// Time adds a time.Time field (stored as UnixNano).
func Time(key string, t time.Time) Field {
return Field{Key: key, knd: kindTime, num: uint64(t.UnixNano())}
}
// Err adds an error under the conventional "error" key. nil is preserved so
// the encoder can emit an explicit null rather than dropping the field.
func Err(err error) Field {
return Field{Key: "error", knd: kindError, any: err}
}
// NamedErr is Err with a caller-chosen key.
func NamedErr(key string, err error) Field {
return Field{Key: key, knd: kindError, any: err}
}
// Any is the escape hatch: arbitrary value via reflection at encode time.
// Prefer a typed constructor on hot paths.
func Any(key string, v any) Field {
return Field{Key: key, knd: kindAny, any: v}
}
// Value boxes the field value into an interface. This is the public accessor
// for adapter/sink authors outside this package (encoders use the unboxed
// accessors below on the hot path). nil-error fields return a nil any.
func (f Field) Value() any {
switch f.knd {
case kindString:
return f.str
case kindInt64:
return int64(f.num)
case kindUint64:
return f.num
case kindFloat64:
return float64frombits(f.num)
case kindBool:
return f.num != 0
case kindDuration:
return time.Duration(f.num)
case kindTime:
return time.Unix(0, int64(f.num))
case kindError:
return f.errVal()
default:
return f.any
}
}
// --- accessors used by encoders --------------------------------------------
func (f Field) string() string { return f.str }
func (f Field) int64() int64 { return int64(f.num) }
func (f Field) uint64() uint64 { return f.num }
func (f Field) float64() float64 { return float64frombits(f.num) }
func (f Field) bool() bool { return f.num != 0 }
func (f Field) duration() time.Duration { return time.Duration(f.num) }
func (f Field) time() time.Time { return time.Unix(0, int64(f.num)) }
func (f Field) errVal() error {
e, _ := f.any.(error)
return e
}