/
result_bind.go
245 lines (215 loc) · 8.26 KB
/
result_bind.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright 2023 eatmoreapple
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package juice
import (
"errors"
"fmt"
"reflect"
)
// binderRouterDuplicateKeyError is a custom error type that is used when a duplicate key is found in a BinderRouter.
type binderRouterDuplicateKeyError struct {
key string
}
// Error method for binderRouterDuplicateKeyError. It returns a string that represents the error message.
func (b *binderRouterDuplicateKeyError) Error() string {
return "duplicate key " + b.key
}
// BinderRouter is a map that associates a string key to any value.
type BinderRouter map[string]any
// Update add other BinderRouter into current
func updateBinderRouter(current, other BinderRouter) error {
for key, value := range other {
_, ok := current[key]
if ok {
return &binderRouterDuplicateKeyError{key: key}
}
current[key] = value
}
return nil
}
// ResultBinder is an interface that defines a single method BindTo.
// This method takes a reflect.Value and returns a BinderRouter and an error.
type ResultBinder interface {
BindTo(v reflect.Value) (BinderRouter, error)
}
// ResultBinderGroup is a slice of ResultBinders.
type ResultBinderGroup []ResultBinder
// BindTo method for a group of ResultBinders. It iterates over each binder in the group,
// calls its BindTo method, and merges the results into a single BinderRouter.
// If a key is found in more than one binder, it returns an error.
func (r ResultBinderGroup) BindTo(v reflect.Value) (BinderRouter, error) {
var result = make(BinderRouter)
for _, binder := range r {
router, err := binder.BindTo(v)
if err != nil {
return nil, err
}
if err = updateBinderRouter(result, router); err != nil {
return nil, err
}
}
return result, nil
}
// propertyResultBinder is a struct that contains a column and a property string.
type propertyResultBinder struct {
column string
property string
}
// BindTo method for a propertyResultBinder. It checks if the provided reflect.Value is a pointer to a struct,
// then it finds the field with the name of the property in the struct and returns a BinderRouter that associates the column to the address of the field.
func (p *propertyResultBinder) BindTo(v reflect.Value) (BinderRouter, error) {
if v.Kind() != reflect.Ptr {
return nil, errors.New("result must be a pointer")
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("result must be a struct, but got %s", v.Kind())
}
field := v.FieldByName(p.property)
if !field.IsValid() {
return nil, fmt.Errorf("property %s not found", p.property)
}
return BinderRouter{p.column: field.Addr().Interface()}, nil
}
// fromResultNode is a function that takes a resultNode and returns a ResultBinder.
func fromResultNode(r resultNode) ResultBinder {
return &propertyResultBinder{column: r.column, property: r.property}
}
// fromResultNodeGroup is a function that takes a resultGroup and returns a ResultBinderGroup.
func fromResultNodeGroup(rs resultGroup) ResultBinderGroup {
group := make(ResultBinderGroup, 0, len(rs))
for _, r := range rs {
group = append(group, fromResultNode(*r))
}
return group
}
// associationResultBinder is a struct that contains a slice of ResultBinders and a property string.
type associationResultBinder struct {
binders []ResultBinder
property string
}
// BindTo method for an associationResultBinder. It checks if the provided reflect.Value is a pointer to a struct,
// then it finds the field with the name of the property in the struct and calls the BindTo method for each binder in the associationResultBinder.
// It merges the results into a single BinderRouter. If a key is found in more than one binder, it returns an error.
func (a *associationResultBinder) BindTo(v reflect.Value) (BinderRouter, error) {
if v.Kind() != reflect.Ptr {
return nil, errors.New("result must be a pointer")
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("result must be a struct, but got %s", v.Kind())
}
field := v.FieldByName(a.property)
if !field.IsValid() {
return nil, fmt.Errorf("property %s not found", a.property)
}
if field.Kind() == reflect.Ptr {
if field.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("pointer type only support struct pointer")
}
field.Set(reflect.New(field.Type()))
field = field.Elem()
} else if field.Kind() != reflect.Struct {
return nil, fmt.Errorf("property %s must be a struct", a.property)
}
var result = make(BinderRouter)
for _, binder := range a.binders {
router, err := binder.BindTo(field.Addr())
if err != nil {
return nil, err
}
if err = updateBinderRouter(result, router); err != nil {
return nil, err
}
}
return result, nil
}
// fromAssociation is a function that takes an association and returns a ResultBinder.
func fromAssociation(association association) ResultBinder {
var binders = make([]ResultBinder, 0, len(association.results))
for _, result := range association.results {
binders = append(binders, fromResultNode(*result))
}
for _, association := range association.associations {
binders = append(binders, fromAssociation(*association))
}
return &associationResultBinder{binders: binders, property: association.property}
}
// fromAssociationGroup is a function that takes an associationGroup and returns a ResultBinderGroup.
func fromAssociationGroup(list associationGroup) ResultBinderGroup {
group := make(ResultBinderGroup, 0, len(list))
for _, a := range list {
group = append(group, fromAssociation(*a))
}
return group
}
// collectionResultBinder is a struct that contains a slice of ResultBinders and a property string.
type collectionResultBinder struct {
binders []ResultBinder
property string
}
// BindTo method for a collectionResultBinder. It checks if the provided reflect.Value is a pointer to a struct,
// then it finds the field with the name of the property in the struct and calls the BindTo method for each binder in the collectionResultBinder.
// It merges the results into a single BinderRouter. If a key is found in more than one binder, it returns an error.
func (c *collectionResultBinder) BindTo(v reflect.Value) (BinderRouter, error) {
if v.Kind() != reflect.Ptr {
return nil, errors.New("result must be a pointer")
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return nil, fmt.Errorf("result must be a struct, but got %s", v.Kind())
}
field := v.FieldByName(c.property)
if !field.IsValid() {
return nil, fmt.Errorf("property %s not found", c.property)
}
if field.Kind() != reflect.Slice {
return nil, fmt.Errorf("property %s must be a slice", c.property)
}
elem := field.Type().Elem()
if elem.Kind() != reflect.Ptr {
return nil, fmt.Errorf("property %s must be a slice of pointer", c.property)
}
instance := reflect.New(elem.Elem())
var result = make(BinderRouter)
for _, binder := range c.binders {
router, err := binder.BindTo(instance)
if err != nil {
return nil, err
}
if err = updateBinderRouter(result, router); err != nil {
return nil, err
}
}
field.Set(reflect.Append(field, instance))
return result, nil
}
// fromCollection is a function that takes a collection and returns a ResultBinder.
func fromCollection(collection collection) ResultBinder {
var binders = make([]ResultBinder, 0, len(collection.resultGroup))
for _, result := range collection.resultGroup {
binders = append(binders, fromResultNode(*result))
}
for _, association := range collection.associationGroup {
binders = append(binders, fromAssociation(*association))
}
return &collectionResultBinder{binders: binders, property: collection.property}
}
// fromCollectionGroup is a function that takes a collectionGroup and returns a ResultBinderGroup.
func fromCollectionGroup(list collectionGroup) ResultBinderGroup {
group := make(ResultBinderGroup, 0, len(list))
for _, c := range list {
group = append(group, fromCollection(*c))
}
return group
}