-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfield.go
70 lines (59 loc) · 1.39 KB
/
field.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
package log
import (
"fmt"
)
// FieldType avoids doing type assertion or calling reflection
// TODO: difference between type assertion and reflection
type FieldType uint8
const (
UnknownType FieldType = iota
IntType
StringType
)
// Fields is a slice of Field
type Fields []Field
// Field is based on uber-go/zap https://github.com/uber-go/zap/blob/master/zapcore/field.go
// It can be treated as a Union, the value is stored in either Int, Str or Interface
// TODO: interface{} is actually not used ...
type Field struct {
Key string
Type FieldType
// values
Int int64
Str string
Interface interface{}
}
// Int creates a field with int value, it uses int64 internally
func Int(k string, v int) Field {
return Field{
Key: k,
Type: IntType,
Int: int64(v),
}
}
// TODO: reuse same int64 and rely on type? ...
//func Float(k string, v float64) Field {
//
//}
// Str creates a field with string value
func Str(k string, v string) Field {
return Field{
Key: k,
Type: StringType,
Str: v,
}
}
// Stringer calls the String() method and stores return value
func Stringer(k string, v fmt.Stringer) Field {
return Field{
Key: k,
Type: StringType,
Str: v.String(),
}
}
// copyFields make a copy of the slice so modifying one won't have effect on another,
func copyFields(fields Fields) Fields {
copied := make([]Field, len(fields))
copy(copied, fields)
return copied
}