-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
124 lines (101 loc) · 2.64 KB
/
helpers.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
package coal
import (
"fmt"
"strings"
"time"
"github.com/globalsign/mgo/bson"
)
// C is a short-hand function to extract the collection of a model.
func C(m Model) string {
return Init(m).Meta().Collection
}
// F is a short-hand function to extract the database BSON field name of a model
// field. Additionally, it supports the "-" prefix for retrieving sort keys.
//
// Note: F will panic if no field has been found.
func F(m Model, field string) string {
// check if prefixed
prefixed := strings.HasPrefix(field, "-")
// remove prefix
if prefixed {
field = strings.TrimLeft(field, "-")
}
// find field
f := Init(m).Meta().Fields[field]
if f == nil {
panic(fmt.Sprintf(`coal: field "%s" not found on "%s"`, field, m.Meta().Name))
}
// get field
bsonField := f.BSONField
// prefix field again
if prefixed {
bsonField = "-" + bsonField
}
return bsonField
}
// A is a short-hand function to extract the attribute JSON key of a model field.
//
// Note: A will panic if no field has been found.
func A(m Model, field string) string {
// find field
f := Init(m).Meta().Fields[field]
if f == nil {
panic(fmt.Sprintf(`coal: field "%s" not found on "%s"`, field, m.Meta().Name))
}
return f.JSONKey
}
// R is a short-hand function to extract the relationship name of a model field.
//
// Note: R will panic if no field has been found.
func R(m Model, field string) string {
// find field
f := Init(m).Meta().Fields[field]
if f == nil {
panic(fmt.Sprintf(`coal: field "%s" not found on "%s"`, field, m.Meta().Name))
}
return f.RelName
}
// P is a short-hand function to get a pointer of the passed object id.
func P(id bson.ObjectId) *bson.ObjectId {
return &id
}
// N is a short-hand function to get a typed nil object id pointer.
func N() *bson.ObjectId {
return nil
}
// T is a short-hand function to get a pointer of a timestamp.
func T(t time.Time) *time.Time {
return &t
}
// Unique is a helper to get a unique list of object ids.
func Unique(ids []bson.ObjectId) []bson.ObjectId {
// prepare map
m := make(map[bson.ObjectId]bool)
l := make([]bson.ObjectId, 0, len(ids))
for _, id := range ids {
if _, ok := m[id]; !ok {
m[id] = true
l = append(l, id)
}
}
return l
}
// Contains returns true if a list of object ids contains the specified id.
func Contains(list []bson.ObjectId, id bson.ObjectId) bool {
for _, item := range list {
if item == id {
return true
}
}
return false
}
// Includes returns true if a list of object ids includes another list of object
// ids.
func Includes(all, subset []bson.ObjectId) bool {
for _, item := range subset {
if !Contains(all, item) {
return false
}
}
return true
}