This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
field.go
152 lines (132 loc) · 3.21 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package processors
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/clbanning/mxj"
"github.com/vjeantet/jodaTime"
)
const (
mark = "%{"
mark_time = "%{+"
)
var maskPattern *regexp.Regexp
var maskTimePattern *regexp.Regexp
var nestedPathPattern *regexp.Regexp
func init() {
maskTimePattern, _ = regexp.Compile(`%\{\+([^\}]+)\}`)
maskPattern, _ = regexp.Compile(`%\{\[?([^\}]*[^\]])\]?\}`)
nestedPathPattern, _ = regexp.Compile(`\[([^0-9\]]+)\]`)
}
// Dynamic includes field value in place of %{key.path}
// When no field is not found replace with ""
func Dynamic(str *string, fields *mxj.Map) {
if true == strings.Contains(*str, mark_time) {
// Search for all %{+word}
for _, values := range maskTimePattern.FindAllStringSubmatch(*str, -1) {
// Search matching value, when not found use ""
t, err := fields.ValueForPath("@timestamp")
if err != nil {
continue
}
switch tt := t.(type) {
case time.Time:
*str = strings.Replace(*str, values[0], jodaTime.Format(values[1], tt), -1)
}
}
}
if true == strings.Contains(*str, mark) {
// Search for all %{word}
for _, values := range maskPattern.FindAllStringSubmatch(*str, -1) {
values[1] = strings.Replace(values[1], `][`, `.`, -1)
// Search matching value, when not found use ""
i, _ := fields.ValueForPath(values[1])
if i == nil {
i = ""
}
*str = strings.Replace(*str, values[0], fmt.Sprintf("%v", i), -1)
}
}
}
func NormalizeNestedPath(path string) string {
if strings.Contains(path, "[") {
path = strings.TrimPrefix(nestedPathPattern.ReplaceAllString(path, ".$1"), ".")
}
return path
}
func SetType(typevalue string, data *mxj.Map) {
Dynamic(&typevalue, data)
if !data.Exists("type") {
data.SetValueForPath(typevalue, "type")
}
}
func AddFields(fields map[string]interface{}, data *mxj.Map) {
for k, v := range fields {
Dynamic(&k, data)
if !data.Exists(k) {
switch v.(type) {
case string:
d := v.(string)
Dynamic(&d, data)
v = d
}
data.SetValueForPath(v, k)
}
}
}
func AddTags(tags []string, data *mxj.Map) {
if len(tags) == 0 {
return
}
var currentTags []string
currentTagsInterface, _ := data.ValueForPath("tags")
switch v := currentTagsInterface.(type) {
case string:
currentTags = []string{v}
case []string:
currentTags = currentTagsInterface.([]string)
default:
currentTags = []string{}
}
tagsEval := []string{}
for _, t := range tags {
Dynamic(&t, data)
if !isInSlice(t, currentTags) {
tagsEval = append(tagsEval, t)
}
}
newTags := append(currentTags, tagsEval...)
data.SetValueForPath(newTags, "tags")
}
func isInSlice(needle string, candidates []string) bool {
for _, symbolType := range candidates {
if needle == symbolType {
return true
}
}
return false
}
func RemoveTags(tags []string, data *mxj.Map) {
currenttags, err := data.ValueForPath("tags")
if err != nil {
return
}
ct := currenttags.([]string)
for i, t := range ct {
for _, ttodelete := range tags {
Dynamic(&ttodelete, data)
if ttodelete == t {
//delete
ct = append(ct[:i], ct[i+1:]...)
}
}
}
data.SetValueForPath(ct, "tags")
}
func RemoveFields(fields []string, data *mxj.Map) {
for _, k := range fields {
Dynamic(&k, data)
data.Remove(k)
}
}