File: id.go

package info (click to toggle)
golang-github-rs-xid 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 152 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 638 bytes parent folder | download | duplicates (2)
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
package xidb

import (
	"database/sql/driver"
	"fmt"

	"github.com/rs/xid"
)

type ID struct {
	xid.ID
}

// Value implements the driver.Valuer interface.
func (id ID) Value() (driver.Value, error) {
	if id.ID.IsNil() {
		return nil, nil
	}
	return id.ID[:], nil
}

// Scan implements the sql.Scanner interface.
func (id *ID) Scan(value interface{}) (err error) {
	switch val := value.(type) {
	case []byte:
		i, err := xid.FromBytes(val)
		if err != nil {
			return err
		}
		*id = ID{ID: i}
		return nil
	case nil:
		*id = ID{ID: xid.NilID()}
		return nil
	default:
		return fmt.Errorf("xid: scanning unsupported type: %T", value)
	}
}