-
Notifications
You must be signed in to change notification settings - Fork 9
/
hal.go
78 lines (63 loc) · 1.85 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
package dwolla
import (
"encoding/json"
"fmt"
)
// HALError is a hal error
type HALError struct {
Code string `json:"code"`
Message string `json:"message"`
Path string `json:"path"`
}
// HALError implements the error interface
func (e HALError) Error() string {
if e.Path != "" {
return fmt.Sprintf("[%s] %s (%s)", e.Code, e.Message, e.Path)
}
return fmt.Sprintf("[%s] %s", e.Code, e.Message)
}
// HALErrors is an array of embedded hal errors
type HALErrors map[string][]HALError
// ValidationError is a dwolla validation error
type ValidationError struct {
Code string `json:"code"`
Message string `json:"message"`
Embedded HALErrors `json:"_embedded"`
}
// Error implements the error interface
func (v ValidationError) Error() string {
if _, ok := v.Embedded["errors"]; ok {
return fmt.Sprintf("[%s] %s (%v)", v.Code, v.Message, v.Embedded["errors"])
}
return fmt.Sprintf("[%s] %s", v.Code, v.Message)
}
// Link is a hal resource link
type Link struct {
Href string `json:"href"`
ResourceType string `json:"resource-type,omitempty"`
Type string `json:"type,omitempty"`
}
// Links is a group of resource links
type Links map[string]Link
// Resource is a hal resource
type Resource struct {
Links Links `json:"_links,omitempty"`
client *Client
}
// NewResource is constructor for Resource
func NewResource(links Links, client *Client) *Resource {
return &Resource{Links: links, client: client}
}
// Embedded is a hal embedded resource
type Embedded map[string][]Resource
// Collection is a collection of hal resources
type Collection struct {
Links Links `json:"_links"`
Embedded Embedded `json:"_embedded"`
Total int `json:"total"`
client *Client
}
// Unmarshal unmarhsals a hal object into a struct
func Unmarshal(data []byte, container interface{}) error {
return json.Unmarshal(data, container)
}