/
hstore.go
59 lines (47 loc) · 984 Bytes
/
hstore.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
package types
import (
"fmt"
"reflect"
)
type Hstore struct {
v reflect.Value
append AppenderFunc
scan ScannerFunc
}
var (
_ ValueAppender = (*Hstore)(nil)
_ ValueScanner = (*Hstore)(nil)
)
func NewHstore(vi interface{}) *Hstore {
v := reflect.ValueOf(vi)
if !v.IsValid() {
panic(fmt.Errorf("pg.Hstore(nil)"))
}
typ := v.Type()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Map {
panic(fmt.Errorf("pg.Hstore(unsupported %s)", typ))
}
return &Hstore{
v: v,
append: HstoreAppender(typ),
scan: HstoreScanner(typ),
}
}
func (h *Hstore) Value() interface{} {
if h.v.IsValid() {
return h.v.Interface()
}
return nil
}
func (h *Hstore) AppendValue(b []byte, flags int) ([]byte, error) {
return h.append(b, h.v, flags), nil
}
func (h *Hstore) ScanValue(rd Reader, n int) error {
if h.v.Kind() != reflect.Ptr {
return fmt.Errorf("pg: Hstore(non-pointer %s)", h.v.Type())
}
return h.scan(h.v.Elem(), rd, n)
}