Skip to content

feat: add Bytes #1

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ As an added bonus using them as their base type eg. String => string, you have t

Variables:
- `String` - The most useful
- `Bytes`
- `Bool`
- `Float32`
- `Float64`
Expand Down Expand Up @@ -115,4 +116,4 @@ func main() {

License
------
Distributed under MIT License, please see license file in code for more details.
Distributed under MIT License, please see license file in code for more details.
32 changes: 32 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sensitive

import (
"encoding"
"encoding/json"
"fmt"
)

var (
_ fmt.Formatter = (*Bytes)(nil)
_ json.Marshaler = (*Bytes)(nil)
_ encoding.TextMarshaler = (*Bytes)(nil)
FormatBytesFn = func(s Bytes, f fmt.State, c rune) {}
)

type Bytes []byte

func (s Bytes) Format(f fmt.State, c rune) {
FormatBytesFn(s, f, c)
}

func (s Bytes) MarshalJSON() ([]byte, error) {
var ss State
s.Format(&ss, 's')
return json.Marshal(ss.b)
}

func (s Bytes) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 'X')
return ss.b, nil
}
169 changes: 169 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package sensitive

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestBytesFormatting(t *testing.T) {
assert := require.New(t)
value := Bytes("value")
var empty *Bytes

tests := []struct {
name string
formatting string
expected string
value interface{}
}{
{
name: "Bytes %s",
formatting: "%s",
value: value,
},
{
name: "Bytes %q",
formatting: "%q",
value: value,
},
{
name: "Bytes %v",
formatting: "%v",
value: value,
},
{
name: "Bytes %#v",
formatting: "%#v",
value: value,
},
{
name: "Bytes %x",
formatting: "%x",
value: value,
},
{
name: "Bytes %X",
formatting: "%X",
value: value,
},
{
name: "Bytes %T",
formatting: "%T",
value: value,
expected: "sensitive.Bytes",
},
{
name: "Ptr Bytes %s",
formatting: "%s",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %q",
formatting: "%q",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %v",
formatting: "%v",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %#v",
formatting: "%#v",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %x",
formatting: "%x",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %X",
formatting: "%X",
value: empty,
expected: "<nil>",
},
{
name: "Ptr Bytes %T",
formatting: "%T",
value: empty,
expected: "*sensitive.Bytes",
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result := fmt.Sprintf(tc.formatting, tc.value)
assert.Equal(tc.expected, result)
})
}
}

func TestBytesJSON(t *testing.T) {
assert := require.New(t)
value := Bytes("value")

b, err := json.Marshal(value)
assert.NoError(err)
assert.Equal("null", string(b))

var empty *Bytes
b, err = json.Marshal(empty)
assert.NoError(err)
assert.Equal("null", string(b))
}

func TestBytesCustomFormatFn(t *testing.T) {
assert := require.New(t)

oldFn := FormatBytesFn
defer func() {
FormatBytesFn = oldFn
}()
FormatBytesFn = func(s Bytes, f fmt.State, c rune) {
_, _ = f.Write([]byte("blah"))
}

value := Bytes("value")
b, err := json.Marshal(value)
assert.NoError(err)
assert.Equal("\"YmxhaA==\"", string(b))
}

func BenchmarkBytes_Format(b *testing.B) {
value := Bytes("value")
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%s", value)
}
}

func BenchmarkBytes_FormatNative(b *testing.B) {
value := "value"
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%s", value)
}
}

func BenchmarkBytesJSON(b *testing.B) {
value := Bytes("value")
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(value)
}
}

func BenchmarkBytes_JSONNative(b *testing.B) {
value := "value"
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(value)
}
}