-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple_url.go
186 lines (155 loc) · 3.76 KB
/
simple_url.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
package jsonapi
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
)
// A SimpleURL represents a URL not validated nor supplemented from a schema.
//
// It parses a URL in text format and stores the values as is.
type SimpleURL struct {
// Source string
// URL
Fragments []string // [users, abc123, articles]
Route string // /users/:id/articles
// Params
Fields map[string][]string
FilterLabel string
Filter *Filter
SortingRules []string
Page map[string]any
Include []string
}
// NewSimpleURL takes and parses a *url.URL and returns a SimpleURL.
func NewSimpleURL(u *url.URL) (SimpleURL, error) {
sURL := SimpleURL{
Fragments: []string{},
Route: "",
Fields: map[string][]string{},
Filter: nil,
SortingRules: []string{},
Include: []string{},
}
if u == nil {
return sURL, errors.New("jsonapi: pointer to url.URL is nil")
}
sURL.Fragments = parseFragments(u.Path)
sURL.Route = deduceRoute(sURL.Fragments)
values := u.Query()
for name := range values {
switch {
case strings.HasPrefix(name, "fields[") && strings.HasSuffix(name, "]") && len(name) > 8:
resType := name[7 : len(name)-1]
if len(values.Get(name)) > 0 {
sURL.Fields[resType] = parseCommaList(values.Get(name))
}
case strings.HasPrefix(name, "page[") && strings.HasSuffix(name, "]") && len(name) > 6:
arg := name[5 : len(name)-1]
if len(values.Get(name)) > 0 {
if sURL.Page == nil {
sURL.Page = map[string]any{}
}
val := values.Get(name)
if num, err := strconv.Atoi(val); err != nil {
sURL.Page[arg] = val
} else {
sURL.Page[arg] = num
}
}
case name == "filter":
var err error
if values.Get(name)[0] != '{' {
// It should be a label
err = json.Unmarshal([]byte("\""+values.Get(name)+"\""), &sURL.FilterLabel)
} else {
// It should be a JSON object
sURL.Filter = &Filter{}
err = json.Unmarshal([]byte(values.Get(name)), sURL.Filter)
}
if err != nil {
sURL.FilterLabel = ""
sURL.Filter = nil
return sURL, NewErrMalformedFilterParameter(values.Get(name))
}
case name == "sort":
for _, rules := range values[name] {
sURL.SortingRules = append(sURL.SortingRules, parseCommaList(rules)...)
}
case name == "include":
for _, include := range values[name] {
sURL.Include = append(sURL.Include, parseCommaList(include)...)
}
default:
return sURL, NewErrUnknownParameter(name)
}
}
return sURL, nil
}
// Path returns the path only of the SimpleURL. It does not include any query
// parameters.
func (s *SimpleURL) Path() string {
return strings.Join(s.Fragments, "/")
}
func parseCommaList(path string) []string {
items := strings.Split(path, ",")
items2 := make([]string, 0, len(items))
for i := range items {
if items[i] != "" {
items2 = append(items2, items[i])
}
}
return items2
}
func parseFragments(path string) []string {
fragments := strings.Split(path, "/")
fragments2 := make([]string, 0, len(fragments))
for i := range fragments {
if fragments[i] != "" {
fragments2 = append(fragments2, fragments[i])
}
}
return fragments2
}
func deduceRoute(path []string) string {
const (
id = "/:id"
meta = "meta"
rel = "relationships"
)
route := ""
if len(path) >= 1 {
route = "/" + path[0]
}
if len(path) >= 2 {
if path[1] == meta {
route += "/" + meta
} else {
route += id
}
}
if len(path) >= 3 {
switch {
case path[2] == rel:
route += "/" + rel
case path[2] == meta:
route += "/" + meta
default:
route += "/" + path[2]
}
}
if len(path) >= 4 {
if path[3] == meta {
route += "/" + meta
} else if path[2] == rel {
route += "/" + path[3]
}
}
if len(path) >= 5 {
if path[4] == meta {
route += "/" + meta
}
}
return route
}