Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scalar/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (s *Binary) Set(val any) error {
}

switch value := val.(type) {
case *[]byte:
if value == nil {
s.Valid = false
return nil
}
return s.Set(*value)
case []byte:
if value == nil {
s.Valid = false
Expand Down
5 changes: 5 additions & 0 deletions scalar/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package scalar
import "testing"

func TestBinarySet(t *testing.T) {
var nilPointerByteArray *[]byte
var nilPointerString *string

successfulTests := []struct {
source any
result Binary
Expand All @@ -12,6 +15,8 @@ func TestBinarySet(t *testing.T) {
{source: []byte(nil), result: Binary{}},
{source: _byteSlice{1, 2, 3}, result: Binary{Value: []byte{1, 2, 3}, Valid: true}},
{source: _byteSlice(nil), result: Binary{}},
{source: nilPointerByteArray, result: Binary{}},
{source: nilPointerString, result: Binary{}},
}

for i, tt := range successfulTests {
Expand Down
5 changes: 5 additions & 0 deletions scalar/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

func TestBoolSet(t *testing.T) {
var nilPointerBool *bool
var nilPointerString *string

successfulTests := []struct {
source any
result Bool
Expand All @@ -19,6 +22,8 @@ func TestBoolSet(t *testing.T) {
{source: _bool(false), result: Bool{Value: false, Valid: true}},
{source: &Bool{Value: true, Valid: true}, result: Bool{Value: true, Valid: true}},
{source: nil, result: Bool{}},
{source: nilPointerBool, result: Bool{}},
{source: nilPointerString, result: Bool{}},
}

for i, tt := range successfulTests {
Expand Down
33 changes: 32 additions & 1 deletion scalar/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFloat32Set(t *testing.T) {
var nilPointerInt8 *int8
var nilPointerInt16 *int16
var nilPointerInt32 *int32
var nilPointerInt64 *int64
var nilPointerUint8 *uint8
var nilPointerUint16 *uint16
var nilPointerUint32 *uint32
var nilPointerUint64 *uint64
var nilPointerFloat32 *float32
var nilPointerFloat64 *float64

func TestFloatAllWidthsSet(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed TestFloat32Set to TestFloatAllWidthsSet

successfulTests := []struct {
source any
expect Float
Expand All @@ -30,6 +41,16 @@ func TestFloat32Set(t *testing.T) {
{source: "1", expect: Float{Value: 1, Valid: true}},
{source: _int8(1), expect: Float{Value: 1, Valid: true}},
{source: &Float{Value: 1, Valid: true, BitWidth: 32}, expect: Float{Value: 1, Valid: true}},
{source: nilPointerInt8, expect: Float{Valid: false}},
{source: nilPointerInt16, expect: Float{Valid: false}},
{source: nilPointerInt32, expect: Float{Valid: false}},
{source: nilPointerInt64, expect: Float{Valid: false}},
{source: nilPointerUint8, expect: Float{Valid: false}},
{source: nilPointerUint16, expect: Float{Valid: false}},
{source: nilPointerUint32, expect: Float{Valid: false}},
{source: nilPointerUint64, expect: Float{Valid: false}},
{source: nilPointerFloat32, expect: Float{Valid: false}},
{source: nilPointerFloat64, expect: Float{Valid: false}},
}

for _, bitWidth := range []uint8{8, 16, 32, 64} {
Expand Down Expand Up @@ -74,6 +95,16 @@ func TestFloat64Set(t *testing.T) {
{source: "1", result: Float{Value: 1, Valid: true}},
{source: _int8(1), result: Float{Value: 1, Valid: true}},
{source: &Float{Value: 1, Valid: true}, result: Float{Value: 1, Valid: true}},
{source: nilPointerInt8, result: Float{Valid: false}},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can remove TestFloat64Set altogether due to TestFloatAllWidthsSet

{source: nilPointerInt16, result: Float{Valid: false}},
{source: nilPointerInt32, result: Float{Valid: false}},
{source: nilPointerInt64, result: Float{Valid: false}},
{source: nilPointerUint8, result: Float{Valid: false}},
{source: nilPointerUint16, result: Float{Valid: false}},
{source: nilPointerUint32, result: Float{Valid: false}},
{source: nilPointerUint64, result: Float{Valid: false}},
{source: nilPointerFloat32, result: Float{Valid: false}},
{source: nilPointerFloat64, result: Float{Valid: false}},
}

for i, tt := range successfulTests {
Expand Down
8 changes: 8 additions & 0 deletions scalar/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ func (s *UUID) Set(src any) error {
return s.Set(value2)
case [16]byte:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if uuid.UUID should also be checked?...

Copy link
Member Author

@erezrokah erezrokah Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works since uuid.UUID implements the Stringer interface

s.Value = uuid.UUID(value)
case *[]byte:
if value == nil {
s.Valid = false
return nil
}
return s.Set(*value)
case []byte:
if value == nil {
s.Valid = false
return nil
}
if len(value) != 16 {
Expand All @@ -79,6 +86,7 @@ func (s *UUID) Set(src any) error {
s.Value = uuidVal
case *string:
if value == nil {
s.Valid = false
return nil
}
return s.Set(*value)
Expand Down
5 changes: 5 additions & 0 deletions scalar/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func (s StringUUIDType) String() string {
}

func TestUUIDSet(t *testing.T) {
var nilPointerByteArray *[]byte
var nilPointerString *string

successfulTests := []struct {
source any
result UUID
Expand Down Expand Up @@ -54,6 +57,8 @@ func TestUUIDSet(t *testing.T) {
source: &UUID{Value: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
result: UUID{Value: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
{source: nilPointerByteArray, result: UUID{}},
{source: nilPointerString, result: UUID{}},
}

for i, tt := range successfulTests {
Expand Down