-
Notifications
You must be signed in to change notification settings - Fork 10
/
ObjectId.go
52 lines (42 loc) · 1.06 KB
/
ObjectId.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
package cqrs
import (
"github.com/pjvds/gouuid"
)
type ObjectId uuid.UUID
// Creates a new unique ObjectId.
func NewObjectId() ObjectId {
guid, err := uuid.NewV4()
if err != nil {
panic(err)
}
return ObjectId(*guid)
}
// Creates a ObjectId from the given string value. It
// accepts strings in the following format: "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
func ParseObjectId(value string) (id ObjectId, err error) {
guid := new(uuid.UUID)
if guid, err = uuid.ParseHex(value); err == nil {
id = ObjectId(*guid)
}
return
}
// Returns a string representation of the ObjectId, like 6ba7b814-9dad-11d1-80b4-00c04fd430c8.
func (id ObjectId) String() string {
guid := uuid.UUID(id)
return guid.String()
}
// Returns the JSON encoding of the id.
func (id ObjectId) MarshalJSON() ([]byte, error) {
value := uuid.UUID(id)
return value.MarshalJSON()
}
// Sets id to the copy of the data.
func (id *ObjectId) UnmarshalJSON(b []byte) error {
value := uuid.UUID(*id)
err := value.UnmarshalJSON(b)
if err != nil {
return err
}
*id = ObjectId(value)
return nil
}