Skip to content

Commit

Permalink
moved separate uuid package to the main package
Browse files Browse the repository at this point in the history
  • Loading branch information
tux21b committed Jan 11, 2014
1 parent a8a708d commit 6e56055
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import (
"log"

"tux21b.org/v1/gocql"
"tux21b.org/v1/gocql/uuid"
)

func main() {
Expand All @@ -68,11 +67,11 @@ func main() {

// insert a tweet
if err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`,
"me", uuid.TimeUUID(), "hello world").Exec(); err != nil {
"me", gocql.TimeUUID(), "hello world").Exec(); err != nil {
log.Fatal(err)
}

var id uuid.UUID
var id gocql.UUID
var text string

// select a single tweet
Expand All @@ -96,6 +95,6 @@ func main() {
License
-------

> Copyright (c) 2012 The gocql Authors. All rights reserved.
> Copyright (c) 2012-2014 The gocql Authors. All rights reserved.
> Use of this source code is governed by a BSD-style
> license that can be found in the LICENSE file.
12 changes: 5 additions & 7 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"sync"
"testing"
"time"

"tux21b.org/v1/gocql/uuid"
)

var (
Expand Down Expand Up @@ -196,9 +194,9 @@ func TestCAS(t *testing.T) {
t.Fatal("create:", err)
}

title, revid := "baz", uuid.TimeUUID()
title, revid := "baz", TimeUUID()
var titleCAS string
var revidCAS uuid.UUID
var revidCAS UUID

if applied, err := session.Query(`INSERT INTO cas_table (title, revid)
VALUES (?, ?) IF NOT EXISTS`,
Expand Down Expand Up @@ -249,7 +247,7 @@ func TestBatch(t *testing.T) {

type Page struct {
Title string
RevId uuid.UUID
RevId UUID
Body string
Views int64
Protected bool
Expand All @@ -263,7 +261,7 @@ type Attachment []byte
var pageTestData = []*Page{
&Page{
Title: "Frontpage",
RevId: uuid.TimeUUID(),
RevId: TimeUUID(),
Body: "Welcome to this wiki page!",
Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
Tags: []string{"start", "important", "test"},
Expand All @@ -274,7 +272,7 @@ var pageTestData = []*Page{
},
&Page{
Title: "Foobar",
RevId: uuid.TimeUUID(),
RevId: TimeUUID(),
Body: "foo::Foo f = new foo::Foo(foo::Foo::INIT);",
Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
},
Expand Down
10 changes: 4 additions & 6 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"math"
"reflect"
"time"

"tux21b.org/v1/gocql/uuid"
)

// Marshaler is the interface implemented by objects that can marshal
Expand Down Expand Up @@ -908,7 +906,7 @@ func marshalUUID(info *TypeInfo, value interface{}) ([]byte, error) {
if val, ok := value.([]byte); ok && len(val) == 16 {
return val, nil
}
if val, ok := value.(uuid.UUID); ok {
if val, ok := value.(UUID); ok {
return val.Bytes(), nil
}
return nil, marshalErrorf("can not marshal %T into %s", value, info)
Expand All @@ -918,8 +916,8 @@ func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
case *uuid.UUID:
*v = uuid.FromBytes(data)
case *UUID:
*v = UUIDFromBytes(data)
return nil
}
return unmarshalErrorf("can not unmarshal %s into %T", info, value)
Expand All @@ -928,7 +926,7 @@ func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case *time.Time:
id := uuid.FromBytes(data)
id := UUIDFromBytes(data)
if id.Version() != 1 {
return unmarshalErrorf("invalid timeuuid")
}
Expand Down
3 changes: 1 addition & 2 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"testing"
"time"
"tux21b.org/v1/gocql/uuid"
)

var marshalTests = []struct {
Expand Down Expand Up @@ -53,7 +52,7 @@ var marshalTests = []struct {
{
&TypeInfo{Type: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
uuid.FromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0}),
UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0}),
},
{
&TypeInfo{Type: TypeInt},
Expand Down
18 changes: 9 additions & 9 deletions uuid/uuid.go → uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// identifiers, a standardized format in the form of a 128 bit number.
//
// http://tools.ietf.org/html/rfc4122
package uuid
package gocql

import (
"crypto/rand"
Expand Down Expand Up @@ -75,9 +75,9 @@ func ParseUUID(input string) (UUID, error) {
return u, nil
}

// FromBytes converts a raw byte slice to an UUID. It will panic if the slice
// isn't exactly 16 bytes long.
func FromBytes(input []byte) UUID {
// UUIDFromBytes converts a raw byte slice to an UUID. It will panic if the
// slice isn't exactly 16 bytes long.
func UUIDFromBytes(input []byte) UUID {
var u UUID
if len(input) != 16 {
panic("UUIDs must be exactly 16 bytes long")
Expand All @@ -103,13 +103,13 @@ var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix()
// TimeUUID generates a new time based UUID (version 1) using the current
// time as the timestamp.
func TimeUUID() UUID {
return FromTime(time.Now())
return UUIDFromTime(time.Now())
}

// FromTime generates a new time based UUID (version 1) as described in RFC
// 4122. This UUID contains the MAC address of the node that generated the
// UUID, the given timestamp and a sequence number.
func FromTime(aTime time.Time) UUID {
// UUIDFromTime generates a new time based UUID (version 1) as described in
// RFC 4122. This UUID contains the MAC address of the node that generated
// the UUID, the given timestamp and a sequence number.
func UUIDFromTime(aTime time.Time) UUID {
var u UUID

utcTime := aTime.In(time.UTC)
Expand Down
32 changes: 16 additions & 16 deletions uuid/uuid_test.go → uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package uuid
package gocql

import (
"bytes"
"testing"
"time"
)

func TestNil(t *testing.T) {
func TestUUIDNil(t *testing.T) {
var uuid UUID
want, got := "00000000-0000-0000-0000-000000000000", uuid.String()
if want != got {
t.Fatalf("TestNil: expected %q got %q", want, got)
}
}

var tests = []struct {
var testsUUID = []struct {
input string
variant int
version int
Expand All @@ -37,26 +37,26 @@ var tests = []struct {
{"d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d", VariantFuture, 0},
}

func TestPredefined(t *testing.T) {
for i := range tests {
uuid, err := ParseUUID(tests[i].input)
func TestPredefinedUUID(t *testing.T) {
for i := range testsUUID {
uuid, err := ParseUUID(testsUUID[i].input)
if err != nil {
t.Errorf("ParseUUID #%d: %v", i, err)
continue
}

if str := uuid.String(); str != tests[i].input {
t.Errorf("String #%d: expected %q got %q", i, tests[i].input, str)
if str := uuid.String(); str != testsUUID[i].input {
t.Errorf("String #%d: expected %q got %q", i, testsUUID[i].input, str)
continue
}

if variant := uuid.Variant(); variant != tests[i].variant {
t.Errorf("Variant #%d: expected %d got %d", i, tests[i].variant, variant)
if variant := uuid.Variant(); variant != testsUUID[i].variant {
t.Errorf("Variant #%d: expected %d got %d", i, testsUUID[i].variant, variant)
}

if tests[i].variant == VariantIETF {
if version := uuid.Version(); version != tests[i].version {
t.Errorf("Version #%d: expected %d got %d", i, tests[i].version, version)
if testsUUID[i].variant == VariantIETF {
if version := uuid.Version(); version != testsUUID[i].version {
t.Errorf("Version #%d: expected %d got %d", i, testsUUID[i].version, version)
}
}
}
Expand All @@ -75,16 +75,16 @@ func TestRandomUUID(t *testing.T) {
}
}

func TestFromTime(t *testing.T) {
func TestUUIDFromTime(t *testing.T) {
date := time.Date(1982, 5, 5, 12, 34, 56, 0, time.UTC)
uuid := FromTime(date)
uuid := UUIDFromTime(date)

if uuid.Time() != date {
t.Errorf("embedded time incorrect. Expected %v got %v", date, uuid.Time())
}
}

func TestParse(t *testing.T) {
func TestParseUUID(t *testing.T) {
uuid, _ := ParseUUID("486f3a88-775b-11e3-ae07-d231feb1dc81")
if uuid.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", uuid.Time())
Expand Down

0 comments on commit 6e56055

Please sign in to comment.