-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
190 lines (147 loc) · 5.91 KB
/
type.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 content
import (
"errors"
"github.com/blevesearch/bleve/mapping"
"github.com/gofrs/uuid"
"net/http"
"net/url"
)
type Creator func() interface{}
type Identifier interface {
ID() string
ContentType() string
}
type Content interface {
AllContentTypeNames() []string
AllContentTypes() map[string]Creator
NormalizeString(s string) (string, error)
GetContentCreator(string) (Creator, bool)
GetContents([]Identifier) ([][]byte, error)
//GetContent Todo, convert to identifier
GetContent(contentType, id, status string) ([]byte, error)
DeleteContent(contentType, id, status string) error
NewContent(contentType string, data url.Values) (string, error)
UpdateContent(contentType string, data url.Values) error
}
type Status string
const (
Public Status = "public"
Pending Status = "pending"
)
// Hideable lets a user keep items hidden
type Hideable interface {
Hide(http.ResponseWriter, *http.Request) error
}
// Pushable lets a user define which values of certain struct fields are
// 'pushed' down to a client via HTTP/2 Server Push. All items in the slice
// should be the json tag names of the struct fields to which they correspond.
type Pushable interface {
// Push the values contained by fields returned by Push must strictly be URL paths
Push(http.ResponseWriter, *http.Request) ([]string, error)
}
// Omittable lets a user define certin fields within a content struct to remove
// from an API response. Helpful when you want data in the CMS, but not entirely
// shown or available from the content API. All items in the slice should be the
// json tag names of the struct fields to which they correspond.
type Omittable interface {
Omit(http.ResponseWriter, *http.Request) ([]string, error)
}
// Createable accepts or rejects external POST requests to endpoints such as:
// /api/content/create?type=Review
type Createable interface {
// Create enables external clients to submit content of a specific type
Create(http.ResponseWriter, *http.Request) error
}
// Trustable allows external content to be auto-approved, meaning content sent
// as an Createable will be stored in the public content bucket
type Trustable interface {
AutoApprove(http.ResponseWriter, *http.Request) error
}
// Sortable ensures data is sortable by time
type Sortable interface {
Time() int64
Touch() int64
}
// CSVFormattable is implemented with the method FormatCSV, which must return the ordered
// slice of JSON struct tag names for the type implmenting it
type CSVFormattable interface {
FormatCSV() []string
}
// Sluggable makes a struct locatable by URL with it's own path.
// As an Item implementing Sluggable, slugs may overlap. If this is an issue,
// make your content struct (or one which embeds Item) implement Sluggable
// and it will override the slug created by Item's SetSlug with your own
type Sluggable interface {
SetSlug(string)
ItemSlug() string
}
// Searchable ...
type Searchable interface {
SearchMapping() (*mapping.IndexMappingImpl, error)
IndexContent() bool
}
// Identifiable enables a struct to have its ID set/get. Typically this is done
// to set an ID to -1 indicating it is new for DB inserts, since by default
// a newly initialized struct would have an ID of 0, the int zero-value, and
// bbolt's starting key per bucket is 0, thus overwriting the first record.
type Identifiable interface {
ItemID() int
SetItemID(int)
UniqueID() uuid.UUID
SetUniqueID(uuid.UUID)
String() string
ItemName() string
}
type Statusable interface {
ItemStatus() Status
SetItemStatus(Status)
}
// Hookable provides our user with an easy way to intercept or add functionality
// to the different lifecycles/events a struct may encounter. Item implements
// Hookable with no-ops so our user can override only whichever ones necessary.
type Hookable interface {
BeforeAPIResponse(http.ResponseWriter, *http.Request, []byte) ([]byte, error)
AfterAPIResponse(http.ResponseWriter, *http.Request, []byte) error
BeforeAPICreate(http.ResponseWriter, *http.Request) error
AfterAPICreate(http.ResponseWriter, *http.Request) error
BeforeAPIUpdate(http.ResponseWriter, *http.Request) error
AfterAPIUpdate(http.ResponseWriter, *http.Request) error
BeforeAPIDelete(http.ResponseWriter, *http.Request) error
AfterAPIDelete(http.ResponseWriter, *http.Request) error
BeforeAdminCreate(http.ResponseWriter, *http.Request) error
AfterAdminCreate(http.ResponseWriter, *http.Request) error
BeforeAdminUpdate(http.ResponseWriter, *http.Request) error
AfterAdminUpdate(http.ResponseWriter, *http.Request) error
BeforeAdminDelete(http.ResponseWriter, *http.Request) error
AfterAdminDelete(http.ResponseWriter, *http.Request) error
BeforeSave(http.ResponseWriter, *http.Request) error
AfterSave(http.ResponseWriter, *http.Request) error
BeforeDelete(http.ResponseWriter, *http.Request) error
AfterDelete(http.ResponseWriter, *http.Request) error
BeforeApprove(http.ResponseWriter, *http.Request) error
AfterApprove(http.ResponseWriter, *http.Request) error
BeforeReject(http.ResponseWriter, *http.Request) error
AfterReject(http.ResponseWriter, *http.Request) error
// Enable/Disable used for addons
BeforeEnable(http.ResponseWriter, *http.Request) error
AfterEnable(http.ResponseWriter, *http.Request) error
BeforeDisable(http.ResponseWriter, *http.Request) error
AfterDisable(http.ResponseWriter, *http.Request) error
}
const (
typeNotRegistered = `Error:
There is no type registered for %[1]s
Add this to the file which defines %[1]s{} in the 'content' package:
func init() {
item.Types["%[1]s"] = func() interface{} { return new(%[1]s) }
}
`
)
var (
// ErrTypeNotRegistered means content type isn't registered (not found in Types map)
ErrTypeNotRegistered = errors.New(typeNotRegistered)
// ErrAllowHiddenItem should be used as an error to tell a caller of Hideable#Hide
// that this type is hidden, but should be shown in a particular case, i.e.
// if requested by a valid admin or user
ErrAllowHiddenItem = errors.New(`allow hidden item`)
)