-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
58 lines (52 loc) · 1.35 KB
/
id.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
package pgsql
import (
"context"
"github.com/rs/rest-layer/schema"
"github.com/rs/xid"
)
var (
// NewID is a field hook handler that generates a new globally unique id if
// none exist, to be used in schema with OnInit.
//
// The generated ID is a Mongo like base64 object id (mgo/bson code has been
// embedded into this function to prevent dep).
NewID = func(ctx context.Context, value interface{}) interface{} {
if value == nil {
value = newID()
}
return value
}
// IDField is a common schema field configuration that generate an globally
// unique id for new item id.
IDFieldValidator = &schema.String{
// This regexp matches a base32 id
Regexp: "^[0-9a-v]{20}$",
}
IDField = schema.Field{
Description: "The item's id",
Required: true,
ReadOnly: true,
OnInit: NewID,
Filterable: true,
Sortable: true,
Validator: IDFieldValidator,
}
// SerialID is a common schema field configuration that generated and
// maintained by storage layer.
SerialID = schema.Field{
Description: "The item's auto increase id",
Required: true,
ReadOnly: true,
Filterable: true,
Sortable: true,
Default: "0",
Validator: &schema.String{
Regexp: "^[0-9]{1,20}$",
},
}
)
// newID returns a new globally unique id using a copy of the mgo/bson
// algorithm.
func newID() string {
return xid.New().String()
}