-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.go
87 lines (71 loc) · 1.58 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
package gitdb
import (
"time"
)
//Model interface describes methods GitDB supports
type Model interface {
GetSchema() *Schema
//Validate validates a Model
Validate() error
//ShouldEncrypt informs GitDb if a Model support encryption
ShouldEncrypt() bool
//BeforeInsert is called by gitdb before insert
BeforeInsert() error
}
type LockableModel interface {
//GetLockFileNames informs GitDb of files a Models using for locking
GetLockFileNames() []string
}
//TimeStampedModel provides time stamp fields
type TimeStampedModel struct {
CreatedAt time.Time
UpdatedAt time.Time
}
//BeforeInsert implements Model.BeforeInsert
func (m *TimeStampedModel) BeforeInsert() error {
stampTime := time.Now()
if m.CreatedAt.IsZero() {
m.CreatedAt = stampTime
}
m.UpdatedAt = stampTime
return nil
}
type model struct {
Version string
Data Model
}
func wrap(m Model) *model {
return &model{
Version: RecVersion,
Data: m,
}
}
func (m *model) GetSchema() *Schema {
return m.Data.GetSchema()
}
func (m *model) Validate() error {
return m.Data.Validate()
}
func (m *model) ShouldEncrypt() bool {
return m.Data.ShouldEncrypt()
}
func (m *model) BeforeInsert() error {
err := m.Data.BeforeInsert()
return err
}
func (g *gitdb) RegisterModel(dataset string, m Model) bool {
if g.registry == nil {
g.registry = make(map[string]Model)
}
g.registry[dataset] = m
return true
}
func (g *gitdb) isRegistered(dataset string) bool {
if _, ok := g.registry[dataset]; ok {
return true
}
if g.config.Factory != nil && g.config.Factory(dataset) != nil {
return true
}
return false
}