forked from jetify-com/typeid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
27 lines (22 loc) · 833 Bytes
/
encoding.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
package typeid
import "encoding"
// TODO: Define a standardized binary encoding for typeids in the spec
// and use that to implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
var _ encoding.TextMarshaler = (*TypeID)(nil)
var _ encoding.TextUnmarshaler = (*TypeID)(nil)
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// It parses a TypeID from a string using the same logic as FromString()
func (tid *TypeID) UnmarshalText(text []byte) error {
parsed, err := FromString(string(text))
if err != nil {
return err
}
*tid = parsed
return nil
}
// MarshalText implements the encoding.TextMarshaler interface.
// It encodes a TypeID as a string using the same logic as String()
func (tid TypeID) MarshalText() (text []byte, err error) {
encoded := tid.String()
return []byte(encoded), nil
}