forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hstore.go
48 lines (39 loc) · 827 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
package types
import (
"fmt"
"reflect"
)
type Hstore struct {
v reflect.Value
append AppenderFunc
scan ScannerFunc
}
var _ ValueAppender = (*Hstore)(nil)
var _ ValueScanner = (*Hstore)(nil)
func NewHstore(vi interface{}) *Hstore {
v := reflect.ValueOf(vi)
if !v.IsValid() {
panic(fmt.Errorf("pg.Hstore(nil)"))
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Map {
panic(fmt.Errorf("pg.Hstore(unsupported %s)", v.Type()))
}
return &Hstore{
v: v,
append: HstoreAppender(v.Type()),
scan: HstoreScanner(v.Type()),
}
}
func (h *Hstore) Value() interface{} {
if h.v.IsValid() {
return h.v.Interface()
}
return nil
}
func (h *Hstore) AppendValue(b []byte, quote int) []byte {
return h.append(b, h.v, quote)
}
func (h *Hstore) ScanValue(rd Reader, n int) error {
return h.scan(h.v, rd, n)
}