-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.go
193 lines (180 loc) · 4.45 KB
/
links.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
package haljson
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
)
// Link represents a link
type Link struct {
Deprecation *string `json:"deprecation,omitempty"`
Href string `json:"href,omitempty"`
HrefLang *string `json:"hreflang,omitempty"`
Profile *string `json:"profile,omitempty"`
Templated bool `json:"templated,omitempty"`
Title *string `json:"title,omitempty"`
Type *string `json:"type,omitempty"`
}
// Links is a container of Link, mapped by relation, and contains Curies
type Links struct {
Self *Link `json:"-"`
Curies *[]Curie `json:"curies,omitempty"`
// When serializing to JSON we need to handle this specially
Relations map[string][]*Link
}
// AddCurie adds a curie to the links
func (l *Links) AddCurie(curie *Curie) error {
if l.Curies == nil {
l.Curies = &[]Curie{}
}
*l.Curies = append(*l.Curies, *curie)
return nil
}
// AddLink adds a link to reltype
func (l *Links) AddLink(reltype string, link *Link) error {
// Check if curied and that if curied, curie exists
curieExists := false
if strings.Index(reltype, ":") > 0 {
parts := strings.Split(reltype, ":")
var curies *[]Curie
curies = l.Curies
if curies == nil {
return ErrNoCurie
}
for _, curie := range *curies {
if parts[0] == curie.Name {
curieExists = true
}
}
if !curieExists {
return ErrNoCurie
}
}
if _, ok := l.Relations[reltype]; !ok {
l.Relations[reltype] = []*Link{}
}
l.Relations[reltype] = append(l.Relations[reltype], link)
return nil
}
// MarshalJSON to marshal Links properly
func (l *Links) MarshalJSON() ([]byte, error) {
// @TODO sort keys
var bufferData []string
if l.Self != nil {
jsonValue, err := json.Marshal(l.Self)
if err != nil {
return nil, err
}
bufferData = append(bufferData, fmt.Sprintf("\"self\": %s", string(jsonValue)))
}
if l.Curies != nil {
jsonValue, err := json.Marshal(l.Curies)
if err != nil {
return nil, err
}
bufferData = append(bufferData, fmt.Sprintf("\"curies\": %s", string(jsonValue)))
}
// Sort keys for data
var sortedKeys = make([]string, len(l.Relations))
i := 0
for k := range l.Relations {
sortedKeys[i] = k
i++
}
sort.Strings(sortedKeys)
for _, key := range sortedKeys {
jsonValue, err := json.Marshal(l.Relations[key])
if err != nil {
return nil, err
}
bufferData = append(bufferData, fmt.Sprintf("\"%s\": %s", key, string(jsonValue)))
}
joined := strings.Join(bufferData, ",")
buffer := bytes.NewBufferString("{")
buffer.WriteString(joined)
buffer.WriteString("}")
return buffer.Bytes(), nil
}
// UnmarshalJSON to unmarshal links
func (l *Links) UnmarshalJSON(b []byte) error {
var temp = make(map[string]interface{})
err := json.Unmarshal(b, &temp)
if err != nil {
return err
}
if _, ok := temp["curies"]; ok {
var mycuries []Curie
for _, curies := range temp["curies"].([]interface{}) {
var curie Curie
for k, v := range curies.(map[string]interface{}) {
switch k {
case NAME:
curie.Name = v.(string)
case HREF:
curie.Href = v.(string)
case TEMPLATED:
curie.Templated = v.(bool)
}
}
mycuries = append(mycuries, curie)
}
l.Curies = &mycuries
delete(temp, "curies")
}
var self Link
if _, ok := temp["self"]; ok {
for k, v := range temp["self"].(map[string]interface{}) {
switch k {
case HREF:
self.Href = v.(string)
}
}
l.Self = &self
delete(temp, "self")
}
l.Relations = make(map[string][]*Link)
for rel, v := range temp {
var links []*Link
for _, properties := range v.([]interface{}) {
var link Link
for key, property := range properties.(map[string]interface{}) {
switch key {
case HREF:
link.Href = property.(string)
case DEPRECATION:
var deprecation string
deprecation = property.(string)
link.Deprecation = &deprecation
case HREFLANG:
var hreflang string
hreflang = property.(string)
link.HrefLang = &hreflang
case PROFILE:
var profile string
profile = property.(string)
link.Profile = &profile
case TITLE:
var title string
title = property.(string)
link.Title = &title
case TYPE:
var typeval string
typeval = property.(string)
link.Type = &typeval
case TEMPLATED:
link.Templated = property.(bool)
}
}
links = append(links, &link)
}
l.Relations[rel] = links
}
return nil
}
// NewLinks creates and initializes Links
func NewLinks() *Links {
l := &Links{}
l.Relations = make(map[string][]*Link)
return l
}