-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.go
More file actions
99 lines (82 loc) · 2.36 KB
/
Copy pathurl.go
File metadata and controls
99 lines (82 loc) · 2.36 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
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
package typx
import (
"database/sql"
"database/sql/driver"
"encoding"
"fmt"
"net/url"
)
/*
[URL] is a [url.URL] wrapper that serialises as a plain URL string in all common transports.
Because it implements [encoding.TextMarshaler] / [encoding.TextUnmarshaler], JSON (and any
other text-based format) represents the value as a quoted URL string with no extra nesting.
It also implements [driver.Valuer] and [sql.Scanner] so it can be stored directly in any
SQL column that holds text (VARCHAR, TEXT, etc.), without manual .String() / url.Parse calls
at the call site:
type Row struct {
ID int
Callback typx.URL
}
Use [Nil][[URL]] for a nullable SQL column.
*/
type URL struct{ url.URL }
// [URLFrom] parses str and returns a URL or an error.
func URLFrom(str string) (URL, error) {
u, err := url.Parse(str)
if err != nil {
return URL{}, fmt.Errorf("typx.URLFrom: %w", err)
}
return URL{*u}, nil
}
var _ fmt.Stringer = URL{}
// String returns the URL in its canonical string form.
func (u URL) String() string {
return u.URL.String()
}
var _ encoding.TextMarshaler = URL{}
// MarshalText implements [encoding.TextMarshaler].
func (u URL) MarshalText() ([]byte, error) {
return []byte(u.URL.String()), nil
}
var _ encoding.TextUnmarshaler = (*URL)(nil)
// UnmarshalText implements [encoding.TextUnmarshaler].
func (u *URL) UnmarshalText(text []byte) error {
parsed, err := url.Parse(string(text))
if err != nil {
return fmt.Errorf("typx.URL.UnmarshalText: %w", err)
}
u.URL = *parsed
return nil
}
var _ driver.Valuer = URL{}
// Value implements [driver.Valuer].
// The URL is stored as a plain string in the database.
func (u URL) Value() (driver.Value, error) {
return u.URL.String(), nil
}
var _ sql.Scanner = (*URL)(nil)
// Scan implements [sql.Scanner].
// Accepts string or []byte from the driver.
// A nil src (SQL NULL) zeroes the receiver.
// Use Nil[[URL]] for a type that can be nil instead of zero.
func (u *URL) Scan(src any) error {
switch v := src.(type) {
case nil:
u.URL = url.URL{}
case string:
parsed, err := url.Parse(v)
if err != nil {
return fmt.Errorf("typx.URL.Scan: %w", err)
}
u.URL = *parsed
case []byte:
parsed, err := url.Parse(string(v))
if err != nil {
return fmt.Errorf("typx.URL.Scan: %w", err)
}
u.URL = *parsed
default:
return fmt.Errorf("typx.URL.Scan: unsupported type %T", src)
}
return nil
}