github.com/influx6/npkg@v0.8.8/npair/pair.go (about) 1 package npair 2 3 import ( 4 "time" 5 ) 6 7 // NilPair defines a nil starting pair. 8 var NilPair = (*Pair)(nil) 9 10 // Field represents a giving map of values associated with a giving field value. 11 type Field map[string]interface{} 12 13 // GetBool collects the string value of a key if it exists. 14 func (p Field) GetBool(key string) (bool, bool) { 15 val, found := p.Get(key) 16 if !found { 17 return false, false 18 } 19 20 value, ok := val.(bool) 21 return value, ok 22 } 23 24 // GetFloat64 collects the string value of a key if it exists. 25 func (p Field) GetFloat64(key string) (float64, bool) { 26 val, found := p.Get(key) 27 if !found { 28 return 0, false 29 } 30 31 value, ok := val.(float64) 32 return value, ok 33 } 34 35 // GetFloat32 collects the string value of a key if it exists. 36 func (p Field) GetFloat32(key string) (float32, bool) { 37 val, found := p.Get(key) 38 if !found { 39 return 0, false 40 } 41 42 value, ok := val.(float32) 43 return value, ok 44 } 45 46 // GetInt8 collects the string value of a key if it exists. 47 func (p Field) GetInt8(key string) (int8, bool) { 48 val, found := p.Get(key) 49 if !found { 50 return 0, false 51 } 52 53 value, ok := val.(int8) 54 return value, ok 55 } 56 57 // GetInt16 collects the string value of a key if it exists. 58 func (p Field) GetInt16(key string) (int16, bool) { 59 val, found := p.Get(key) 60 if !found { 61 return 0, false 62 } 63 64 value, ok := val.(int16) 65 return value, ok 66 } 67 68 // GetInt64 collects the string value of a key if it exists. 69 func (p Field) GetInt64(key string) (int64, bool) { 70 val, found := p.Get(key) 71 if !found { 72 return 0, false 73 } 74 75 value, ok := val.(int64) 76 return value, ok 77 } 78 79 // GetInt32 collects the string value of a key if it exists. 80 func (p Field) GetInt32(key string) (int32, bool) { 81 val, found := p.Get(key) 82 if !found { 83 return 0, false 84 } 85 86 value, ok := val.(int32) 87 return value, ok 88 } 89 90 // GetInt collects the string value of a key if it exists. 91 func (p Field) GetInt(key string) (int, bool) { 92 val, found := p.Get(key) 93 if !found { 94 return 0, false 95 } 96 97 value, ok := val.(int) 98 return value, ok 99 } 100 101 // GetDuration collects the string value of a key if it exists. 102 func (p Field) GetDuration(key string) (time.Duration, bool) { 103 val, found := p.Get(key) 104 if !found { 105 return 0, false 106 } 107 108 switch item := val.(type) { 109 case string: 110 var dur, err = time.ParseDuration(item) 111 if err != nil { 112 return 0, false 113 } 114 return dur, true 115 case time.Duration: 116 return item, true 117 } 118 119 return 0, false 120 } 121 122 // GetString collects the string value of a key if it exists. 123 func (p Field) GetString(key string) (string, bool) { 124 val, found := p.Get(key) 125 if !found { 126 return "", false 127 } 128 129 value, ok := val.(string) 130 return value, ok 131 } 132 133 // Get collects the value of a key if it exists. 134 func (p Field) Get(key string) (value interface{}, found bool) { 135 if p == nil { 136 return 137 } 138 139 val, ok := p[key] 140 return val, ok 141 } 142 143 func (p Field) Has(key string) bool { 144 if p == nil { 145 return false 146 } 147 148 var _, ok = p[key] 149 return ok 150 } 151 152 // Pair defines a struct for storing a linked pair of key and values. 153 type Pair struct { 154 prev *Pair 155 key string 156 value interface{} 157 } 158 159 // NewPair returns a a key-value pair chain for setting fields. 160 func NewPair(key string, value interface{}) *Pair { 161 return &Pair{ 162 key: key, 163 value: value, 164 } 165 } 166 167 // Append returns a new Pair with the giving key and with the provded Pair set as 168 // it's previous link. 169 func Append(p *Pair, key string, value interface{}) *Pair { 170 return p.Append(key, value) 171 } 172 173 // Fields returns all internal pair data as a map. 174 func (p *Pair) Fields() Field { 175 if p == nil { 176 return make(Field) 177 } 178 179 if p.prev == nil { 180 f := make(Field) 181 f[p.key] = p.value 182 return f 183 } 184 185 f := p.prev.Fields() 186 if p.key != "" { 187 f[p.key] = p.value 188 } 189 190 return f 191 } 192 193 // Append returns a new pair with the giving key and value and its previous 194 // set to this pair. 195 func (p *Pair) Append(key string, val interface{}) *Pair { 196 return &Pair{ 197 prev: p, 198 key: key, 199 value: val, 200 } 201 } 202 203 // Root returns the root Pair in the chain which links all pairs together. 204 func (p *Pair) Root() *Pair { 205 if p.prev == nil { 206 return p 207 } 208 209 return p.prev.Root() 210 } 211 212 // GetBool collects the string value of a key if it exists. 213 func (p *Pair) GetBool(key string) (bool, bool) { 214 val, found := p.Get(key) 215 if !found { 216 return false, false 217 } 218 219 value, ok := val.(bool) 220 return value, ok 221 } 222 223 // GetFloat64 collects the string value of a key if it exists. 224 func (p *Pair) GetFloat64(key string) (float64, bool) { 225 val, found := p.Get(key) 226 if !found { 227 return 0, false 228 } 229 230 value, ok := val.(float64) 231 return value, ok 232 } 233 234 // GetFloat32 collects the string value of a key if it exists. 235 func (p *Pair) GetFloat32(key string) (float32, bool) { 236 val, found := p.Get(key) 237 if !found { 238 return 0, false 239 } 240 241 value, ok := val.(float32) 242 return value, ok 243 } 244 245 // GetInt8 collects the string value of a key if it exists. 246 func (p *Pair) GetInt8(key string) (int8, bool) { 247 val, found := p.Get(key) 248 if !found { 249 return 0, false 250 } 251 252 value, ok := val.(int8) 253 return value, ok 254 } 255 256 // GetInt16 collects the string value of a key if it exists. 257 func (p *Pair) GetInt16(key string) (int16, bool) { 258 val, found := p.Get(key) 259 if !found { 260 return 0, false 261 } 262 263 value, ok := val.(int16) 264 return value, ok 265 } 266 267 // GetInt64 collects the string value of a key if it exists. 268 func (p *Pair) GetInt64(key string) (int64, bool) { 269 val, found := p.Get(key) 270 if !found { 271 return 0, false 272 } 273 274 value, ok := val.(int64) 275 return value, ok 276 } 277 278 // GetInt32 collects the string value of a key if it exists. 279 func (p *Pair) GetInt32(key string) (int32, bool) { 280 val, found := p.Get(key) 281 if !found { 282 return 0, false 283 } 284 285 value, ok := val.(int32) 286 return value, ok 287 } 288 289 // GetInt collects the string value of a key if it exists. 290 func (p *Pair) GetInt(key string) (int, bool) { 291 val, found := p.Get(key) 292 if !found { 293 return 0, false 294 } 295 296 value, ok := val.(int) 297 return value, ok 298 } 299 300 // GetString collects the string value of a key if it exists. 301 func (p *Pair) GetString(key string) (string, bool) { 302 val, found := p.Get(key) 303 if !found { 304 return "", false 305 } 306 307 value, ok := val.(string) 308 return value, ok 309 } 310 311 // Get collects the value of a key if it exists. 312 func (p *Pair) Get(key string) (value interface{}, found bool) { 313 if p == nil { 314 return 315 } 316 317 if p.key == key { 318 return p.value, true 319 } 320 321 if p.prev == nil { 322 return 323 } 324 325 return p.prev.Get(key) 326 }