-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary package for base64 encoding (#137)
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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 b64 = base64.StdEncoding | ||
|
||
// Binary wraps binary data and provides encoding helpers using base64.StdEncoding. | ||
// Use this type for binary fields serialized/deserialized as base64 text. | ||
// Type is specified as string rather than []byte so that it can be used as a map key. | ||
// Use Bytes() to access the raw bytes. | ||
// Values of this type are only valid when the backing string is Base64-encoded using standard encoding. | ||
type Binary string | ||
|
||
func New(data []byte) Binary { | ||
return Binary(b64.EncodeToString(data)) | ||
} | ||
|
||
func (b Binary) Bytes() ([]byte, error) { | ||
return b64.DecodeString(string(b)) | ||
} | ||
|
||
func (b Binary) MarshalText() (text []byte, err error) { | ||
// Verify that data is base64-encoded | ||
if _, err := b.Bytes(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(b), nil | ||
} | ||
|
||
func (b *Binary) UnmarshalText(data []byte) error { | ||
// Verify that data is base64-encoded | ||
if _, err := Binary(data).Bytes(); err != nil { | ||
return err | ||
} | ||
|
||
*b = Binary(data) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |