-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
165 lines (146 loc) · 3.11 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
package rest
import (
"errors"
"net/http"
"reflect"
)
// Serializer - could be used for JSON marshalling and unmarshalling
type Serializer interface {
Decode() error
Encode(v interface{}) ([]byte, error)
UseContext(*Context)
}
// Validator - input validation
type Validator interface {
Validate() error
UseContext(*Context)
}
// Storage - database abstraction
type Storage interface {
InsertOne() error
InsertMany() error
Remove() error
FindOne() error
FindMany() error
Update() error
Upsert() error
UseContext(*Context)
}
// Response - holds the data to be sent to the client
type Response struct {
Body interface{}
Headers map[string][]string
Status int
}
// Model -
type Model struct {
Name string
Context
Storage
Validator
Serializer
}
const (
// INSERTONE -
INSERTONE = "insertOne"
// INSERTMANY -
INSERTMANY = "insertMany"
// UPDATE -
UPDATE = "update"
// UPSERT -
UPSERT = "upsert"
// FINDONE -
FINDONE = "findOne"
// FINDMANY -
FINDMANY = "findMany"
// REMOVE -
REMOVE = "remove"
)
// Execute -
func (model *Model) Execute(action string) error {
switch {
case action == INSERTONE:
return model.InsertOne()
case action == INSERTMANY:
return model.InsertMany()
case action == UPDATE:
return model.Update()
case action == UPSERT:
return model.Upsert()
case action == FINDONE:
return model.FindOne()
case action == FINDMANY:
return model.FindMany()
case action == REMOVE:
return model.Remove()
}
return errors.New("Action must be one of [insertOne, insertMany, update, upsert, findOne, findMany, remove]")
}
// UseStorage -
func (model *Model) UseStorage(s Storage) {
s.UseContext(&model.Context)
model.Storage = s
}
// UseSerializer -
func (model *Model) UseSerializer(s Serializer) {
s.UseContext(&model.Context)
model.Serializer = s
}
// UseValidator -
func (model *Model) UseValidator(s Validator) {
s.UseContext(&model.Context)
model.Validator = s
}
// Resource -
type Resource struct {
Name string
Type reflect.Type
Headers map[string][]string
Storage Storage
Validator Validator
Serializer Serializer
}
// NewModel -
func (r *Resource) NewModel(req *http.Request, action string) *Model {
model := Model{}
model.Name = r.Name
model.Context = NewContext()
model.Context.Set("action", action)
model.Context.Set("request", req)
model.Context.Set("response", Response{Headers: r.Headers})
model.Context.Set("type", r.Type)
model.UseStorage(r.Storage)
model.UseValidator(r.Validator)
model.UseSerializer(r.Serializer)
return &model
}
// UseType -
func (r *Resource) UseType(t reflect.Type) *Resource {
r.Type = t
return r
}
// UseHeaders -
func (r *Resource) UseHeaders(headers map[string][]string) *Resource {
r.Headers = headers
return r
}
// UseStorage -
func (r *Resource) UseStorage(s Storage) *Resource {
r.Storage = s
return r
}
// UseValidator -
func (r *Resource) UseValidator(v Validator) *Resource {
r.Validator = v
return r
}
// UseSerializer -
func (r *Resource) UseSerializer(s Serializer) *Resource {
r.Serializer = s
return r
}
// NewResource -
func NewResource(name string) *Resource {
r := &Resource{Name: name}
return r
}