-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
108 lines (98 loc) · 3.58 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
package gohateoas
import "net/http"
// LinkInfo represents a link to a resource.
type LinkInfo struct {
Method string `json:"method"`
Href string `json:"href"`
Comment string `json:"comment"`
// Templated marks the link as a template, so the client can identify a link
// as a template and they don't try to call the endpoint as it is presented.
//
// This field is defined in the draft RFC in section 5.2:
// https://datatracker.ietf.org/doc/html/draft-kelly-json-hal#section-5.2
Templated bool `json:"templated,omitempty"`
}
// LinkOption is used to register links in a LinkRegistry. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
type LinkOption func(map[string]LinkInfo)
// Custom allows you to define a custom action and info. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Custom(action string, info LinkInfo) LinkOption {
return func(registry map[string]LinkInfo) {
registry[action] = info
}
}
// Self Adds the self url of an object to the type, probably an url with an id. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Self(href string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["self"] = LinkInfo{
Method: http.MethodGet,
Href: href,
Comment: comment,
}
}
}
// Index Adds a general Index route to the type. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Index(href string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["index"] = LinkInfo{
Method: http.MethodGet,
Href: href,
Comment: comment,
}
}
}
// Post Adds a general POST route to the LinkRegistry. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Post(href string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["post"] = LinkInfo{
Method: http.MethodPost,
Href: href,
Comment: comment,
}
}
}
// Put Adds a general Put route to the LinkRegistry. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Put(href string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["put"] = LinkInfo{
Method: http.MethodPut,
Href: href,
Comment: comment,
}
}
}
// Patch Adds a general Patch route to the LinkRegistry. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Patch(url string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["patch"] = LinkInfo{
Method: http.MethodPatch,
Href: url,
Comment: comment,
}
}
}
// Delete Adds a general Delete route to the LinkRegistry. Urls may contain
// replaceable tokens like {id} or {name}. These tokens will be replaced by
// the values of the corresponding json fields in the struct.
func Delete(url string, comment string) LinkOption {
return func(registry map[string]LinkInfo) {
registry["delete"] = LinkInfo{
Method: http.MethodDelete,
Href: url,
Comment: comment,
}
}
}