Просмотр исходного кода

moved separate uuid package to the main package

Christoph Hack 12 лет назад
Родитель
Сommit
6e56055fd0
6 измененных файлов с 38 добавлено и 44 удалено
  1. 3 4
      README.md
  2. 5 7
      cassandra_test.go
  3. 4 6
      marshal.go
  4. 1 2
      marshal_test.go
  5. 9 9
      uuid.go
  6. 16 16
      uuid_test.go

+ 3 - 4
README.md

@@ -55,7 +55,6 @@ import (
 	"log"
 
 	"tux21b.org/v1/gocql"
-	"tux21b.org/v1/gocql/uuid"
 )
 
 func main() {
@@ -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
@@ -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.

+ 5 - 7
cassandra_test.go

@@ -13,8 +13,6 @@ import (
 	"sync"
 	"testing"
 	"time"
-
-	"tux21b.org/v1/gocql/uuid"
 )
 
 var (
@@ -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`,
@@ -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
@@ -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"},
@@ -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),
 	},

+ 4 - 6
marshal.go

@@ -10,8 +10,6 @@ import (
 	"math"
 	"reflect"
 	"time"
-
-	"tux21b.org/v1/gocql/uuid"
 )
 
 // Marshaler is the interface implemented by objects that can marshal
@@ -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)
@@ -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)
@@ -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")
 		}

+ 1 - 2
marshal_test.go

@@ -7,7 +7,6 @@ import (
 	"strings"
 	"testing"
 	"time"
-	"tux21b.org/v1/gocql/uuid"
 )
 
 var marshalTests = []struct {
@@ -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},

+ 9 - 9
uuid/uuid.go → uuid.go

@@ -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"
@@ -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")
@@ -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)

+ 16 - 16
uuid/uuid_test.go → uuid_test.go

@@ -2,7 +2,7 @@
 // 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"
@@ -10,7 +10,7 @@ import (
 	"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 {
@@ -18,7 +18,7 @@ func TestNil(t *testing.T) {
 	}
 }
 
-var tests = []struct {
+var testsUUID = []struct {
 	input   string
 	variant int
 	version int
@@ -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)
 			}
 		}
 	}
@@ -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())