This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
forked from badgateway/ketting
/
hal.go
288 lines (241 loc) · 5.67 KB
/
hal.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package representor
import (
// "bytes"
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/identbase/getting/pkg/link"
)
/*
HALRepresentor is a basic template implementation of a Representor. It is
basically a 'body' of a request or response.
This should not be used, refer to hal.go for the HalRepresentor as a good
default option to use. */
type HALRepresentor struct {
URI url.URL
ContentType string
Body interface{}
Links link.LinkSet
}
type HALLink struct {
HRef string `json:"href"`
Name string `json:"name,omitempty"`
Title string `json:"title"`
// Templated string
Type string `json:"type,omitempty"`
}
type HALBody struct {
// TODO: This does not handle multiple "item" links. Need to handle
// either a single HALLink or an array of HALLinks
Links map[string][]HALLink `json:"_links,omitempty"`
// This should be only JSON acceptable types: string, int, float, bool
Properties map[string]interface{} `json:"-"`
Embedded map[string]HALBody `json:"_embedded,omitempty"`
}
func mapInterfaceToHALLink(i map[string]interface{}) HALLink {
// TODO: This seems like there is a
// better way to do this
var link HALLink
_, nok := i["name"]
_, tok := i["type"]
if !nok && !tok {
link = HALLink{
HRef: i["href"].(string),
Title: i["title"].(string),
}
} else {
if nok && tok {
link = HALLink{
HRef: i["href"].(string),
Name: i["name"].(string),
Title: i["title"].(string),
Type: i["type"].(string),
}
} else {
if nok {
link = HALLink{
HRef: i["href"].(string),
Name: i["name"].(string),
Title: i["title"].(string),
}
}
if tok {
link = HALLink{
HRef: i["href"].(string),
Title: i["title"].(string),
Type: i["type"].(string),
}
}
}
}
return link
}
/*
UnmarshalJSON will properly convert JSON back into a HALBody object. */
func (b *HALBody) UnmarshalJSON(d []byte) error {
var r map[string]interface{}
if err := json.Unmarshal(d, &r); err != nil {
return err
}
for bk, bv := range r {
switch bk {
case "_links":
links := map[string][]HALLink{}
lbuf := bv.(map[string]interface{})
// TODO: Unmarshal the other potential HALLink data
// _links: {
for lk, lv := range lbuf {
if lvbuf, ok := lv.(map[string]interface{}); ok {
// self: {
links[lk] = []HALLink{
mapInterfaceToHALLink(lvbuf),
}
} else if lvbuf, ok := lv.([]interface{}); ok {
// item: [
for i := 0; i < len(lvbuf); i++ {
// TODO: This seems like there is a
// better way to do this
lvbufitem := lvbuf[i].(map[string]interface{})
links[lk] = append(links[lk], mapInterfaceToHALLink(lvbufitem))
}
}
}
b.Links = links
case "_embedded":
// TODO: Unmarshal HalBody recursively
// b.Embedded = v.([]*HALBody)
fmt.Println("TODO: Unmarshal _links", bk, bv)
default:
if b.Properties == nil {
b.Properties = map[string]interface{}{}
}
b.Properties[bk] = bv
}
}
return nil
}
/*
MarshalJSON will properly convert a HALBody into JSON. */
func (b HALBody) MarshalJSON() ([]byte, error) {
r := map[string]interface{}{}
for k, v := range b.Properties {
r[k] = v
}
if len(b.Embedded) > 0 {
r["_embedded"] = make(map[string]HALBody)
for k, v := range b.Embedded {
// TODO: Marshal the _embedded stuff recursively
fmt.Println("TODO: Marshal _embedded", k, v)
}
}
if len(b.Links) > 0 {
r["_links"] = map[string]interface{}{}
for k, v := range b.Links {
if len(v) == 1 {
l := map[string]HALLink{}
l[k] = v[0]
r["_links"] = l
} else {
l := map[string][]HALLink{}
l[k] = v
r["_links"] = l
}
}
}
buf, err := json.Marshal(&r)
if err != nil {
return nil, err
}
return buf, nil
}
/*
New creates a new Representor object. */
func NewHALRepresentor(u url.URL, ct string, b []byte) (*HALRepresentor, error) {
r := HALRepresentor{
URI: u,
ContentType: ct,
}
if b != nil {
p, err := r.parse(b)
if err != nil {
return nil, err
}
r.setBody(p)
}
return &r, nil
}
/*
parse converts a JSON byte string into a HALBody. */
func (r *HALRepresentor) parse(b []byte) (interface{}, error) {
var h HALBody
if err := json.Unmarshal(b, &h); err != nil {
return nil, err
}
return h, nil
}
// TODO: This function should be called in parse (and there should also be a
// parseEmbedded called there too)
/*
parseLinks converts HALLink into link.Link. */
func (r *HALRepresentor) parseLinks(b interface{}) []*link.Link {
h := b.(HALBody)
l := []*link.Link{}
// TODO: This doesnt account for links in the Header
for k, v := range h.Links {
for i := 0; i < len(v); i++ {
l = append(l, &link.Link{
Context: r.URI.String(),
HRef: v[i].HRef,
Rel: k,
Name: v[i].Name,
Title: v[i].Title,
Type: v[i].Type,
})
}
}
return l
}
/*
GetBody */
func (r *HALRepresentor) GetBody() interface{} {
return r.Body
}
/*
setBody */
func (r *HALRepresentor) setBody(b interface{}) {
r.Body = b.(HALBody)
// TODO: Initialize this somewhere else
r.Links = link.LinkSet{}
// TODO: The links dont need to be reparsed here really
l := r.parseLinks(r.Body)
for _, v := range l {
if r.Links.Has(v.Rel) {
r.Links.Add(v.Rel, *v)
} else {
r.Links.Set(v.Rel, append([]link.Link{}, *v))
}
}
}
/*
GetLink */
func (r *HALRepresentor) GetLink(rt string) (*link.Link, error) {
l := r.Links.Get(rt)
if len(l) == 0 {
return nil, errors.New("link not found")
}
return &l[0], nil
}
/*
GetLinks */
func (r *HALRepresentor) GetLinks(rt string) []link.Link {
if rt == "" {
return r.Links.Values()
}
return r.Links.Get(rt)
}
/*
HasLink */
func (r *HALRepresentor) HasLink(rt string) bool {
return r.Links.Has(rt)
}