/
single.go
58 lines (47 loc) · 1.46 KB
/
single.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
package fields
// With creates an instance of Fields for the given key value pair.
func With(key string, value interface{}) Fields {
return &single{key: key, value: value}
}
// With creates an instance of Fields for the given key and a Lazy fmt.Sprintf
// value from the given format and args.
func Withf(key string, format string, args ...interface{}) Fields {
return With(key, LazyFormat(format, args...))
}
type single struct {
key string
value interface{}
}
func (instance *single) ForEach(consumer func(key string, value interface{}) error) error {
if instance == nil || consumer == nil {
return nil
}
return consumer(instance.key, instance.value)
}
func (instance *single) Get(key string) (interface{}, bool) {
if instance != nil && key == instance.key {
return instance.value, true
}
return nil, false
}
func (instance *single) With(key string, value interface{}) Fields {
return instance.asParentOf(With(key, value))
}
func (instance *single) Withf(key string, format string, args ...interface{}) Fields {
return instance.asParentOf(Withf(key, format, args...))
}
func (instance *single) WithAll(of map[string]interface{}) Fields {
return instance.asParentOf(WithAll(of))
}
func (instance *single) asParentOf(fields Fields) Fields {
return newLineage(fields, instance)
}
func (instance *single) Without(keys ...string) Fields {
return newWithout(instance, keys...)
}
func (instance *single) Len() (result int) {
if instance == nil {
return 0
}
return 1
}