-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathguid.go
90 lines (70 loc) · 1.69 KB
/
guid.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package dtyp
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
uuid "github.com/oiweiwei/go-msrpc/midl/uuid"
)
func (g *GUID) String() string {
if g != nil && len(g.Data4) == 8 {
return fmt.Sprintf("%08x-%04x-%04x-%02x%02x-%012s",
g.Data1, g.Data2, g.Data3, g.Data4[0], g.Data4[1], hex.EncodeToString(g.Data4[2:]))
}
return ""
}
func (g *GUID) MarshalJSON() ([]byte, error) {
return json.Marshal(g.String())
}
func GUIDFromBytes(b []byte) (*GUID, error) {
u := &uuid.UUID{}
if err := u.DecodeBinary(b); err != nil {
return nil, err
}
return GUIDFromUUID(u), nil
}
func GUIDFromUUID(u *uuid.UUID) *GUID {
return &GUID{
Data1: u.TimeLow,
Data2: u.TimeMid,
Data3: u.TimeHiAndVersion,
Data4: []byte{u.ClockSeqHiAndReserved, u.ClockSeqLow, u.Node[0], u.Node[1], u.Node[2], u.Node[3], u.Node[4], u.Node[5]},
}
}
func (o *GUID) UUID() *uuid.UUID {
data4 := o.Data4
if len(data4) < 8 {
data4 = make([]byte, 8)
}
return &uuid.UUID{
TimeLow: o.Data1,
TimeMid: o.Data2,
TimeHiAndVersion: o.Data3,
ClockSeqHiAndReserved: data4[0],
ClockSeqLow: data4[1],
Node: [6]byte{data4[2], data4[3], data4[4], data4[5], data4[6], data4[7]},
}
}
func (o *GUID) IsZero() bool {
if o == nil {
return true
}
if !(o.Data1 == 0 && o.Data2 == 0 && o.Data3 == 0) {
return false
}
for i := range o.Data4 {
if o.Data4[i] != 0 {
return false
}
}
return true
}
func (o *GUID) Equal(other *GUID) bool {
if o == nil != (other == nil) {
return false
}
if o == nil {
return true
}
return o.Data1 == other.Data1 && o.Data2 == other.Data2 && o.Data3 == other.Data3 && bytes.Compare(o.Data4, other.Data4) == 0
}