-
Notifications
You must be signed in to change notification settings - Fork 6
/
eval_log.go
115 lines (102 loc) · 2.43 KB
/
eval_log.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package configcat
import (
"bytes"
"fmt"
"strings"
"time"
)
var indentBytes = []byte(" ")
const (
newLineByte byte = '\n'
stringListMaxLength = 10
)
type evalLogBuilder struct {
builder strings.Builder
indentLevel int
user User
}
func (b *evalLogBuilder) userAsString() string {
return fmt.Sprintf("%#v", b.user)
}
func (b *evalLogBuilder) resetIndent() *evalLogBuilder {
b.indentLevel = 0
return b
}
func (b *evalLogBuilder) incIndent() *evalLogBuilder {
b.indentLevel++
return b
}
func (b *evalLogBuilder) decIndent() *evalLogBuilder {
b.indentLevel--
return b
}
func (b *evalLogBuilder) newLine() *evalLogBuilder {
b.builder.WriteByte(newLineByte)
b.builder.Write(bytes.Repeat(indentBytes, b.indentLevel))
return b
}
func (b *evalLogBuilder) newLineString(msg string) *evalLogBuilder {
b.newLine().builder.WriteString(msg)
return b
}
func (b *evalLogBuilder) append(val interface{}) *evalLogBuilder {
b.builder.WriteString(fmt.Sprintf("%v", val))
return b
}
func (b *evalLogBuilder) appendUserCondition(comparisonAttribute string, op Comparator, comparisonValue interface{}) *evalLogBuilder {
b.append(fmt.Sprintf("User.%s %s ", comparisonAttribute, op.String()))
if comparisonValue == nil {
return b.append("<invalid value>")
}
switch val := comparisonValue.(type) {
case float64:
if op.IsDateTime() {
t := time.UnixMilli(int64(val) * 1000)
return b.append(fmt.Sprintf("'%.0f' (%s)", val, t.UTC()))
} else {
return b.append(fmt.Sprintf("'%g'", val))
}
case string:
var res string
if op.IsSensitive() {
res = "<hashed value>"
} else {
res = val
}
return b.append(fmt.Sprintf("'%s'", res))
case []string:
if op.IsSensitive() {
var valText string
if len(val) > 1 {
valText = "values"
} else {
valText = "value"
}
return b.append(fmt.Sprintf("[<%d hashed %s>]", len(val), valText))
} else {
length := len(val)
var valText string
if length-stringListMaxLength > 1 {
valText = "values"
} else {
valText = "value"
}
var res string
limit := length
if limit > stringListMaxLength {
limit = stringListMaxLength
}
for i, item := range val {
res += "'" + item + "'"
if i < limit-1 {
res += ", "
} else if length > stringListMaxLength {
res += fmt.Sprintf(", ... <%d more %s>", length-stringListMaxLength, valText)
break
}
}
return b.append(fmt.Sprintf("[%s]", res))
}
}
return b
}