blob: 14bd34072b64d2c9cd0491d00454e593af99e1e2 [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001// 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)
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000019 if err != nil {
20 return err
divyadesai81bb7ba2020-03-11 11:45:23 +000021 }
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000022 *uuid = id
23 return nil
divyadesai81bb7ba2020-03-11 11:45:23 +000024}
25
26// MarshalBinary implements encoding.BinaryMarshaler.
27func (uuid UUID) MarshalBinary() ([]byte, error) {
28 return uuid[:], nil
29}
30
31// UnmarshalBinary implements encoding.BinaryUnmarshaler.
32func (uuid *UUID) UnmarshalBinary(data []byte) error {
33 if len(data) != 16 {
34 return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
35 }
36 copy(uuid[:], data)
37 return nil
38}