-
Notifications
You must be signed in to change notification settings - Fork 785
/
field_map.go
54 lines (45 loc) · 1.15 KB
/
field_map.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
package kube
import (
"fmt"
"strings"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/util"
)
// FieldMap is a map of field:value. It implements fields.Fields.
type fieldMap map[string]interface{}
func newFieldMap(pipelineActivity v1.PipelineActivity) (fieldMap, error) {
return util.ToObjectMap(pipelineActivity)
}
// Has returns whether the provided field exists in the map.
func (m fieldMap) Has(field string) bool {
_, exists := m.get(field)
return exists
}
// Get returns the value in the map for the provided field.
func (m fieldMap) Get(field string) string {
val, _ := m.get(field)
return val
}
// Get returns the value in the map for the provided field.
func (m fieldMap) get(field string) (string, bool) {
pathElements := strings.Split(field, ".")
valueMap := m
value := ""
for i, element := range pathElements {
tmp, exists := valueMap[element]
if !exists {
return "", false
}
if i == len(pathElements)-1 {
value = fmt.Sprintf("%v", tmp)
} else {
switch v := tmp.(type) {
case map[string]interface{}:
valueMap = v
default:
return "", false
}
}
}
return value, true
}