-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
56 lines (47 loc) · 1.02 KB
/
fields.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
package log
// Field represents a logging field.
type Field struct {
Name string
Value interface{}
}
type fieldStackItem struct {
fields []Field
}
type fieldStack struct {
items []*fieldStackItem
}
// MakeField creates a new logging field.
func MakeField(name string, value interface{}) Field {
return Field{name, value}
}
func FieldsToFieldsArray(fields Fields) (arr []Field) {
for name, value := range fields {
arr = append(arr, MakeField(name, value))
}
return
}
func makeFieldStackItem(fields []Field) *fieldStackItem {
return &fieldStackItem{fields}
}
func makeFieldStack() *fieldStack {
return &fieldStack{}
}
func (s *fieldStack) push(fields []Field) *fieldStack {
item := makeFieldStackItem(fields)
s.items = append(s.items, item)
return s
}
func (s *fieldStack) pop() {
if len(s.items) > 0 {
s.items = s.items[:len(s.items)-1]
}
}
func (s *fieldStack) allFields() Fields {
fields := make(Fields)
for _, i := range s.items {
for _, f := range i.fields {
fields[f.Name] = f.Value
}
}
return fields
}