forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
190 lines (156 loc) · 3.77 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
package rel
import (
"reflect"
)
type slice interface {
table
Reset()
Add() *Document
Get(index int) *Document
Len() int
}
// Collection provides an abstraction over reflect to easily works with slice for database purpose.
type Collection struct {
v interface{}
rv reflect.Value
rt reflect.Type
data documentData
index map[interface{}]int
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 {
if tn, ok := c.v.(table); ok {
return tn.Table()
}
return c.tableName()
}
func (c Collection) tableName() string {
var (
rt = c.rt.Elem()
)
// check for cache
if name, cached := tablesCache.Load(rt); cached {
return name.(string)
}
if rt.Implements(rtTable) {
var (
v = reflect.Zero(rt).Interface().(table)
)
tablesCache.Store(rt, v.Table())
return v.Table()
}
return tableName(rt)
}
// PrimaryField column name of this collection.
func (c Collection) PrimaryField() string {
if p, ok := c.v.(primary); ok {
return p.PrimaryField()
}
if c.data.primaryField == "" {
panic("rel: failed to infer primary key for type " + c.rt.String())
}
return c.data.primaryField
}
// PrimaryValue of collection.
// Returned value will be interface of slice interface.
func (c Collection) PrimaryValue() interface{} {
if p, ok := c.v.(primary); ok {
return p.PrimaryValue()
}
var (
index = c.data.primaryIndex
ids = make([]interface{}, c.rv.Len())
)
for i := 0; i < len(ids); i++ {
var (
fv = c.rv.Index(i)
)
if index == -2 {
// using interface
ids[i] = fv.Interface().(primary).PrimaryValue()
} else {
ids[i] = fv.Field(index).Interface()
}
}
return ids
}
// Get an element from the underlying slice as a document.
func (c Collection) Get(index int) *Document {
return NewDocument(c.rv.Index(index).Addr())
}
// Len of the underlying slice.
func (c Collection) Len() int {
return c.rv.Len()
}
// 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 {
var (
index = c.Len()
typ = c.rt.Elem()
drv = reflect.Zero(typ)
)
c.rv.Set(reflect.Append(c.rv, drv))
return NewDocument(c.rv.Index(index).Addr())
}
// 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(records interface{}, readonly ...bool) *Collection {
switch v := records.(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 interface{}, 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,
data: extractDocumentData(rt.Elem(), false),
}
}