-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UUID: compatibiltiy for SQL serialization
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package uuid | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
) | ||
|
||
var _ driver.Valuer = UUID("") | ||
var _ sql.Scanner = (*UUID)(nil) | ||
|
||
// Value implements the driver.Valuer interface. | ||
func (u UUID) Value() (driver.Value, error) { | ||
return u.String(), nil | ||
} | ||
|
||
// Scan implements the sql.Scanner interface. | ||
// A 16-byte slice will be handled by UnmarshalBinary, while | ||
// a longer byte slice or a string will be handled by UnmarshalText. | ||
func (u *UUID) Scan(src interface{}) error { | ||
switch src := src.(type) { | ||
case UUID: | ||
*u = src | ||
return nil | ||
case []byte: | ||
if len(src) == Size { | ||
return u.UnmarshalBinary(src) | ||
} | ||
return u.UnmarshalText(src) | ||
case string: | ||
uu, err := Parse(src) | ||
*u = uu | ||
return err | ||
} | ||
|
||
return fmt.Errorf("cannot convert %T to UUID", src) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package uuid_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/invopop/gobl/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValue(t *testing.T) { | ||
u := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") | ||
v, err := u.Value() | ||
require.NoError(t, err) | ||
assert.Equal(t, u.String(), v) | ||
} | ||
|
||
func TestScan(t *testing.T) { | ||
u := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") | ||
t.Run("with UUID", func(t *testing.T) { | ||
var uu uuid.UUID | ||
err := uu.Scan(u) | ||
require.NoError(t, err) | ||
assert.Equal(t, u, uu) | ||
}) | ||
t.Run("with string", func(t *testing.T) { | ||
var uu uuid.UUID | ||
err := uu.Scan(u.String()) | ||
require.NoError(t, err) | ||
assert.Equal(t, u, uu) | ||
}) | ||
t.Run("with []byte text", func(t *testing.T) { | ||
var uu uuid.UUID | ||
err := uu.Scan([]byte(u.String())) | ||
require.NoError(t, err) | ||
assert.Equal(t, u, uu) | ||
}) | ||
t.Run("with bytes", func(t *testing.T) { | ||
var uu uuid.UUID | ||
err := uu.Scan(u.Bytes()) | ||
require.NoError(t, err) | ||
assert.Equal(t, u, uu) | ||
}) | ||
|
||
t.Run("with int", func(t *testing.T) { | ||
var uu uuid.UUID | ||
err := uu.Scan(42) | ||
require.ErrorContains(t, err, "cannot convert int to UUI") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters