-
Notifications
You must be signed in to change notification settings - Fork 13
/
data.go
107 lines (98 loc) · 2.35 KB
/
data.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
package filter
import (
"reflect"
)
// Data - Interface representing the data for filter evaluation
type Data interface {
GetValue(filterType string, filterKey string) (string, bool)
GetKeys(filterType string) []string
GetValues(filterType string) []string
}
// AgentFilterData - Represents the data for filter evaluation
type AgentFilterData struct {
tags map[string]string
attr map[string]string
}
// NewFilterData - Transforms the data to flat map which is used for filter evaluation
func NewFilterData(tags interface{}, attr interface{}) Data {
vTags := reflect.ValueOf(tags)
tagsMap := make(map[string]string)
// Todo address other types
if vTags.Kind() == reflect.Map {
for _, key := range vTags.MapKeys() {
value := vTags.MapIndex(key)
vInterface := reflect.ValueOf(value.Interface())
if vInterface.Kind() == reflect.Ptr {
vInterface = vInterface.Elem()
}
if vInterface.Kind() == reflect.String {
keyValue := vInterface.String()
tagsMap[key.String()] = keyValue
}
if vInterface.Kind() == reflect.Slice {
tagsMap[key.String()] = parseStringSliceFilterData(vInterface)
}
}
}
return &AgentFilterData{
tags: tagsMap,
}
}
func parseStringSliceFilterData(v reflect.Value) string {
var val = ""
for i := 0; i < v.Len(); i++ {
vItem := v.Index(i)
if vItem.Kind() == reflect.String {
if len(val) > 0 {
val += ","
}
val += vItem.String()
}
}
return val
}
// GetKeys - Returns all the map keys based on the filter data type
func (ad *AgentFilterData) GetKeys(ftype string) []string {
keys := make([]string, 0)
var m map[string]string
if ftype == "attr" {
m = ad.attr
} else {
m = ad.tags
}
if m != nil {
for k := range m {
keys = append(keys, k)
}
}
return keys
}
// GetValues - Returns all the map values based on the filter data type
func (ad *AgentFilterData) GetValues(ftype string) []string {
values := make([]string, 0)
var m map[string]string
if ftype == "attr" {
m = ad.attr
} else {
m = ad.tags
}
if m != nil {
for _, v := range m {
values = append(values, v)
}
}
return values
}
// GetValue - Returns the value for map entry based on the filter data type
func (ad *AgentFilterData) GetValue(ftype, fName string) (val string, ok bool) {
var m map[string]string
if ftype == "attr" {
m = ad.attr
} else {
m = ad.tags
}
if m != nil {
val, ok = m[fName]
}
return
}