-
Notifications
You must be signed in to change notification settings - Fork 559
/
url.go
52 lines (43 loc) · 922 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
44
45
46
47
48
49
50
51
52
package config
import (
"encoding/json"
"errors"
"net/url"
)
// A url.URL wrapper that can marshal and unmarshal into simple URL strings.
type URL struct {
url.URL
}
func (d URL) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *URL) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case string:
u, err := url.Parse(value)
if err != nil {
return err
}
d.URL = *u
return nil
default:
return errors.New("invalid url")
}
}
// Set implements PFlag's Value interface's set method to set the value of duration from string.
func (d *URL) Set(val string) error {
u, err := url.Parse(val)
if err != nil {
return err
}
d.URL = *u
return nil
}
// Type implements PFlag's Value interface's Type method to return the name of the type.
func (URL) Type() string {
return "URL"
}