Skip to content

Commit

Permalink
marshal: Add an error when UUID has wrong length (#890)
Browse files Browse the repository at this point in the history
* marshal: Add expectations to errors for Marshaling

What
===
Add explicit expectations to exactly what error is returned in
test cases where the marshal and unmarshal results in an error.

Why
===
In cases where an unmarshal fails the presence of an error was expected
but not what error was returned. Marshal is not tested for the cases
where it fails in the table tests. Both of these are needed to add
more tests that test for new errors being returned.

* marshal: Add an error when UUID has wrong length

What
===
Add a descriptive error when a UUID is attempted to be marshaled or
unmarshaled and the length of the []byte is not exactly 16 bytes long.

Example
===

```
can not marshal []byte 6 bytes long into timeuuid, must be exactly 16 bytes long
```

Why
===
In cases where UUID []byte was not the correct length the generic `can
not marshal %T into %s` error was being returned. The error was
misleading because it gave the impression the root cause was the type of
the data and not it's length. Additionally the `%T` formatter for a
`[]byte` prints out `[]uint8` which was even more confusing.

* authors: Add Leigh McCulloch to AUTHORS

What
===
Add `Leigh McCulloch` to AUTHORS.

Why
===
As part of his first contribution.
  • Loading branch information
leighmcculloch authored and Zariel committed Apr 9, 2017
1 parent f8b6dca commit 329465a
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 15 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Bo Blanton <bo.blanton@gmail.com>
Vincent Rischmann <me@vrischmann.me>
Jesse Claven <jesse.claven@gmail.com>
Derrick Wippler <thrawn01@gmail.com>
Leigh McCulloch <leigh@leighmcculloch.com>
5 changes: 3 additions & 2 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,9 +1423,10 @@ func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) {
case UUID:
return val.Bytes(), nil
case []byte:
if len(val) == 16 {
return val, nil
if len(val) != 16 {
return nil, marshalErrorf("can not marshal []byte %d bytes long into %s, must be exactly 16 bytes long", len(val), info)
}
return val, nil
case string:
b, err := ParseUUID(val)
if err != nil {
Expand Down
Loading

0 comments on commit 329465a

Please sign in to comment.