-
Notifications
You must be signed in to change notification settings - Fork 2
/
pair.go
180 lines (147 loc) · 3.41 KB
/
pair.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
package metrics
// NilPair defines a nil starting pair.
var NilPair = (*Pair)(nil)
// Pair defines a struct for storing a linked pair of key and values.
type Pair struct {
prev *Pair
key string
value interface{}
}
// NewPair returns a a key-value pair chain for setting fields.
func NewPair(key string, value interface{}) *Pair {
return &Pair{
key: key,
value: value,
}
}
// Append returns a new Pair with the giving key and with the provded Pair set as
// it's previous link.
func Append(p *Pair, key string, value interface{}) *Pair {
return p.Append(key, value)
}
// Fields returns all internal pair data as a map.
func (p *Pair) Fields() Field {
if p == nil {
return make(Field)
}
if p.prev == nil {
f := make(Field)
f[p.key] = p.value
return f
}
f := p.prev.Fields()
if p.key != "" {
f[p.key] = p.value
}
return f
}
// Append returns a new pair with the giving key and value and its previous
// set to this pair.
func (p *Pair) Append(key string, val interface{}) *Pair {
return &Pair{
prev: p,
key: key,
value: val,
}
}
// Root returns the root Pair in the chain which links all pairs together.
func (p *Pair) Root() *Pair {
if p.prev == nil {
return p
}
return p.prev.Root()
}
// GetBool collects the string value of a key if it exists.
func (p *Pair) GetBool(key string) (bool, bool) {
val, found := p.Get(key)
if !found {
return false, false
}
value, ok := val.(bool)
return value, ok
}
// GetFloat64 collects the string value of a key if it exists.
func (p *Pair) GetFloat64(key string) (float64, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(float64)
return value, ok
}
// GetFloat32 collects the string value of a key if it exists.
func (p *Pair) GetFloat32(key string) (float32, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(float32)
return value, ok
}
// GetInt8 collects the string value of a key if it exists.
func (p *Pair) GetInt8(key string) (int8, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(int8)
return value, ok
}
// GetInt16 collects the string value of a key if it exists.
func (p *Pair) GetInt16(key string) (int16, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(int16)
return value, ok
}
// GetInt64 collects the string value of a key if it exists.
func (p *Pair) GetInt64(key string) (int64, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(int64)
return value, ok
}
// GetInt32 collects the string value of a key if it exists.
func (p *Pair) GetInt32(key string) (int32, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(int32)
return value, ok
}
// GetInt collects the string value of a key if it exists.
func (p *Pair) GetInt(key string) (int, bool) {
val, found := p.Get(key)
if !found {
return 0, false
}
value, ok := val.(int)
return value, ok
}
// GetString collects the string value of a key if it exists.
func (p *Pair) GetString(key string) (string, bool) {
val, found := p.Get(key)
if !found {
return "", false
}
value, ok := val.(string)
return value, ok
}
// Get collects the value of a key if it exists.
func (p *Pair) Get(key string) (value interface{}, found bool) {
if p == nil {
return
}
if p.key == key {
return p.value, true
}
if p.prev == nil {
return
}
return p.prev.Get(key)
}