-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathid.go
42 lines (37 loc) · 955 Bytes
/
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
package schema
import (
"context"
"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.
IDField = Field{
Description: "The item's id",
Required: true,
ReadOnly: true,
OnInit: NewID,
Filterable: true,
Sortable: true,
Validator: &String{
// This regexp matches a base32 id
Regexp: "^[0-9a-v]{20}$",
},
}
)
// newID returns a new globally unique id using a copy of the mgo/bson
// algorithm.
func newID() string {
return xid.New().String()
}