blob: 7f9e0c6c0e385471487c18a0e7577a6191ee30e2 [file] [log] [blame]
Scott Baker2d897982019-09-24 11:50:08 -07001// Copyright 2016 Google Inc. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package uuid
6
7import "fmt"
8
9// MarshalText implements encoding.TextMarshaler.
10func (uuid UUID) MarshalText() ([]byte, error) {
11 var js [36]byte
12 encodeHex(js[:], uuid)
13 return js[:], nil
14}
15
16// UnmarshalText implements encoding.TextUnmarshaler.
17func (uuid *UUID) UnmarshalText(data []byte) error {
18 id, err := ParseBytes(data)
19 if err == nil {
20 *uuid = id
21 }
22 return err
23}
24
25// MarshalBinary implements encoding.BinaryMarshaler.
26func (uuid UUID) MarshalBinary() ([]byte, error) {
27 return uuid[:], nil
28}
29
30// UnmarshalBinary implements encoding.BinaryUnmarshaler.
31func (uuid *UUID) UnmarshalBinary(data []byte) error {
32 if len(data) != 16 {
33 return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
34 }
35 copy(uuid[:], data)
36 return nil
37}