-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
properties.go
168 lines (151 loc) · 4.63 KB
/
properties.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package biloba
func newProperties(input any) Properties {
x := input.(map[string]any)
return x
}
func newSliceOfProperties(input []any) SliceOfProperties {
out := make(SliceOfProperties, len(input))
for i, input := range input {
out[i] = newProperties(input)
}
return out
}
/*
Properties is returned by [Biloba.GetProperties] and provides getters that perform type assertions and nil-conversions for you
For each of the Get* methods if the requested property does not exist or is nil, the getter will return the zero value of the associated type (or an empty slice of correct type for the Get*Slice methods)
Read https://onsi.github.io/biloba/#properties to learn more about properties
*/
type Properties map[string]any
func (p Properties) Get(k string) any { return p[k] }
func (p Properties) GetString(k string) string { return toString(p[k]) }
func (p Properties) GetInt(k string) int { return toInt(p[k]) }
func (p Properties) GetFloat64(k string) float64 { return toFloat64(p[k]) }
func (p Properties) GetBool(k string) bool { return toBool(p[k]) }
func (p Properties) GetStringSlice(k string) []string { return toStringSlice(p[k]) }
func (p Properties) GetAnySlice(k string) []any { return toAnySlice(p[k]) }
/*
SliceOfProperties has underlying type []Properties and is returned by [GetPropertiesForEach]. SliceOfProperties provides getters that perform type assertions and nil-conversions for you.
For each of the Get* methods the return value is a slice of appropriate type. The slice will have the same length as the SliceOfProperties instance and will be filled with the values returned by each Properties invocation of Get* (i.e. nil and missing types will return the zero value.)
Read https://onsi.github.io/biloba/#properties to learn more about properties
*/
type SliceOfProperties []Properties
/*
Find() the first Properties in SliceOfProperties that satisfies the provided search criteria.
If search is a a Gomega matcher then the return Properties will have a value for key k that is successfully matched by the matcher.
# If search is not a matcher then Equal(search) is used to perform the match
Read https://onsi.github.io/biloba/#properties to learn more about properties
*/
func (sp SliceOfProperties) Find(k string, search any) Properties {
matcher := matcherOrEqual(search)
for _, p := range sp {
if matches, _ := matcher.Match(p[k]); matches {
return p
}
}
return nil
}
/*
Filter() returns the subset of Properties in SliceOfProperties that satisfies the provided search criteria.
The search behaves similarly to [Biloba.Find].
Read https://onsi.github.io/biloba/#properties to learn more about properties
*/
func (sp SliceOfProperties) Filter(k string, search any) SliceOfProperties {
out := SliceOfProperties{}
matcher := matcherOrEqual(search)
for _, p := range sp {
if matches, _ := matcher.Match(p[k]); matches {
out = append(out, p)
}
}
return out
}
func (sp SliceOfProperties) Get(k string) []any {
out := make([]any, len(sp))
for i, p := range sp {
out[i] = p.Get(k)
}
return out
}
func (sp SliceOfProperties) GetString(k string) []string {
out := make([]string, len(sp))
for i, p := range sp {
out[i] = p.GetString(k)
}
return out
}
func (sp SliceOfProperties) GetInt(k string) []int {
out := make([]int, len(sp))
for i, p := range sp {
out[i] = p.GetInt(k)
}
return out
}
func (sp SliceOfProperties) GetFloat64(k string) []float64 {
out := make([]float64, len(sp))
for i, p := range sp {
out[i] = p.GetFloat64(k)
}
return out
}
func (sp SliceOfProperties) GetBool(k string) []bool {
out := make([]bool, len(sp))
for i, p := range sp {
out[i] = p.GetBool(k)
}
return out
}
func (sp SliceOfProperties) GetStringSlice(k string) [][]string {
out := make([][]string, len(sp))
for i, p := range sp {
out[i] = p.GetStringSlice(k)
}
return out
}
func (sp SliceOfProperties) GetAnySlice(k string) [][]any {
out := make([][]any, len(sp))
for i, p := range sp {
out[i] = p.GetAnySlice(k)
}
return out
}
func toString(input any) string {
if input == nil {
return ""
}
return input.(string)
}
func toBool(input any) bool {
if input == nil {
return false
}
return input.(bool)
}
func toInt(input any) int {
if input == nil {
return 0
}
return int(input.(float64))
}
func toFloat64(input any) float64 {
if input == nil {
return 0
}
return input.(float64)
}
func toAnySlice(input any) []any {
if input == nil {
return []any{}
}
return input.([]any)
}
func toStringSlice(input any) []string {
if input == nil {
return []string{}
}
vs := input.([]any)
out := make([]string, len(vs))
for i, v := range vs {
out[i] = toString(v)
}
return out
}