Skip to content

Commit b2b3421

Browse files
bmoylannmiyake
authored andcommitted
Add binary package for base64 encoding (#137)
1 parent d12f406 commit b2b3421

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

binary/binary.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2018 Palantir Technologies. 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+
5+
package binary
6+
7+
import (
8+
"encoding/base64"
9+
)
10+
11+
var b64 = base64.StdEncoding
12+
13+
// Binary wraps binary data and provides encoding helpers using base64.StdEncoding.
14+
// Use this type for binary fields serialized/deserialized as base64 text.
15+
// Type is specified as string rather than []byte so that it can be used as a map key.
16+
// Use Bytes() to access the raw bytes.
17+
// Values of this type are only valid when the backing string is Base64-encoded using standard encoding.
18+
type Binary string
19+
20+
func New(data []byte) Binary {
21+
return Binary(b64.EncodeToString(data))
22+
}
23+
24+
func (b Binary) Bytes() ([]byte, error) {
25+
return b64.DecodeString(string(b))
26+
}
27+
28+
func (b Binary) MarshalText() (text []byte, err error) {
29+
// Verify that data is base64-encoded
30+
if _, err := b.Bytes(); err != nil {
31+
return nil, err
32+
}
33+
34+
return []byte(b), nil
35+
}
36+
37+
func (b *Binary) UnmarshalText(data []byte) error {
38+
// Verify that data is base64-encoded
39+
if _, err := Binary(data).Bytes(); err != nil {
40+
return err
41+
}
42+
43+
*b = Binary(data)
44+
return nil
45+
}

binary/binary_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2018 Palantir Technologies. 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+
5+
package binary_test
6+
7+
import (
8+
"encoding/json"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
13+
"github.com/palantir/pkg/binary"
14+
)
15+
16+
func TestBinary_Marshal(t *testing.T) {
17+
for _, test := range []struct {
18+
Name string
19+
Input []byte
20+
Output []byte
21+
}{
22+
{
23+
Name: "hello world",
24+
Input: []byte(`hello world`),
25+
Output: []byte(`"aGVsbG8gd29ybGQ="`),
26+
},
27+
} {
28+
t.Run(test.Name, func(t *testing.T) {
29+
out, err := json.Marshal(binary.New(test.Input))
30+
assert.NoError(t, err)
31+
assert.Equal(t, string(test.Output), string(out))
32+
})
33+
}
34+
}
35+
36+
func TestBinary_Unmarshal(t *testing.T) {
37+
for _, test := range []struct {
38+
Name string
39+
Input []byte
40+
Output []byte
41+
}{
42+
{
43+
Name: "hello world",
44+
Input: []byte(`"aGVsbG8gd29ybGQ="`),
45+
Output: []byte(`hello world`),
46+
},
47+
} {
48+
t.Run(test.Name, func(t *testing.T) {
49+
var bin binary.Binary
50+
err := json.Unmarshal(test.Input, &bin)
51+
assert.NoError(t, err)
52+
bytes, err := bin.Bytes()
53+
assert.NoError(t, err)
54+
assert.Equal(t, string(test.Output), string(bytes))
55+
})
56+
}
57+
}

0 commit comments

Comments
 (0)