/
attributes.go
51 lines (43 loc) · 1.04 KB
/
attributes.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
// SPDX-FileCopyrightText: 2020 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
type BasicAttributes map[string]interface{}
func (a BasicAttributes) Get(key string) (interface{}, bool) {
v, ok := a[key]
return v, ok
}
// NewAttributes builds an Attributes instance with
// the given map as datasource.
func NewAttributes(m map[string]interface{}) Attributes {
return BasicAttributes(m)
}
// GetNestedAttribute uses multiple keys in order to obtain an attribute.
func GetNestedAttribute(attributes Attributes, keys ...string) (interface{}, bool) {
// need at least one key.
if len(keys) == 0 {
return nil, false
}
var (
result interface{}
ok bool
)
result = attributes
for _, k := range keys {
var a Attributes
switch r := result.(type) {
case BasicAttributes:
a = r
case Attributes:
a = r
case map[string]interface{}:
a = BasicAttributes(r)
default:
return nil, false
}
result, ok = a.Get(k)
if !ok {
return nil, false
}
}
return result, ok
}