Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make Uuid an array, rather than a slice
  • Loading branch information
tamird committed Jul 31, 2016
commit 13259fec6e849a4c8b3d5aea1404a5165d5debe5
12 changes: 0 additions & 12 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,3 @@ regenerate:
go install github.com/gogo/protobuf/protoc-gen-combo
protoc --gogo_out=. --proto_path=../:../../../../:../protobuf/:. thetest.proto
protoc-gen-combo --default=false --proto_path=../:../../../../:../protobuf/:. thetest.proto
cp uuid.go ./combos/both/
cp uuid.go ./combos/marshaler/
cp uuid.go ./combos/unmarshaler/
cp uuid.go ./combos/unsafeboth/
cp uuid.go ./combos/unsafemarshaler/
cp uuid.go ./combos/unsafeunmarshaler/
cp bug_test.go ./combos/both/
cp bug_test.go ./combos/marshaler/
cp bug_test.go ./combos/unmarshaler/
cp bug_test.go ./combos/unsafeboth/
cp bug_test.go ./combos/unsafemarshaler/
cp bug_test.go ./combos/unsafeunmarshaler/
37 changes: 9 additions & 28 deletions test/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"encoding/json"
)

func PutLittleEndianUint64(b []byte, offset int, v uint64) {
func PutLittleEndianUint64(b [16]byte, offset int, v uint64) {
b[offset] = byte(v)
b[offset+1] = byte(v >> 8)
b[offset+2] = byte(v >> 16)
Expand All @@ -45,46 +45,27 @@ func PutLittleEndianUint64(b []byte, offset int, v uint64) {
b[offset+7] = byte(v >> 56)
}

type Uuid []byte
type Uuid [16]byte

func (uuid Uuid) Marshal() ([]byte, error) {
if len(uuid) == 0 {
return nil, nil
}
return []byte(uuid), nil
return uuid[:], nil
}

func (uuid Uuid) MarshalTo(data []byte) (n int, err error) {
if len(uuid) == 0 {
return 0, nil
}
copy(data, uuid)
return 16, nil
return copy(data, uuid[:]), nil
}

func (uuid *Uuid) Unmarshal(data []byte) error {
if len(data) == 0 {
uuid = nil
return nil
}
id := Uuid(make([]byte, 16))
copy(id, data)
*uuid = id
copy(uuid[:], data)
return nil
}

func (uuid *Uuid) Size() int {
if uuid == nil {
return 0
}
if len(*uuid) == 0 {
return 0
}
func (uuid Uuid) Size() int {
return 16
}

func (uuid Uuid) MarshalJSON() ([]byte, error) {
s := hex.EncodeToString([]byte(uuid))
s := hex.EncodeToString(uuid[:])
return json.Marshal(s)
}

Expand All @@ -103,7 +84,7 @@ func (uuid *Uuid) UnmarshalJSON(data []byte) error {
}

func (uuid Uuid) Equal(other Uuid) bool {
return bytes.Equal(uuid[0:], other[0:])
return uuid == other
}

func (uuid Uuid) Compare(other Uuid) int {
Expand All @@ -120,7 +101,7 @@ func NewPopulatedUuid(r int63) *Uuid {
}

func RandV4(r int63) Uuid {
uuid := make(Uuid, 16)
var uuid Uuid
uuid.RandV4(r)
return uuid
}
Expand Down
16 changes: 8 additions & 8 deletions test/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
package test

import (
"github.com/gogo/protobuf/proto"
"testing"

"github.com/gogo/protobuf/proto"
)

func TestBugUuid(t *testing.T) {
u := &CustomContainer{CustomStruct: NidOptCustom{Id: Uuid{}}}
data, err := proto.Marshal(u)
if err != nil {
panic(err)
t.Fatal(err)
}
u2 := &CustomContainer{}
err = proto.Unmarshal(data, u2)
if err != nil {
panic(err)
if err := proto.Unmarshal(data, u2); err != nil {
t.Fatal(err)
}
t.Logf("%+v", u2)
if u2.CustomStruct.Id != nil {
t.Fatalf("should be nil")
empty := Uuid{}
if u2.CustomStruct.Id != empty {
t.Fatal("should be empty")
}
}