/
JSON.go
72 lines (63 loc) · 1.29 KB
/
JSON.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
package main
// This file contains structs for JSON interactions.
import (
"github.com/nvellon/hal"
)
// Password is used for authentication
type Password struct {
Password string
}
type Auth struct {
JWT string
Valid bool
}
func (auth Auth) GetMap() hal.Entry {
return hal.Entry{
"jwt": auth.JWT,
"valid": auth.Valid,
}
}
type Response struct {
element hal.Mapper
Length int
Content string
}
func (resp Response) GetMap() hal.Entry {
if resp.element != nil {
return hal.Entry{
"Length": resp.Length,
"Content": resp.Content,
"element": resp.element,
}
} else {
return hal.Entry{
"Length": resp.Length,
"Content": resp.Content,
}
}
}
func NodeToMap(nodes *[]mNode) *[]hal.Mapper {
mapper := make([]hal.Mapper, len(*nodes))
for i, elem := range *nodes {
mapper[i] = elem
}
return &mapper
}
func UserToMap(users *[]mUsers) *[]hal.Mapper {
mapper := make([]hal.Mapper, len(*users))
for i, elem := range *users {
mapper[i] = elem
}
return &mapper
}
func MapArray(objects []hal.Mapper, selfUri string, content string) *hal.Resource {
resp := Response{
Length: len(objects),
Content: content,
}
respResource := hal.NewResource(resp, selfUri)
for _, object := range objects {
respResource.Embed("", hal.NewResource(object, ""))
}
return respResource
}