From 7464a2351d307dba69271f2ba792779cb715f087 Mon Sep 17 00:00:00 2001 From: Justin Overfelt Date: Sat, 4 Jul 2015 13:42:16 -0400 Subject: [PATCH] Implement database/sql's Scanner interface. Fixes #5 --- sql.go | 40 ++++++++++++++++++++++++++++++++++++++++ sql_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 sql.go create mode 100644 sql_test.go diff --git a/sql.go b/sql.go new file mode 100644 index 0000000..2d7679e --- /dev/null +++ b/sql.go @@ -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 +} diff --git a/sql_test.go b/sql_test.go new file mode 100644 index 0000000..d643567 --- /dev/null +++ b/sql_test.go @@ -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") + } +}