forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
186 lines (162 loc) · 4.25 KB
/
model.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
package pop
import (
"fmt"
"reflect"
"sync"
"time"
"github.com/markbates/inflect"
"github.com/markbates/validate"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
)
var tableMap = map[string]string{}
var tableMapMu = sync.RWMutex{}
// MapTableName allows for the customize table mapping
// between a name and the database. For example the value
// `User{}` will automatically map to "users".
// MapTableName would allow this to change.
//
// m := &pop.Model{Value: User{}}
// m.TableName() // "users"
//
// pop.MapTableName("user", "people")
// m = &pop.Model{Value: User{}}
// m.TableName() // "people"
func MapTableName(name string, tableName string) {
defer tableMapMu.Unlock()
tableMapMu.Lock()
tableMap[name] = tableName
}
type Value interface{}
// Model is used throughout Pop to wrap the end user interface
// that is passed in to many functions.
type Model struct {
Value
tableName string
}
func (m *Model) runValidations(c *Connection, names ...string) (*validate.Errors, error) {
for _, n := range names {
rv := reflect.ValueOf(m.Value)
mv := rv.MethodByName(n)
if mv.IsValid() {
if mv.Type().NumOut() < 2 {
return nil, errors.Errorf("%s does not have the correct method signature!", n)
}
out := mv.Call([]reflect.Value{reflect.ValueOf(c)})
verrs := validate.NewErrors()
var err error
if !out[0].IsNil() {
verrs = out[0].Interface().(*validate.Errors)
}
if !out[1].IsNil() {
err = out[1].Interface().(error)
}
if verrs.HasAny() || err != nil {
return verrs, err
}
}
}
return validate.NewErrors(), nil
}
func (m *Model) validate(c *Connection) (*validate.Errors, error) {
return m.runValidations(c, "Validate")
}
func (m *Model) validateCreate(c *Connection) (*validate.Errors, error) {
return m.runValidations(c, "Validate", "ValidateCreate")
}
func (m *Model) validateSave(c *Connection) (*validate.Errors, error) {
return m.runValidations(c, "Validate", "ValidateSave")
}
func (m *Model) validateUpdate(c *Connection) (*validate.Errors, error) {
return m.runValidations(c, "Validate", "ValidateUpdate")
}
// ID returns the ID of the Model. All models must have an `ID` field this is
// of type `int` or of type `uuid.UUID`.
func (m *Model) ID() interface{} {
fbn, err := m.fieldByName("ID")
if err != nil {
return 0
}
if m.PrimaryKeyType() == "UUID" {
return fbn.Interface().(uuid.UUID).String()
}
return fbn.Interface()
}
func (m *Model) PrimaryKeyType() string {
fbn, err := m.fieldByName("ID")
if err != nil {
return "int"
}
return fbn.Type().Name()
}
// TableName returns the corresponding name of the underlying database table
// for a given `Model`. See also `MapTableName` to change the default name of
// the table.
func (m *Model) TableName() string {
if m.tableName != "" {
return m.tableName
}
t := reflect.TypeOf(m.Value)
name := m.typeName(t)
defer tableMapMu.Unlock()
tableMapMu.Lock()
if tableMap[name] == "" {
m.tableName = inflect.Tableize(name)
tableMap[name] = m.tableName
}
return tableMap[name]
}
func (m *Model) typeName(t reflect.Type) string {
kind := t.Kind().String()
switch kind {
case "ptr":
st := reflect.ValueOf(m.Value).Elem()
return m.typeName(st.Type())
case "string":
return m.Value.(string)
case "slice":
if t.Elem().Kind().String() == "ptr" {
return m.typeName(t.Elem().Elem())
}
return t.Elem().Name()
default:
return t.Name()
}
}
func (m *Model) fieldByName(s string) (reflect.Value, error) {
el := reflect.ValueOf(m.Value).Elem()
fbn := el.FieldByName(s)
if !fbn.IsValid() {
return fbn, errors.Errorf("Model does not have a field named %s", s)
}
return fbn, nil
}
func (m *Model) associationName() string {
tn := inflect.Singularize(m.TableName())
return fmt.Sprintf("%s_id", tn)
}
func (m *Model) setID(i interface{}) {
fbn, err := m.fieldByName("ID")
if err == nil {
fbn.Set(reflect.ValueOf(i))
}
}
func (m *Model) touchCreatedAt() {
fbn, err := m.fieldByName("CreatedAt")
if err == nil {
fbn.Set(reflect.ValueOf(time.Now()))
}
}
func (m *Model) touchUpdatedAt() {
fbn, err := m.fieldByName("UpdatedAt")
if err == nil {
fbn.Set(reflect.ValueOf(time.Now()))
}
}
func (m *Model) whereID() string {
id := m.ID()
if _, ok := id.(int); ok {
return fmt.Sprintf("id = %d", id)
}
return fmt.Sprintf("id ='%s'", id)
}