-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
collection.go
224 lines (187 loc) · 4.8 KB
/
collection.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
package rel
import (
"reflect"
)
type slice interface {
table
Reset()
NewDocument() *Document
Append(doc *Document)
Get(index int) *Document
Len() int
Meta() DocumentMeta
}
// Collection provides an abstraction over reflect to easily works with slice for database purpose.
type Collection struct {
v any
rv reflect.Value
rt reflect.Type
meta DocumentMeta
swapper func(i, j int)
}
// ReflectValue of referenced document.
func (c Collection) ReflectValue() reflect.Value {
return c.rv
}
// Table returns name of the table.
func (c *Collection) Table() string {
return c.meta.Table()
}
// PrimaryFields column name of this collection.
func (c Collection) PrimaryFields() []string {
if p, ok := c.v.(primary); ok {
return p.PrimaryFields()
}
if len(c.meta.primaryField) == 0 {
panic("rel: failed to infer primary key for type " + c.rt.String())
}
return c.meta.primaryField
}
// PrimaryField column name of this document.
// panic if document uses composite key.
func (c Collection) PrimaryField() string {
if fields := c.PrimaryFields(); len(fields) == 1 {
return fields[0]
}
panic("rel: composite primary key is not supported")
}
// PrimaryValues of collection.
// Returned value will be interface of slice interface.
func (c Collection) PrimaryValues() []any {
if p, ok := c.v.(primary); ok {
return p.PrimaryValues()
}
var (
index = c.meta.primaryIndex
pValues = make([]any, len(c.PrimaryFields()))
)
if index != nil {
for i := range index {
var (
idxLen = c.rv.Len()
values = make([]any, 0, idxLen)
)
for j := 0; j < idxLen; j++ {
if item := c.rvIndex(j); item.IsValid() {
values = append(values, reflectValueFieldByIndex(item, index[i], false).Interface())
}
}
pValues[i] = values
}
} else {
// using interface.
var (
tmp = make([][]any, len(pValues))
)
for i := 0; i < c.rv.Len(); i++ {
item := c.rvIndex(i)
if !item.IsValid() {
continue
}
for j, id := range item.Interface().(primary).PrimaryValues() {
tmp[j] = append(tmp[j], id)
}
}
for i := range tmp {
pValues[i] = tmp[i]
}
}
return pValues
}
// PrimaryValue of this document.
// panic if document uses composite key.
func (c Collection) PrimaryValue() any {
if values := c.PrimaryValues(); len(values) == 1 {
return values[0]
}
panic("rel: composite primary key is not supported")
}
func (c Collection) rvIndex(index int) reflect.Value {
return reflect.Indirect(c.rv.Index(index))
}
// Get an element from the underlying slice as a document.
func (c Collection) Get(index int) *Document {
return NewDocument(c.rvIndex(index).Addr())
}
// Len of the underlying slice.
func (c Collection) Len() int {
return c.rv.Len()
}
// Meta returns document meta.
func (c Collection) Meta() DocumentMeta {
return c.meta
}
// Reset underlying slice to be zero length.
func (c Collection) Reset() {
c.rv.Set(reflect.MakeSlice(c.rt, 0, 0))
}
// Add new document into collection.
func (c Collection) Add() *Document {
c.Append(c.NewDocument())
return c.Get(c.Len() - 1)
}
// NewDocument returns new document with zero values.
func (c Collection) NewDocument() *Document {
return newZeroDocument(c.rt.Elem())
}
// Append new document into collection.
func (c Collection) Append(doc *Document) {
if c.rt.Elem().Kind() == reflect.Ptr {
c.rv.Set(reflect.Append(c.rv, doc.rv.Addr()))
} else {
c.rv.Set(reflect.Append(c.rv, doc.rv))
}
}
// Truncate collection.
func (c Collection) Truncate(i, j int) {
c.rv.Set(c.rv.Slice(i, j))
}
// Slice returns a new collection that is a slice of the original collection.s
func (c Collection) Slice(i, j int) *Collection {
return NewCollection(c.rv.Slice(i, j), true)
}
// Swap element in the collection.
func (c Collection) Swap(i, j int) {
if c.swapper == nil {
c.swapper = reflect.Swapper(c.rv.Interface())
}
c.swapper(i, j)
}
// NewCollection used to create abstraction to work with slice.
// COllection can be created using interface or reflect.Value.
func NewCollection(entities any, readonly ...bool) *Collection {
switch v := entities.(type) {
case *Collection:
return v
case reflect.Value:
return newCollection(v.Interface(), v, len(readonly) > 0 && readonly[0])
case reflect.Type:
panic("rel: cannot use reflect.Type")
case nil:
panic("rel: cannot be nil")
default:
return newCollection(v, reflect.ValueOf(v), len(readonly) > 0 && readonly[0])
}
}
func newCollection(v any, rv reflect.Value, readonly bool) *Collection {
var (
rt = rv.Type()
)
if rt.Kind() != reflect.Ptr {
if !readonly {
panic("rel: must be a pointer to slice")
}
} else {
rv = rv.Elem()
rt = rt.Elem()
}
if rt.Kind() != reflect.Slice {
panic("rel: must be a slice or pointer to a slice")
}
return &Collection{
v: v,
rv: rv,
rt: rt,
meta: getDocumentMeta(indirectReflectType(rt.Elem()), false),
}
}