-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
69 lines (59 loc) · 1.74 KB
/
transform.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
package entries
type SubjectField string
type transFunc func(val any) any
func (fn transFunc) then(after transFunc) transFunc {
if fn == nil {
return after
}
return func(val any) any {
val = fn(val)
return after(val)
}
}
// TransformSpec contains the transform functions for any given field in a LogEntry.
// If the field is not found or is nil, then the transform function will not be executed.
type TransformSpec map[SubjectField]transFunc
func NewTransformSpec() TransformSpec {
return TransformSpec{}
}
func (e LogEntry) getSubjectFieldVal(field SubjectField) any {
return e[string(field)]
}
func (e LogEntry) setSubjectField(field SubjectField, val any) {
s := string(field)
if e.HasField(s) {
e[s] = val
}
}
// Transform will add a field transform.
// Adding a transform for a field where one is already assigned will append the given transform function to the existing one.
func (s TransformSpec) Transform(field SubjectField, trans transFunc) TransformSpec {
s[field] = s[field].then(trans)
return s
}
// TransformType will create a transform function that first asserts that the field type matches the expected type for the given transform function.
// If the types don't match, then the transform function will not be called.
func TransformType[T any](trans func(val T) T) func(any) any {
return func(a any) any {
if a == nil {
return a
}
if v, ok := a.(T); ok {
return trans(v)
}
return a
}
}
// Transform transforms field values according to the given TransformSpec.
func Transform(entry LogEntry, spec TransformSpec) LogEntry {
for field, trans := range spec {
if trans == nil {
continue
}
val := entry.getSubjectFieldVal(field)
if val != nil {
entry.setSubjectField(field, trans(val))
}
}
return entry
}