Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary package for base64 encoding #137

Merged
merged 5 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions binary/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2018 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package binary

import (
"encoding/base64"
)

var encoding = base64.StdEncoding

// Binary wraps binary data and provides encoding helpers using base64.StdEncoding.
// Use this type for binary fields serialized/deserialized as base64 text.
// We store then encoded string instead of an aliased []byte so this type can be used as a map key.
// Use Bytes() to access the raw bytes.
type Binary string

func New(data []byte) Binary {
return Binary(encoding.EncodeToString(data))
}

func (b Binary) Bytes() ([]byte, error) {
return encoding.DecodeString(string(b))
}

func (b Binary) MarshalText() (text []byte, err error) {
// Test that we can decode data before returning invalid base64
if _, err := b.Bytes(); err != nil {
return nil, err
}

return []byte(b), nil
}

func (b *Binary) UnmarshalText(data []byte) error {
// Test that we can decode data before storing invalid base64
if _, err := b.Bytes(); err != nil {
return err
}

*b = Binary(data)
return nil
}
57 changes: 57 additions & 0 deletions binary/binary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2018 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package binary_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"

"github.com/palantir/pkg/binary"
)

func TestBinary_Marshal(t *testing.T) {
for _, test := range []struct {
Name string
Input []byte
Output []byte
}{
{
Name: "hello world",
Input: []byte(`hello world`),
Output: []byte(`"aGVsbG8gd29ybGQ="`),
},
} {
t.Run(test.Name, func(t *testing.T) {
out, err := json.Marshal(binary.New(test.Input))
assert.NoError(t, err)
assert.Equal(t, string(test.Output), string(out))
})
}
}

func TestBinary_Unmarshal(t *testing.T) {
for _, test := range []struct {
Name string
Input []byte
Output []byte
}{
{
Name: "hello world",
Input: []byte(`"aGVsbG8gd29ybGQ="`),
Output: []byte(`hello world`),
},
} {
t.Run(test.Name, func(t *testing.T) {
var bin binary.Binary
err := json.Unmarshal(test.Input, &bin)
assert.NoError(t, err)
bytes, err := bin.Bytes()
assert.NoError(t, err)
assert.Equal(t, string(test.Output), string(bytes))
})
}
}