-
Notifications
You must be signed in to change notification settings - Fork 50
/
named.go
149 lines (113 loc) · 2.87 KB
/
named.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
package fixtures
import (
"fmt"
"reflect"
"github.com/justtrackio/gosoline/pkg/funk"
"github.com/justtrackio/gosoline/pkg/mdl"
"github.com/justtrackio/gosoline/pkg/mdlsub"
)
type NamedFixture struct {
Name string
Value interface{}
}
type NamedFixtureSet []*NamedFixture
func (l *NamedFixtureSet) All() []interface{} {
values := make([]interface{}, 0)
for _, named := range *l {
values = append(values, named.Value)
}
return values
}
func (l *NamedFixtureSet) Len() int {
return len(*l)
}
func (l *NamedFixtureSet) CountIf(f func(elem interface{}) bool) int {
count := 0
for _, elem := range *l {
if f(elem.Value) {
count++
}
}
return count
}
func (l *NamedFixtureSet) FindFirst(f func(elem interface{}) bool) (interface{}, bool) {
for _, elem := range *l {
if f(elem.Value) {
return elem.Value, true
}
}
return nil, false
}
func (l *NamedFixtureSet) FindAll(f func(elem interface{}) bool) []interface{} {
a := make([]interface{}, 0)
for _, elem := range *l {
if f(elem.Value) {
a = append(a, elem.Value)
}
}
return a
}
func (l *NamedFixtureSet) GetValueByName(name string) interface{} {
fixture, ok := funk.FindFirstFunc(*l, func(item *NamedFixture) bool {
return item.Name == name
})
if !ok {
panic(fmt.Errorf("failed to get value by name: %s", name))
}
return fixture.Value
}
func (l *NamedFixtureSet) GetValueById(id interface{}) interface{} {
fixture, ok := funk.FindFirstFunc(*l, func(item *NamedFixture) bool {
valueId, ok := GetValueId(item.Value)
return ok && id == valueId
})
if !ok {
panic(fmt.Errorf("failed to get value by id"))
}
return fixture.Value
}
func GetValueId(value interface{}) (interface{}, bool) {
if kvValue, ok := value.(KvStoreFixture); ok {
return GetValueId(kvValue.Value)
}
if kvValue, ok := value.(*KvStoreFixture); ok && kvValue != nil {
return GetValueId(kvValue.Value)
}
if identifiable, ok := value.(mdl.Identifiable); ok {
return mdl.EmptyIfNil(identifiable.GetId()), true
}
model, ok := value.(mdlsub.Model)
if !ok {
return nil, false
}
modelId := model.GetId()
rf := reflect.ValueOf(modelId)
if rf.Kind() != reflect.Ptr {
return modelId, true
}
if mdl.IsNil(modelId) {
return reflect.New(rf.Type().Elem()), true
}
return rf.Elem(), true
}
func GetNamedFixtures(container interface{}) []NamedFixtureSet {
v := reflect.ValueOf(container)
for v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic(fmt.Errorf("expected a struct when getting named fixtureSets out of %T", container))
}
// extract the values of all fields which are a NamedFixtureSet
result := make([]NamedFixtureSet, 0, v.NumField())
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanInterface() {
continue
}
fieldVal := v.Field(i).Interface()
if val, ok := fieldVal.(NamedFixtureSet); ok {
result = append(result, val)
}
}
return result
}