-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
62 lines (59 loc) · 1.17 KB
/
parse.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
package claxon
import (
"strings"
"github.com/mtiller/rfc8288"
)
func ParseLinkHeader(s ...string) Claxon {
ret := Claxon{
Links: []Link{},
Actions: []Action{},
}
all := strings.Join(s, "\r\n")
links, err := rfc8288.ParseLinkHeaders(all)
if err != nil {
return ret
}
for _, link := range links {
if link.Rel == "describedby" {
ret.Schema = link.HREF.String()
continue
}
if link.Rel == "self" {
ret.Self = link.HREF.String()
continue
}
key, exists := link.StringExtension("claxon")
if exists && key == "action" {
add := Action{
Href: link.HREF.String(),
}
add.Id = link.Title
method, has := link.StringExtension("method")
if has {
add.Method = method
}
reqs, has := link.StringExtension("reqs")
if has {
add.RequestSchema = reqs
}
ress, has := link.StringExtension("ress")
if has {
add.ResponseSchema = ress
}
ret.Actions = append(ret.Actions, add)
} else {
add := Link{
Href: link.HREF.String(),
Rel: link.Rel,
}
if link.Title != "" {
add.Title = link.Title
}
if link.Type != "" {
add.Type = link.Type
}
ret.Links = append(ret.Links, add)
}
}
return ret
}