-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuri.go
73 lines (60 loc) · 1.33 KB
/
uri.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
package graphql
import (
"database/sql/driver"
"fmt"
"io"
)
// URI is a string representation of a URI.
// TODO: Turn into an actual URI.
type URI struct {
raw string
}
// NewURI creates a URI from a string.
func NewURI(raw string) *URI {
u := &URI{}
u.raw = raw
return u
}
// String returns the value
func (u *URI) String() string {
return u.raw
}
// Scan implements the driver.Scan interface
func (u *URI) Scan(v interface{}) error {
return u.UnmarshalGQL(v)
}
// UnmarshalGQL implements the graphql.Marshaler interface
func (u *URI) UnmarshalGQL(v interface{}) error {
if v == nil {
u.raw = ""
return nil
}
in, ok := v.(URI)
if ok {
u.raw = in.String()
return nil
}
str, ok := v.(string)
if !ok {
return fmt.Errorf("URI must be a string")
}
u.raw = str
return nil
}
// MarshalGQL implements the graphql.Marshaler interface
func (u URI) MarshalGQL(w io.Writer) {
fmt.Fprintf(w, `"%s"`, u.String())
}
// Value implements the driver.Value interface
func (u URI) Value() (driver.Value, error) {
return u.raw, nil
}
// MarshalJSON implements the encoding/json interface.
func (u URI) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, u.String())), nil
}
// UnmarshalJSON implements the encoding/json interface.
func (u *URI) UnmarshalJSON(value []byte) error {
u.raw = string(value)
return nil
}