go-uuid: Fix JSON encoding for empty UUIDs
diff --git a/uuid/json.go b/uuid/json.go
index 4a77fa5..760580a 100644
--- a/uuid/json.go
+++ b/uuid/json.go
@@ -7,10 +7,16 @@
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")
}
diff --git a/uuid/json_test.go b/uuid/json_test.go
index 691e935..b5eae09 100644
--- a/uuid/json_test.go
+++ b/uuid/json_test.go
@@ -6,6 +6,7 @@
import (
"encoding/json"
+ "reflect"
"testing"
)
@@ -13,9 +14,10 @@
func TestJSON(t *testing.T) {
type S struct {
- ID UUID
+ ID1 UUID
+ ID2 UUID
}
- s1 := S{testUUID}
+ s1 := S{ID1: testUUID}
data, err := json.Marshal(&s1)
if err != nil {
t.Fatal(err)
@@ -24,7 +26,7 @@
if err := json.Unmarshal(data, &s2); err != nil {
t.Fatal(err)
}
- if !Equal(s1.ID, s2.ID) {
- t.Errorf("got UUID %v, want %v", s2.ID, s1.ID)
+ if !reflect.DeepEqual(&s1, &s2) {
+ t.Errorf("got %#v, want %#v", s2, s1)
}
}
diff --git a/uuid/seq_test.go b/uuid/seq_test.go
index 5645aa7..3b3d143 100644
--- a/uuid/seq_test.go
+++ b/uuid/seq_test.go
@@ -48,16 +48,16 @@
}()
}
- uuid_map := make(map[string]bool)
+ uuids := make(map[string]bool)
cnt := 0
start := time.Now()
for u := range ch {
s := u.String()
- if uuid_map[s] {
+ if uuids[s] {
t.Errorf("duplicate uuid after %d in %v: %s", cnt, time.Since(start), s)
return
}
- uuid_map[s] = true
+ uuids[s] = true
if time.Since(start) > duration {
return
}
diff --git a/uuid/time.go b/uuid/time.go
index d92f844..7ebc9be 100755
--- a/uuid/time.go
+++ b/uuid/time.go
@@ -40,8 +40,8 @@
}
// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and
-// adjusts the clock sequence as needed. An error is returned if the current
-// time cannot be determined.
+// clock sequence as well as adjusting the clock sequence as needed. An error
+// is returned if the current time cannot be determined.
func GetTime() (Time, uint16, error) {
defer mu.Unlock()
mu.Lock()