-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
50 lines (42 loc) · 1.05 KB
/
array.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
package sequel
import (
"fmt"
"github.com/jackc/pgx/v5/pgtype"
)
var defaultMap = pgtype.NewMap()
// Array is a generic type that implements the sql.Scanner interface.
type Array[T any] []T
// Scan implements the sql.Scanner interface on the Array.
func (a *Array[T]) Scan(src any) error {
typ, ok := defaultMap.TypeForValue(pgtype.Array[T]{})
if !ok {
return fmt.Errorf("cannot type for %T", a)
}
var aa []T
if err := ArrayScan[T](typ.OID, src, &aa); err != nil {
return err
}
*a = aa
return nil
}
// ArrayScan scans the source using the PostgresType with the given oid and
// stores the result in the destination.
func ArrayScan[T any](oid uint32, src any, dest *[]T) error {
if src == nil {
*dest = nil
return nil
}
switch v := src.(type) {
case []byte:
var pgArray pgtype.Array[T]
if err := defaultMap.Scan(oid, pgtype.TextFormatCode, v, &pgArray); err != nil {
return err
}
*dest = pgArray.Elements
return nil
case string:
return ArrayScan(oid, []byte(v), dest)
default:
return fmt.Errorf("unsupported type %T", v)
}
}