-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.go
More file actions
68 lines (59 loc) · 1.61 KB
/
Copy pathurl.go
File metadata and controls
68 lines (59 loc) · 1.61 KB
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
// SPDX-License-Identifier: EUPL-1.2
// URL helpers for the Core framework.
// Wraps net/url so consumers can use core primitives for URL parsing
// and escaping.
package core
import "net/url"
// URL is the canonical parsed URL type.
//
// r := core.URLParse("https://example.com/path")
// if r.OK { u := r.Value.(*core.URL); _ = u }
type URL = url.URL
// URLValues is the canonical URL form/query values map.
//
// values := core.URLValues{"key": {"value"}}
type URLValues = url.Values
// URLParse parses a raw URL string.
//
// r := core.URLParse("https://example.com/path")
// if r.OK { u := r.Value.(*core.URL) }
func URLParse(rawURL string) Result {
u, err := url.Parse(rawURL)
if err != nil {
return Result{err, false}
}
return Result{u, true}
}
// URLEncode escapes a string for use in URL query components.
//
// s := core.URLEncode("hello world")
func URLEncode(s string) string {
return url.QueryEscape(s)
}
// URLDecode unescapes a URL query component string.
//
// r := core.URLDecode("hello+world")
// if r.OK { s := r.Value.(string) }
func URLDecode(s string) Result {
decoded, err := url.QueryUnescape(s)
if err != nil {
return Result{err, false}
}
return Result{decoded, true}
}
// URLPathEscape escapes a string for use in URL path components.
//
// s := core.URLPathEscape("a/b")
func URLPathEscape(s string) string {
return url.PathEscape(s)
}
// URLNormalize parses and re-encodes a URL into net/url's canonical string form.
//
// s := core.URLNormalize("https://example.com/a b")
func URLNormalize(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return ""
}
return u.String()
}