-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
113 lines (99 loc) · 2.77 KB
/
options.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
package crud
import (
"html/template"
"net/http"
"path"
)
// Option represents an admin option.
type Option func(*Admin) error
// WithDatabaseURI returns an admin option that sets the database uri.
func WithDatabaseURI(uri string) Option {
return func(a *Admin) error {
a.DatabaseURI = uri
a.databaseEngine = path.Ext(uri)
return nil
}
}
// WithBaseURL returns an admin option that sets the admin base url.
func WithBaseURL(baseURL string) Option {
return func(a *Admin) error {
a.BaseURL = baseURL
return nil
}
}
// WithDefaultFormatters returns an admin option that sets the default formatters.
func WithDefaultFormatters(formatters map[string]Formatter) Option {
return func(a *Admin) error {
a.DefaultFormatters = formatters
return nil
}
}
// WithDefaultFormatter returns an admin option that sets a default formatter.
func WithDefaultFormatter(column string, formatter Formatter) Option {
return func(a *Admin) error {
a.DefaultFormatters[column] = formatter
return nil
}
}
// WithEntity returns an admin option that adds an entity.
func WithEntity(entity Entity) Option {
return func(a *Admin) error {
a.Entities[entity.TableName] = entity
return nil
}
}
// WithEntities returns an admin option that adds entities.
func WithEntities(entities []Entity) Option {
return func(a *Admin) error {
for _, entity := range entities {
a.Entities[entity.TableName] = entity
}
return nil
}
}
// WithTemplateFuncs returns an admin option that adds template funcs.
func WithTemplateFuncs(funcs template.FuncMap) Option {
return func(a *Admin) error {
a.TemplateFuncs = funcs
return nil
}
}
// WithTemplateFunc returns an admin option that adds a template func.
func WithTemplateFunc(name string, fn interface{}) Option {
return func(a *Admin) error {
a.TemplateFuncs[name] = fn
return nil
}
}
// WithTemplate returns an admin option that adds a template.
func WithTemplate(name, text string) Option {
return func(a *Admin) error {
tmpl, err := template.New(name).Parse(text)
if err != nil {
return err
}
a.Templates[name] = tmpl
return nil
}
}
// WithUserIdentifier returns an admin option that sets the user identifier.
func WithUserIdentifier(fn func(r *http.Request) string) Option {
return func(a *Admin) error {
a.UserIdentifier = fn
return nil
}
}
// WithPermissionChecker returns an admin option that sets the permission checker.
func WithPermissionChecker(fn func(r *http.Request, userID, entityName, action string) bool) Option {
return func(a *Admin) error {
a.PermissionChecker = fn
return nil
}
}
// WithSearchHandler returns an admin option that sets the search handler.
func WithSearchHandler(fn func(r *http.Request, q string) ([]SearchResult, error)) Option {
return func(a *Admin) error {
a.SearchHandler = fn
return nil
}
}