/
add_fields_preprocess.go
36 lines (33 loc) · 1.08 KB
/
add_fields_preprocess.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
package util
import (
"fmt"
log "github.com/sirupsen/logrus"
)
// PreprocessAddedFields preprocesses the added fields to be able to only use
// fast string operations to add them to JSON text later. This code
// progressively builds a JSON snippet by adding JSON key-value pairs for each
// added field, e.g. `, "foo":"bar"`.
func PreprocessAddedFields(fields map[string]string) (string, error) {
j := ""
for k, v := range fields {
// Escape the fields to make sure we do not mess up the JSON when
// encountering weird symbols in field names or values.
kval, err := EscapeJSON(k)
if err != nil {
log.Warningf("cannot escape value: %s", v)
return "", err
}
vval, err := EscapeJSON(v)
if err != nil {
log.Warningf("cannot escape value: %s", v)
return "", err
}
j += fmt.Sprintf(",%s:%s", kval, vval)
}
// We finish the list of key-value pairs with a final brace:
// `, "foo":"bar"}`. This string can now just replace the final brace in a
// given JSON string. If there were no added fields, we just leave the
// output at the final brace.
j += "}"
return j, nil
}