Repository navigation
Expand file tree
/
Copy pathjson.go
More file actions
109 lines (86 loc) · 2.4 KB
/
Copy pathjson.go
File metadata and controls
109 lines (86 loc) · 2.4 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
100
101
102
103
104
105
106
107
108
109
package ant
import (
"bytes"
"database/sql/driver"
"encoding/gob"
"encoding/json"
"fmt"
"github.com/skudra-go/ant/internal/jsontext"
)
// JSON stores a Go value in a JSON column. The zero value of V is stored as its
// JSON form, and a NULL column reads as the zero value. A number read into an
// interface, as in a map[string]any, is a json.Number, so an integer past 2^53
// keeps its digits when the value is written back.
type JSON[V any] struct {
V V
}
// ColumnType implements schema.Typed.
func (j JSON[V]) ColumnType(dialect string) string {
if dialect == "postgres" {
return "jsonb"
}
return "json"
}
// MarshalJSON writes the value inside, so that an API response shows the document
// and not this wrapper.
func (j JSON[V]) MarshalJSON() ([]byte, error) { return json.Marshal(j.V) }
// UnmarshalJSON reads the value inside, as Scan does.
func (j *JSON[V]) UnmarshalJSON(data []byte) error { return jsontext.Unmarshal(data, &j.V) }
// Value implements driver.Valuer. Text that is not valid UTF-8 is refused, as a
// text column refuses it, since JSON would store U+FFFD in its place.
func (j JSON[V]) Value() (driver.Value, error) {
if err := jsontext.Valid(j.V); err != nil {
return nil, err
}
text, err := json.Marshal(j.V)
if err != nil {
return nil, err
}
return string(text), nil
}
// Scan implements sql.Scanner.
func (j *JSON[V]) Scan(src any) error {
var zero V
j.V = zero
switch src := src.(type) {
case nil:
return nil
case string:
return jsontext.Unmarshal([]byte(src), &j.V)
case []byte:
return jsontext.Unmarshal(src, &j.V)
}
return fmt.Errorf("ant: cannot scan %T into JSON", src)
}
// Gob stores a Go value in a binary column with encoding/gob, for a value that only
// Go reads and JSON does not fit.
type Gob[V any] struct {
V V
}
// ColumnType implements schema.Typed.
func (g Gob[V]) ColumnType(dialect string) string {
if dialect == "postgres" {
return "bytea"
}
return "blob"
}
// Value implements driver.Valuer.
func (g Gob[V]) Value() (driver.Value, error) {
var b bytes.Buffer
if err := gob.NewEncoder(&b).Encode(g.V); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// Scan implements sql.Scanner.
func (g *Gob[V]) Scan(src any) error {
var zero V
g.V = zero
switch src := src.(type) {
case nil:
return nil
case []byte:
return gob.NewDecoder(bytes.NewReader(src)).Decode(&g.V)
}
return fmt.Errorf("ant: cannot scan %T into Gob", src)
}