-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattrs.go
More file actions
83 lines (68 loc) · 1.69 KB
/
Copy pathattrs.go
File metadata and controls
83 lines (68 loc) · 1.69 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
//go:build go1.21
package sdk
import (
sdk "log/slog"
"github.com/echocat/slf4g/fields"
)
type attrs []sdk.Attr
func (instance attrs) ForEach(consumer func(key string, value interface{}) error) error {
if consumer == nil {
return nil
}
for _, a := range instance {
if err := consumer(a.Key, a.Value.Any()); err != nil {
return err
}
}
return nil
}
func (instance attrs) Get(key string) (interface{}, bool) {
for _, a := range instance {
if a.Key == key {
return a.Value.Any(), true
}
}
return nil, false
}
func (instance attrs) With(key string, value interface{}) fields.Fields {
return instance.asParentOf(fields.With(key, value))
}
func (instance attrs) Withf(key string, format string, args ...interface{}) fields.Fields {
return instance.asParentOf(fields.Withf(key, format, args...))
}
func (instance attrs) WithAll(of map[string]interface{}) fields.Fields {
return instance.asParentOf(fields.WithAll(of))
}
func (instance attrs) Without(keys ...string) fields.Fields {
return fields.NewWithout(instance, keys...)
}
func (instance attrs) asParentOf(fds fields.Fields) fields.Fields {
return fields.NewLineage(fds, instance)
}
func (instance attrs) Len() (result int) {
return len(instance)
}
func (instance attrs) clone() attrs {
result := make(attrs, len(instance))
copy(result, instance)
return result
}
func (instance *attrs) add(keyPrefix string, vs ...sdk.Attr) {
for _, v := range vs {
nv := sdk.Attr{
Key: keyPrefix + v.Key,
Value: v.Value,
}
replaced := false
for i, existing := range *instance {
if existing.Key == nv.Key {
(*instance)[i] = nv
replaced = true
break
}
}
if !replaced {
*instance = append(*instance, nv)
}
}
}