-
Notifications
You must be signed in to change notification settings - Fork 11
/
streamid.go
114 lines (94 loc) · 2.76 KB
/
streamid.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storj
import (
"database/sql/driver"
"encoding/base64"
"github.com/zeebo/errs"
)
// ErrStreamID is used when something goes wrong with a stream ID.
var ErrStreamID = errs.Class("stream ID")
// StreamID is the unique identifier for stream related to object.
type StreamID []byte
// StreamIDFromString decodes an base32 encoded.
func StreamIDFromString(s string) (StreamID, error) {
idBytes, err := base32Encoding.DecodeString(s)
if err != nil {
return StreamID{}, ErrStreamID.Wrap(err)
}
return StreamIDFromBytes(idBytes)
}
// StreamIDFromBytes converts a byte slice into a stream ID.
func StreamIDFromBytes(b []byte) (StreamID, error) {
id := make([]byte, len(b))
copy(id, b)
return id, nil
}
// IsZero returns whether stream ID is unassigned.
func (id StreamID) IsZero() bool {
return len(id) == 0
}
// String representation of the stream ID.
func (id StreamID) String() string { return base32Encoding.EncodeToString(id.Bytes()) }
// Bytes returns bytes of the stream ID.
func (id StreamID) Bytes() []byte { return id[:] }
// Marshal serializes a stream ID.
func (id StreamID) Marshal() ([]byte, error) {
return id.Bytes(), nil
}
// MarshalTo serializes a stream ID into the passed byte slice.
func (id *StreamID) MarshalTo(data []byte) (n int, err error) {
n = copy(data, id.Bytes())
return n, nil
}
// Unmarshal deserializes a stream ID.
func (id *StreamID) Unmarshal(data []byte) error {
var err error
*id, err = StreamIDFromBytes(data)
return err
}
// Size returns the length of a stream ID (implements gogo's custom type interface).
func (id StreamID) Size() int {
return len(id)
}
// MarshalText serializes a stream ID to a base32 string.
func (id StreamID) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText deserializes a base32 string to a stream ID.
func (id *StreamID) UnmarshalText(data []byte) error {
var err error
*id, err = StreamIDFromString(string(data))
if err != nil {
return err
}
return nil
}
// Value set a stream ID to a database field.
func (id StreamID) Value() (driver.Value, error) {
return id.Bytes(), nil
}
// Scan extracts a stream ID from a database field.
func (id *StreamID) Scan(src interface{}) (err error) {
b, ok := src.([]byte)
if !ok {
return ErrStreamID.New("Stream ID Scan expects []byte")
}
n, err := StreamIDFromBytes(b)
*id = n
return err
}
// DecodeSpanner implements spanner.Decoder.
func (id *StreamID) DecodeSpanner(val any) (err error) {
if v, ok := val.(string); ok {
val, err = base64.StdEncoding.DecodeString(v)
if err != nil {
return err
}
}
return id.Scan(val)
}
// EncodeSpanner implements spanner.Encoder.
func (id StreamID) EncodeSpanner() (any, error) {
return id.Value()
}