-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredact_test.go
85 lines (73 loc) · 2.73 KB
/
redact_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package xoptest_test
import (
"fmt"
"strings"
"testing"
"github.com/xoplog/xop-go"
"github.com/xoplog/xop-go/xopat"
"github.com/xoplog/xop-go/xopbase"
"github.com/xoplog/xop-go/xoprecorder"
"github.com/xoplog/xop-go/xoptest"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type selfRedactor struct {
V string
}
func (r selfRedactor) Redact() interface{} {
r.V = strings.ReplaceAll(r.V, "bribe", "consideration")
return r
}
func (r selfRedactor) String() string {
return r.V
}
type redactor interface {
Redact() interface{}
}
func TestRedaction(t *testing.T) {
tLog := xoptest.New(t)
log := xop.NewSeed(
xop.WithBase(tLog),
xop.WithSettings(func(settings *xop.LogSettings) {
settings.SetRedactStringFunc(func(baseLine xopbase.Line, k xopat.K, v string) {
v = strings.ReplaceAll(v, "sunflower", "daisy")
baseLine.String(k, v, xopbase.StringDataType)
})
settings.SetRedactAnyFunc(func(baseLine xopbase.Line, k xopat.K, v interface{}, alreadyImmutable bool) {
if !alreadyImmutable {
v = deepcopy.Copy(v)
}
if canRedact, ok := v.(redactor); ok {
baseLine.Any(k, xopbase.ModelArg{
Model: canRedact.Redact(),
})
} else {
baseLine.Any(k, xopbase.ModelArg{
Model: v,
})
}
})
settings.SetRedactErrorFunc(func(baseLine xopbase.Line, k xopat.K, v error) {
baseLine.String(k, v.Error()+"(as string)", xopbase.ErrorDataType)
})
}),
).Request(t.Name())
a := selfRedactor{V: "I got the contract with a small bribe, just a sunflower cookie"}
log.Info().
String(xop.Key("garden"), "nothing in my garden is taller than my sunflower!").
Any(xop.Key("story"), a).
Any(xop.Key("tale"), a).
AnyWithoutRedaction(xop.Key("raw"), a).
Stringer(xop.Key("success"), a).
Error(xop.Key("oops"), fmt.Errorf("outer: %w", fmt.Errorf("inner"))).
Msg("foo")
foos := tLog.Recorder().FindLines(xoprecorder.MessageEquals("foo"))
require.NotEmpty(t, foos, "foo line")
assert.Equal(t, "nothing in my garden is taller than my daisy!", foos[0].Data["garden"], "garden")
assert.Equal(t, "I got the contract with a small consideration, just a sunflower cookie", foos[0].Data["story"].(xopbase.ModelArg).Model.(selfRedactor).V, "story")
assert.Equal(t, "I got the contract with a small consideration, just a sunflower cookie", foos[0].Data["tale"].(xopbase.ModelArg).Model.(selfRedactor).V, "tale")
assert.Equal(t, "I got the contract with a small bribe, just a sunflower cookie", foos[0].Data["raw"].(xopbase.ModelArg).Model.(selfRedactor).V, "raw")
assert.Equal(t, "I got the contract with a small bribe, just a daisy cookie", foos[0].Data["success"], "success")
assert.Equal(t, "outer: inner(as string)", foos[0].Data["oops"], "oops")
}