-
Notifications
You must be signed in to change notification settings - Fork 3
/
codec.go
29 lines (24 loc) · 1.04 KB
/
codec.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
package freedb
import "errors"
const basicCodecPrefix string = "!"
// basicCodec encodes and decodes any bytes data using a very simple encoding rule.
// A prefix (an exclamation mark "!") will be attached to the raw bytes data.
//
// This allows the library to differentiate between empty raw bytes provided by the client from
// getting an empty data from Google Sheets API.
type basicCodec struct{}
// Encode encodes the given raw bytes by using an exclamation mark "!" as a prefix.
func (c *basicCodec) Encode(value []byte) (string, error) {
return basicCodecPrefix + string(value), nil
}
// Decode converts the string data read from Google Sheet into raw bytes data after removing the
// exclamation mark "!" prefix.
func (c *basicCodec) Decode(value string) ([]byte, error) {
if len(value) == 0 {
return nil, errors.New("basic decode fail: empty string")
}
if value[:len(basicCodecPrefix)] != basicCodecPrefix {
return nil, errors.New("basic decode fail: first character is not an empty space")
}
return []byte(value[len(basicCodecPrefix):]), nil
}