-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_json.go
46 lines (39 loc) · 962 Bytes
/
sql_json.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
package unusual_generics
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
)
// SQLJSON is a wrapper for jsonb-like fields in sql databases.
// It marshals/unmarshalls internal type as json. See examples for usage.
type SQLJSON[T any] struct {
X T // this way because we can't embed type parameter or use it as RHS
}
// SQLJSONOf constructs SQLJSON.
func SQLJSONOf[T any](v T) SQLJSON[T] {
return SQLJSON[T]{X: v}
}
var (
_ sql.Scanner = &SQLJSON[any]{}
_ driver.Valuer = SQLJSON[any]{}
)
// Scan implements sql.Scanner.
func (s *SQLJSON[T]) Scan(x any) error {
switch t := x.(type) {
case string:
return json.Unmarshal([]byte(t), &s.X)
case []byte:
return json.Unmarshal(t, &s.X)
default:
return fmt.Errorf("unkown type %T", x)
}
}
// Value implements driver.Valuer.
func (s SQLJSON[T]) Value() (driver.Value, error) {
res, err := json.Marshal(s.X)
if err != nil {
return nil, err
}
return string(res), nil
}