...
1 package xidb
2
3 import (
4 "database/sql/driver"
5 "fmt"
6
7 "github.com/rs/xid"
8 )
9
10 type ID struct {
11 xid.ID
12 }
13
14
15 func (id ID) Value() (driver.Value, error) {
16 if id.ID.IsNil() {
17 return nil, nil
18 }
19 return id.ID[:], nil
20 }
21
22
23 func (id *ID) Scan(value interface{}) (err error) {
24 switch val := value.(type) {
25 case []byte:
26 i, err := xid.FromBytes(val)
27 if err != nil {
28 return err
29 }
30 *id = ID{ID: i}
31 return nil
32 case nil:
33 *id = ID{ID: xid.NilID()}
34 return nil
35 default:
36 return fmt.Errorf("xid: scanning unsupported type: %T", value)
37 }
38 }
39
View as plain text