Debian Codegraph @

types.go

raw
1 package uuid
2
3 import (
4 "database/sql/driver"
5 "fmt"
6 "strings"
7 )
8
9 const (
10 length = 16
11 variantIndex = 8
12 versionIndex = 6
13 )
14
15 // **************************************************** Create UUIDs
16
17 type array [length]byte
18
19 func (o *array) unmarshal(pData []byte) {
20 copy(o[:], pData)
21 }
22
23 // Set the three most significant bits (bits 0, 1 and 2) of the
24 // sequenceHiAndVariant equivalent in the array to ReservedRFC4122.
25 func (o *array) setRFC4122Version(pVersion uint8) {
26 o[versionIndex] &= 0x0f
27 o[versionIndex] |= uint8(pVersion << 4)
28 o[variantIndex] &= variantSet
29 o[variantIndex] |= VariantRFC4122
30 }
31
32 // **************************************************** Default implementation
33
34 var _ UUID = &Uuid{}
35
36 // Uuid is the default UUID implementation. All uuid functions will return this
37 // type. All uuid functions should use UUID as their in parameter.
38 type Uuid []byte
39
40 // Size returns the octet length of the Uuid
41 func (o Uuid) Size() int {
42 return length
43 }
44
45 // Version returns the uuid.Version of the Uuid
46 func (o Uuid) Version() Version {
47 return resolveVersion(o[versionIndex] >> 4)
48 }
49
50 // Variant returns the implementation variant of the Uuid
51 func (o Uuid) Variant() uint8 {
52 return variant(o[variantIndex])
53 }
54
55 // Bytes return the underlying data representation of the Uuid in network byte
56 // order
57 func (o Uuid) Bytes() []byte {
58 return o
59 }
60
61 // String returns the canonical string representation of the UUID or the
62 // uuid.Format the package is set to via uuid.SwitchFormat
63 func (o Uuid) String() string {
64 return formatUuid(o, printFormat)
65 }
66
67 // **************************************************** Implementations
68
69 // MarshalBinary implements the encoding.BinaryMarshaler interface
70 func (o Uuid) MarshalBinary() ([]byte, error) {
71 return o.Bytes(), nil
72 }
73
74 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
75 func (o *Uuid) UnmarshalBinary(pBytes []byte) error {
76 if len(pBytes) != o.Size() {
77 return fmt.Errorf("uuid.Uuid.UnmarshalBinary: invalid length")
78 }
79 if len(*o) != 0 {
80 panic("uuid.Uuid.UnmarshalBinary: you must use an empty or new Uuid to unmarhal bytes")
81 }
82 *o = append(*o, pBytes...)
83 return nil
84 }
85
86 // MarshalText implements the encoding.TextMarshaler interface. It will marshal
87 // text into one of the known formats, if you have changed to a custom Format
88 // the text
89 func (o Uuid) MarshalText() ([]byte, error) {
90 f := FormatCanonical
91 if defaultFormats[printFormat] {
92 f = printFormat
93 }
94 return []byte(strings.ToLower(string(format(o.Bytes(), string(f))))), nil
95 }
96
97 // UnmarshalText implements the encoding.TextUnmarshaler interface. It will
98 // support any text that MarshalText can produce.
99 func (o *Uuid) UnmarshalText(pUuid []byte) error {
100 id, err := parse(string(pUuid))
101 if err == nil {
102 o.UnmarshalBinary(id)
103 }
104 return err
105 }
106
107 // Value implements the driver.Valuer interface
108 func (o Uuid) Value() (value driver.Value, err error) {
109 if len(o) == 0 {
110 value, err = nil, nil
111 return
112 }
113 value, err = o.MarshalText()
114 return
115 }
116
117 // Scan implements the sql.Scanner interface
118 func (o *Uuid) Scan(pSrc interface{}) error {
119 if pSrc == nil {
120 return nil
121 }
122 if pSrc == "" {
123 return nil
124 }
125 switch src := pSrc.(type) {
126
127 case string:
128 return o.UnmarshalText([]byte(src))
129
130 case []byte:
131 if len(src) == length {
132 return o.UnmarshalBinary(src)
133 } else {
134 return o.UnmarshalText(src)
135 }
136
137 default:
138 return fmt.Errorf("uuid.Uuid.Scan: cannot scan type %T into Uuid", pSrc)
139 }
140 }
141
142 // **************************************************** Immutable UUID
143
144 var _ UUID = new(Immutable)
145
146 // Immutable is an easy to use UUID which can be used as a key or for constants
147 type Immutable string
148
149 // Size returns the octet length of the Uuid
150 func (o Immutable) Size() int {
151 return length
152 }
153
154 // Version returns the uuid.Version of the Uuid
155 func (o Immutable) Version() Version {
156 return resolveVersion(o[versionIndex] >> 4)
157 }
158
159 // Variant returns the implementation variant of the Uuid
160 func (o Immutable) Variant() uint8 {
161 return variant(o[variantIndex])
162 }
163
164 // Bytes return the underlying data representation of the Uuid in network byte
165 // order
166 func (o Immutable) Bytes() []byte {
167 return Uuid(o).Bytes()
168 }
169
170 // String returns the canonical string representation of the UUID or the
171 // uuid.Format the package is set to via uuid.SwitchFormat
172 func (o Immutable) String() string {
173 return Uuid(o).String()
174 }