-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
49 lines (42 loc) · 1.52 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
package jsonhandlers
import (
"net/http"
"github.com/abusomani/jsonhandlers/handler"
)
// Option is a self-referential function to make palette modular and extensible.
// Read more about self-referential function here:
// https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
type Option func(*JSONHandler)
// WithHTTPRequestHandler is a self-referential function that takes in a http.ResponseWriter
// and a pointer to http.Request
// It sets the handler as HTTPRequestHandler on the JSONHandler receiver.
func WithHTTPRequestHandler(rw http.ResponseWriter, req *http.Request) Option {
return func(jh *JSONHandler) {
h := handler.NewHTTPRequestHandler(rw, req)
jh.handler = h
}
}
// WithHTTPResponseHandler is a self-referential function that takes in a pointer to http.Response
// It sets the handler as HTTPResponseHandler on the JSONHandler receiver.
func WithHTTPResponseHandler(res *http.Response) Option {
return func(jh *JSONHandler) {
h := handler.NewHTTPResponseHandler(res)
jh.handler = h
}
}
// WithFileHandler is a self-referential function that takes in a fileName
// It sets the handler as FileHandler on the JSONHandler receiver.
func WithFileHandler(fileName string) Option {
return func(jh *JSONHandler) {
h := handler.NewFileHandler(fileName)
jh.handler = h
}
}
// WithDefaults is a self-referential function.
// It sets the NoopHandler on the JSONHandler receiver.
func WithDefaults() Option {
return func(jh *JSONHandler) {
h := handler.NewNoopHandler()
jh.handler = h
}
}