mirror of https://github.com/ethereum/go-ethereum
Merge pull request #1792 from jeffallen/uuid
Change go-uuid to use the current supported repository.pull/1802/head
commit
a9c809b441
@ -0,0 +1 @@ |
|||||||
|
Paul Borman <borman@google.com> |
@ -1,4 +1,4 @@ |
|||||||
Copyright (c) 2009 Google Inc. All rights reserved. |
Copyright (c) 2009,2014 Google Inc. All rights reserved. |
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
||||||
modification, are permitted provided that the following conditions are |
modification, are permitted provided that the following conditions are |
@ -0,0 +1,30 @@ |
|||||||
|
// Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package uuid |
||||||
|
|
||||||
|
import "errors" |
||||||
|
|
||||||
|
func (u UUID) MarshalJSON() ([]byte, error) { |
||||||
|
if len(u) == 0 { |
||||||
|
return []byte(`""`), nil |
||||||
|
} |
||||||
|
return []byte(`"` + u.String() + `"`), nil |
||||||
|
} |
||||||
|
|
||||||
|
func (u *UUID) UnmarshalJSON(data []byte) error { |
||||||
|
if len(data) == 0 || string(data) == `""` { |
||||||
|
return nil |
||||||
|
} |
||||||
|
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { |
||||||
|
return errors.New("invalid UUID format") |
||||||
|
} |
||||||
|
data = data[1 : len(data)-1] |
||||||
|
uu := Parse(string(data)) |
||||||
|
if uu == nil { |
||||||
|
return errors.New("invalid UUID format") |
||||||
|
} |
||||||
|
*u = uu |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
// Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package uuid |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"reflect" |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
var testUUID = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479") |
||||||
|
|
||||||
|
func TestJSON(t *testing.T) { |
||||||
|
type S struct { |
||||||
|
ID1 UUID |
||||||
|
ID2 UUID |
||||||
|
} |
||||||
|
s1 := S{ID1: testUUID} |
||||||
|
data, err := json.Marshal(&s1) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
var s2 S |
||||||
|
if err := json.Unmarshal(data, &s2); err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
if !reflect.DeepEqual(&s1, &s2) { |
||||||
|
t.Errorf("got %#v, want %#v", s2, s1) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,66 @@ |
|||||||
|
// Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package uuid |
||||||
|
|
||||||
|
import ( |
||||||
|
"flag" |
||||||
|
"runtime" |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
// This test is only run when --regressions is passed on the go test line.
|
||||||
|
var regressions = flag.Bool("regressions", false, "run uuid regression tests") |
||||||
|
|
||||||
|
// TestClockSeqRace tests for a particular race condition of returning two
|
||||||
|
// identical Version1 UUIDs. The duration of 1 minute was chosen as the race
|
||||||
|
// condition, before being fixed, nearly always occured in under 30 seconds.
|
||||||
|
func TestClockSeqRace(t *testing.T) { |
||||||
|
if !*regressions { |
||||||
|
t.Skip("skipping regression tests") |
||||||
|
} |
||||||
|
duration := time.Minute |
||||||
|
|
||||||
|
done := make(chan struct{}) |
||||||
|
defer close(done) |
||||||
|
|
||||||
|
ch := make(chan UUID, 10000) |
||||||
|
ncpu := runtime.NumCPU() |
||||||
|
switch ncpu { |
||||||
|
case 0, 1: |
||||||
|
// We can't run the test effectively.
|
||||||
|
t.Skip("skipping race test, only one CPU detected") |
||||||
|
return |
||||||
|
default: |
||||||
|
runtime.GOMAXPROCS(ncpu) |
||||||
|
} |
||||||
|
for i := 0; i < ncpu; i++ { |
||||||
|
go func() { |
||||||
|
for { |
||||||
|
select { |
||||||
|
case <-done: |
||||||
|
return |
||||||
|
case ch <- NewUUID(): |
||||||
|
} |
||||||
|
} |
||||||
|
}() |
||||||
|
} |
||||||
|
|
||||||
|
uuids := make(map[string]bool) |
||||||
|
cnt := 0 |
||||||
|
start := time.Now() |
||||||
|
for u := range ch { |
||||||
|
s := u.String() |
||||||
|
if uuids[s] { |
||||||
|
t.Errorf("duplicate uuid after %d in %v: %s", cnt, time.Since(start), s) |
||||||
|
return |
||||||
|
} |
||||||
|
uuids[s] = true |
||||||
|
if time.Since(start) > duration { |
||||||
|
return |
||||||
|
} |
||||||
|
cnt++ |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
// Copyright 2015 Google Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package uuid |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
"fmt" |
||||||
|
) |
||||||
|
|
||||||
|
// Scan implements sql.Scanner so UUIDs can be read from databases transparently
|
||||||
|
// Currently, database types that map to string and []byte are supported. Please
|
||||||
|
// consult database-specific driver documentation for matching types.
|
||||||
|
func (uuid *UUID) Scan(src interface{}) error { |
||||||
|
switch src.(type) { |
||||||
|
case string: |
||||||
|
// see uuid.Parse for required string format
|
||||||
|
parsed := Parse(src.(string)) |
||||||
|
|
||||||
|
if parsed == nil { |
||||||
|
return errors.New("Scan: invalid UUID format") |
||||||
|
} |
||||||
|
|
||||||
|
*uuid = parsed |
||||||
|
case []byte: |
||||||
|
// assumes a simple slice of bytes, just check validity and store
|
||||||
|
u := UUID(src.([]byte)) |
||||||
|
|
||||||
|
if u.Variant() == Invalid { |
||||||
|
return errors.New("Scan: invalid UUID format") |
||||||
|
} |
||||||
|
|
||||||
|
*uuid = u |
||||||
|
default: |
||||||
|
return fmt.Errorf("Scan: unable to scan type %T into UUID", src) |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
// Copyright 2015 Google Inc. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package uuid |
||||||
|
|
||||||
|
import ( |
||||||
|
"strings" |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
func TestScan(t *testing.T) { |
||||||
|
var stringTest string = "f47ac10b-58cc-0372-8567-0e02b2c3d479" |
||||||
|
var byteTest []byte = Parse(stringTest) |
||||||
|
var badTypeTest int = 6 |
||||||
|
var invalidTest string = "f47ac10b-58cc-0372-8567-0e02b2c3d4" |
||||||
|
var invalidByteTest []byte = Parse(invalidTest) |
||||||
|
|
||||||
|
var uuid UUID |
||||||
|
err := (&uuid).Scan(stringTest) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
err = (&uuid).Scan(byteTest) |
||||||
|
if err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
err = (&uuid).Scan(badTypeTest) |
||||||
|
if err == nil { |
||||||
|
t.Error("int correctly parsed and shouldn't have") |
||||||
|
} |
||||||
|
if !strings.Contains(err.Error(), "unable to scan type") { |
||||||
|
t.Error("attempting to parse an int returned an incorrect error message") |
||||||
|
} |
||||||
|
|
||||||
|
err = (&uuid).Scan(invalidTest) |
||||||
|
if err == nil { |
||||||
|
t.Error("invalid uuid was parsed without error") |
||||||
|
} |
||||||
|
if !strings.Contains(err.Error(), "invalid UUID") { |
||||||
|
t.Error("attempting to parse an invalid UUID returned an incorrect error message") |
||||||
|
} |
||||||
|
|
||||||
|
err = (&uuid).Scan(invalidByteTest) |
||||||
|
if err == nil { |
||||||
|
t.Error("invalid byte uuid was parsed without error") |
||||||
|
} |
||||||
|
if !strings.Contains(err.Error(), "invalid UUID") { |
||||||
|
t.Error("attempting to parse an invalid byte UUID returned an incorrect error message") |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue