generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
url.go
43 lines (36 loc) 路 830 Bytes
/
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
package projectconfig
import (
"fmt"
"net/url"
)
// A URL that supports marshalling and unmarshalling which is, very
// frustratingly, not possible with the stdlib url.URL.
type URL url.URL
func ParseURL(rawurl string) (*URL, error) {
parsed, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
return (*URL)(parsed), nil
}
func MustParseURL(rawurl string) *URL {
parsed, err := ParseURL(rawurl)
if err != nil {
panic(err)
}
return parsed
}
func (u *URL) UnmarshalText(text []byte) error {
parsed, err := url.Parse(string(text))
if err != nil {
return err
}
*u = URL(*parsed)
return nil
}
func (u URL) MarshalText() ([]byte, error) {
return []byte((*url.URL)(&u).String()), nil
}
func (u URL) GoString() string {
return fmt.Sprintf("projectconfig.MustParseURL(%q)", (*url.URL)(&u).String())
}