Add boil.Byte type

This commit is contained in:
Patrick O'brien 2016-11-12 10:25:07 +10:00
parent d27823de53
commit 3bfdae6b5c
3 changed files with 137 additions and 2 deletions

61
types/byte.go Normal file
View file

@ -0,0 +1,61 @@
package types
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// Byte is an alias for byte.
// Byte implements Marshal and Unmarshal.
type Byte byte
// String output your byte.
func (b Byte) String() string {
return string(b)
}
// UnmarshalJSON sets *b to a copy of data.
func (b *Byte) UnmarshalJSON(data []byte) error {
if b == nil {
return errors.New("json: unmarshal json on nil pointer to byte")
}
var x string
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if len(x) > 1 {
return errors.New("json: cannot convert to byte, text len is greater than one")
}
*b = Byte(x[0])
return nil
}
// MarshalJSON returns the JSON encoding of b.
func (b Byte) MarshalJSON() ([]byte, error) {
return []byte{'"', byte(b), '"'}, nil
}
// Value returns b as a driver.Value.
func (b Byte) Value() (driver.Value, error) {
return []byte{byte(b)}, nil
}
// Scan stores the src in *b.
func (b *Byte) Scan(src interface{}) error {
switch src.(type) {
case uint8:
*b = Byte(src.(uint8))
case string:
*b = Byte(src.(string)[0])
case []byte:
*b = Byte(src.([]byte)[0])
default:
return errors.New("incompatible type for byte")
}
return nil
}

74
types/byte_test.go Normal file
View file

@ -0,0 +1,74 @@
package types
import (
"bytes"
"encoding/json"
"testing"
)
func TestByteString(t *testing.T) {
t.Parallel()
b := Byte('b')
if b.String() != "b" {
t.Errorf("Expected %q, got %s", "b", b.String())
}
}
func TestByteUnmarshal(t *testing.T) {
t.Parallel()
var b Byte
err := json.Unmarshal([]byte(`"b"`), &b)
if err != nil {
t.Error(err)
}
if b != 'b' {
t.Errorf("Expected %q, got %s", "b", b)
}
}
func TestByteMarshal(t *testing.T) {
t.Parallel()
b := Byte('b')
res, err := json.Marshal(&b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(res, []byte(`"b"`)) {
t.Errorf("expected %s, got %s", `"b"`, b.String())
}
}
func TestByteValue(t *testing.T) {
t.Parallel()
b := Byte('b')
v, err := b.Value()
if err != nil {
t.Error(err)
}
if !bytes.Equal([]byte{byte(b)}, v.([]byte)) {
t.Errorf("byte mismatch, %v %v", b, v)
}
}
func TestByteScan(t *testing.T) {
t.Parallel()
var b Byte
s := "b"
err := b.Scan(s)
if err != nil {
t.Error(err)
}
if !bytes.Equal([]byte{byte(b)}, []byte{'b'}) {
t.Errorf("bad []byte: %#v ≠ %#v\n", b, "b")
}
}

View file

@ -35,7 +35,7 @@ func (j *JSON) Marshal(obj interface{}) error {
// UnmarshalJSON sets *j to a copy of data.
func (j *JSON) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("JSON: UnmarshalJSON on nil pointer")
return errors.New("json: unmarshal json on nil pointer to json")
}
*j = append((*j)[0:0], data...)
@ -68,7 +68,7 @@ func (j *JSON) Scan(src interface{}) error {
case []byte:
source = src.([]byte)
default:
return errors.New("Incompatible type for JSON")
return errors.New("incompatible type for json")
}
*j = JSON(append((*j)[0:0], source...))