-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathjson_map.go
More file actions
73 lines (60 loc) · 1.84 KB
/
json_map.go
File metadata and controls
73 lines (60 loc) · 1.84 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
package types
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// JSONMap defines a map that is safe for json and db read/write.
type JSONMap[T any] map[string]T
// MarshalJSON implements the [json.Marshaler] interface.
func (m JSONMap[T]) MarshalJSON() ([]byte, error) {
type alias JSONMap[T] // prevent recursion
// initialize an empty map to ensure that `{}` is returned as json
if m == nil {
m = JSONMap[T]{}
}
return json.Marshal(alias(m))
}
// String returns the string representation of the current json map.
func (m JSONMap[T]) String() string {
v, _ := m.MarshalJSON()
return string(v)
}
// Get retrieves a single value from the current JSONMap[T].
//
// This helper was added primarily to assist the goja integration since custom map types
// don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
func (m JSONMap[T]) Get(key string) T {
return m[key]
}
// Set sets a single value in the current JSONMap[T].
//
// This helper was added primarily to assist the goja integration since custom map types
// don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
func (m JSONMap[T]) Set(key string, value T) {
m[key] = value
}
// Value implements the [driver.Valuer] interface.
func (m JSONMap[T]) Value() (driver.Value, error) {
data, err := json.Marshal(m)
return string(data), err
}
// Scan implements [sql.Scanner] interface to scan the provided value
// into the current JSONMap[T] instance.
func (m *JSONMap[T]) Scan(value any) error {
var data []byte
switch v := value.(type) {
case nil:
// no cast needed
case []byte:
data = v
case string:
data = []byte(v)
default:
return fmt.Errorf("failed to unmarshal JSONMap[T] value: %q", value)
}
if len(data) == 0 {
data = []byte("{}")
}
return json.Unmarshal(data, m)
}