-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded.go
125 lines (106 loc) · 2.93 KB
/
embedded.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
package siren
type EmbeddedEntity[P any] struct {
Rel []string `json:"rel"`
// Only present if this is an "embedded link" (href required in that case)
Href string `json:"href,omitempty"`
Type string `json:"type,omitempty"`
// Otherwise, just like a normal SirenEntity
Title string `json:"title,omitempty"`
Class []string `json:"class,omitempty"`
Properties P `json:"properties,omitempty"`
Entities []EmbeddedEntity[any] `json:"entities,omitempty"`
Actions []SirenAction `json:"actions,omitempty"`
Links []SirenLink `json:"links,omitempty"`
}
func (e *EmbeddedEntity[P]) GetHref() string {
return e.Href
}
func (e *EmbeddedEntity[P]) SetHref(href string) *EmbeddedEntity[P] {
e.Href = href
return e
}
func (e *EmbeddedEntity[P]) GetRel() []string {
return e.Rel
}
func (e *EmbeddedEntity[P]) AddRel(rel string) *EmbeddedEntity[P] {
if e.Rel == nil {
e.Rel = []string{}
}
e.Rel = append(e.Rel, rel)
return e
}
func (e *EmbeddedEntity[P]) SetRel(rels []string) *EmbeddedEntity[P] {
e.Rel = rels
return e
}
func (e *EmbeddedEntity[P]) SetType(t string) *EmbeddedEntity[P] {
e.Type = t
return e
}
func (e *EmbeddedEntity[P]) SetTitle(title string) *EmbeddedEntity[P] {
e.Title = title
return e
}
func (e *EmbeddedEntity[P]) SetClasses(classes []string) *EmbeddedEntity[P] {
e.Class = classes
return e
}
func (e *EmbeddedEntity[P]) AddClass(class string) *EmbeddedEntity[P] {
if e.Class == nil {
e.Class = []string{}
}
e.Class = append(e.Class, class)
return e
}
func (e *EmbeddedEntity[P]) SetProperties(props P) *EmbeddedEntity[P] {
e.Properties = props
return e
}
func (e *EmbeddedEntity[P]) SetLinks(links []SirenLink) *EmbeddedEntity[P] {
e.Links = links
return e
}
func (e *EmbeddedEntity[P]) AddLink(rel []string, href string, l SirenLink) *EmbeddedEntity[P] {
l.Rel = rel
l.Href = href
e.Links = append(e.Links, l)
return e
}
func (e *EmbeddedEntity[P]) SetActions(actions []SirenAction) *EmbeddedEntity[P] {
e.Actions = actions
return e
}
func (e *EmbeddedEntity[P]) AddAction(name string, title string, method string,
href string, ctype string, fields ...SirenField) *EmbeddedEntity[P] {
e.Actions = append(e.Actions, SirenAction{
Name: name,
Title: title,
Method: method,
Href: href,
Type: ctype,
Fields: fields,
})
return e
}
func (e *EmbeddedEntity[P]) AddEntity(siren *Siren[any], rel ...string) *EmbeddedEntity[P] {
embed := EmbeddedEntity[any]{
Rel: rel,
Title: siren.Title,
Class: siren.Class,
Properties: siren.Properties,
Entities: siren.Entities,
Actions: siren.Actions,
Links: siren.Links,
}
e.Entities = append(e.Entities, embed)
return e
}
func NewSirenEmbed[P any](rel []string) *EmbeddedEntity[P] {
return &EmbeddedEntity[P]{
Rel: rel,
Class: []string{},
Entities: []EmbeddedEntity[any]{},
Actions: []SirenAction{},
Links: []SirenLink{},
}
}