forked from nauyey/factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to.go
164 lines (133 loc) · 3.8 KB
/
to.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
package factory
import (
"database/sql"
"fmt"
"reflect"
)
const (
invalidTargetTypeErr = "cannot use target (type *%v) as type *%v in func To"
invalidTargetSliceTypeErr = "cannot use target (type []*%v) as type []*%v in func To"
)
// to is the interface that wraps the basic To method.
//
// To sets the value of instance built by strategies to the target value.
// It returns error if any errors encountered.
type to interface {
To(target interface{}) error
}
type buildTo struct {
blueprint *blueprint
}
func (to *buildTo) To(target interface{}) error {
if err := checkTargetType(to.blueprint.factory.ModelType, target); err != nil {
return err
}
instanceIface, err := to.blueprint.build()
if err != nil {
return err
}
setValue(target, instanceIface)
return nil
}
type buildSliceTo struct {
blueprint *blueprint
count int
}
func (to *buildSliceTo) To(target interface{}) error {
targetType, targetValue := targetTypeAndValue(target)
elemType, isPtrElem := elemTypeOf(targetType)
// check element type of target slice
if err := checkTargetSliceType(to.blueprint.factory.ModelType, elemType); err != nil {
return err
}
sliceValue := reflect.MakeSlice(targetType, 0, to.count)
for i := 0; i < to.count; i++ {
elemIface, err := to.blueprint.build()
if err != nil {
return err
}
sliceValue = appendSliceValue(sliceValue, isPtrElem, reflect.ValueOf(elemIface))
}
targetValue.Set(sliceValue)
return nil
}
type createTo struct {
blueprint *blueprint
dbConnection *sql.DB
}
func (to *createTo) To(target interface{}) error {
if err := checkTargetType(to.blueprint.factory.ModelType, target); err != nil {
return err
}
instanceIface, err := to.blueprint.create(to.dbConnection)
if err != nil {
return err
}
setValue(target, instanceIface)
return nil
}
type createSliceTo struct {
blueprint *blueprint
count int
dbConnection *sql.DB
}
func (to *createSliceTo) To(target interface{}) error {
targetType, targetValue := targetTypeAndValue(target)
elemType, isPtrElem := elemTypeOf(targetType)
// check element type of target slice
if err := checkTargetSliceType(to.blueprint.factory.ModelType, elemType); err != nil {
return err
}
sliceValue := reflect.MakeSlice(targetType, 0, to.count)
for i := 0; i < to.count; i++ {
elemIface, err := to.blueprint.create(to.dbConnection)
if err != nil {
return err
}
sliceValue = appendSliceValue(sliceValue, isPtrElem, reflect.ValueOf(elemIface))
}
targetValue.Set(sliceValue)
return nil
}
func targetTypeAndValue(target interface{}) (reflect.Type, reflect.Value) {
targetType := reflect.TypeOf(target)
targetValue := reflect.ValueOf(target)
if targetType.Kind() == reflect.Ptr {
targetType = targetType.Elem()
targetValue = targetValue.Elem()
}
return targetType, targetValue
}
func elemTypeOf(targetType reflect.Type) (elemType reflect.Type, isPtrElem bool) {
elemType = targetType.Elem()
if elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
isPtrElem = true
}
return
}
func checkTargetType(wantType reflect.Type, target interface{}) error {
typ := reflect.TypeOf(target).Elem()
if typ != wantType {
return fmt.Errorf(invalidTargetTypeErr, typ, wantType)
}
return nil
}
func checkTargetSliceType(wantType, targetSliceElemType reflect.Type) error {
if targetSliceElemType != wantType {
return fmt.Errorf(invalidTargetSliceTypeErr, targetSliceElemType, wantType)
}
return nil
}
func setValue(dest interface{}, src interface{}) {
targetValue := reflect.ValueOf(dest).Elem()
targetValue.Set(reflect.ValueOf(src).Elem())
}
func appendSliceValue(sliceValue reflect.Value, isPtrElem bool, elemValue reflect.Value) reflect.Value {
if isPtrElem {
sliceValue = reflect.Append(sliceValue, elemValue)
} else {
sliceValue = reflect.Append(sliceValue, elemValue.Elem())
}
return sliceValue
}