-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaxon.go
73 lines (68 loc) · 1.8 KB
/
claxon.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
package claxon
type Claxon struct {
Schema string `json:"$schema,omitempty"`
Self string `json:"$self,omitempty"`
Links []Link `json:"$links,omitempty"`
Actions []Action `json:"$actions,omitempty"`
}
type Link struct {
Href string `json:"href"`
Rel string `json:"rel"`
Title string `json:"title"`
Type string `json:"type,omitempty"`
}
type Action struct {
Href string `json:"href"`
Id string `json:"id"`
Method string `json:"method,omitempty"`
RequestSchema string `json:"reqs,omitempty"`
ResponseSchema string `json:"ress,omitempty"`
}
// The AddLink method takes the two required arguments (rel and href).
// If a third argument is present, it is interpreted as the title
// and the fourth argument as the content type
func (c *Claxon) AddLink(rel string, href string, more ...string) *Claxon {
link := Link{
Rel: rel,
Href: href,
}
if len(more) >= 1 {
link.Title = more[0]
}
if len(more) >= 2 {
link.Type = more[1]
}
links := c.Links
if links == nil {
links = []Link{}
}
links = append(links, link)
c.Links = links
return c
}
// The AddAction method takes the two required arguments (id and href).
// If a third argument is present, it is interpreted as the method.
// If a fourth argument is present, it is interpreted as the **response** schema
// If a fifth argument is present, it is interpreted as the **request** schema
func (c *Claxon) AddAction(id string, href string, more ...string) *Claxon {
action := Action{
Id: id,
Href: href,
}
if len(more) >= 1 {
action.Method = more[0]
}
if len(more) >= 2 {
action.ResponseSchema = more[1]
}
if len(more) >= 3 {
action.RequestSchema = more[2]
}
actions := c.Actions
if actions == nil {
actions = []Action{}
}
actions = append(actions, action)
c.Actions = actions
return c
}