-
Notifications
You must be signed in to change notification settings - Fork 55
/
shared.go
115 lines (93 loc) · 2.26 KB
/
shared.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
package presenter
import (
"net/url"
"path"
)
type Lifecycle struct {
Type string `json:"type"`
Data LifecycleData `json:"data"`
}
type LifecycleData struct {
Buildpacks []string `json:"buildpacks"`
Stack string `json:"stack"`
}
type Relationships map[string]Relationship
type Relationship struct {
Data *RelationshipData `json:"data"`
}
type RelationshipData struct {
GUID string `json:"guid"`
}
type Metadata struct {
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
}
type Link struct {
HRef string `json:"href,omitempty"`
Method string `json:"method,omitempty"`
}
type ListResponse struct {
PaginationData PaginationData `json:"pagination"`
Resources []interface{} `json:"resources"`
Included *IncludedData `json:"included,omitempty"`
}
type PaginationData struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
First PageRef `json:"first"`
Last PageRef `json:"last"`
Next *int `json:"next"`
Previous *int `json:"previous"`
}
type IncludedData struct {
Apps []interface{} `json:"apps"`
}
type PageRef struct {
HREF string `json:"href"`
}
func ForList(resources []interface{}, baseURL, requestURL url.URL) ListResponse {
return ListResponse{
PaginationData: PaginationData{
TotalResults: len(resources),
TotalPages: 1,
First: PageRef{
HREF: buildURL(baseURL).appendPath(requestURL.Path).setQuery(requestURL.RawQuery).build(),
},
Last: PageRef{
HREF: buildURL(baseURL).appendPath(requestURL.Path).setQuery(requestURL.RawQuery).build(),
},
},
Resources: resources,
}
}
type buildURL url.URL
func (u buildURL) appendPath(subpath ...string) buildURL {
rest := path.Join(subpath...)
if u.Path == "" {
u.Path = rest
} else {
u.Path = path.Join(u.Path, rest)
}
return u
}
func (u buildURL) setQuery(rawQuery string) buildURL {
u.RawQuery = rawQuery
return u
}
func (u buildURL) build() string {
native := url.URL(u)
nativeP := &native
return nativeP.String()
}
func emptyMapIfNil(m map[string]string) map[string]string {
if m == nil {
return map[string]string{}
}
return m
}
func emptySliceIfNil(m []string) []string {
if m == nil {
return []string{}
}
return m
}