Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add support for time.Time #20

Merged
merged 2 commits into from
Feb 15, 2024
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
7 changes: 7 additions & 0 deletions gonull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"reflect"
"time"
)

var (
Expand Down Expand Up @@ -114,6 +115,12 @@ func convertToDriverValue(v any) (driver.Value, error) {
case reflect.String:
return rv.String(), nil

case reflect.Struct:
if t, ok := v.(time.Time); ok {
return t, nil
}
return nil, fmt.Errorf("unsupported struct type: %s", rv.Type())

default:
return nil, fmt.Errorf("unsupported type: %T", v)
}
Expand Down
13 changes: 13 additions & 0 deletions gonull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -344,6 +345,10 @@ func TestConvertToTypeWithNilValue(t *testing.T) {
name: "Nil to string",
expected: "",
},
{
name: "Nil to time.Time",
expected: time.Time{},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -380,6 +385,8 @@ func TestConvertToTypeWithNilValue(t *testing.T) {
result, err = convertToType[bool](nil)
case string:
result, err = convertToType[string](nil)
case time.Time:
result, err = convertToType[time.Time](nil)
}

assert.NoError(t, err)
Expand Down Expand Up @@ -511,6 +518,8 @@ type customValuer struct {
err error
}

type unknowType interface{}

func (cv customValuer) Value() (driver.Value, error) {
return cv.value, cv.err
}
Expand All @@ -531,11 +540,13 @@ func TestConvertToDriverValue(t *testing.T) {
float64Val float64 = 123.456
boolVal bool = true
stringVal string = "test"
timeVal time.Time = time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)
byteSlice []byte = []byte("byte slice")
ptrToInt *int = &intVal
nilPtr *int = nil
valuerSuccess customValuer = customValuer{value: "valuer value", err: nil}
valuerError customValuer = customValuer{err: errors.New("valuer error")}
unknowTypeError unknowType = map[bool]bool{}
unsupportedSlice = []int{1, 2, 3}
)

Expand All @@ -560,12 +571,14 @@ func TestConvertToDriverValue(t *testing.T) {
{"Bool", boolVal, boolVal, false},
{"String", stringVal, stringVal, false},
{"ByteSlice", byteSlice, byteSlice, false},
{"Time", timeVal, timeVal, false},
{"PointerToInt", ptrToInt, int64(*ptrToInt), false},
{"NilPointer", nilPtr, nil, false},
{"UnsupportedType", struct{}{}, nil, true},
{"Uint64HighBitSet", uint64(1 << 63), nil, true}, // Uint64 with high bit set
{"ValuerInterfaceSuccess", valuerSuccess, "valuer value", false},
{"ValuerInterfaceError", valuerError, nil, true},
{"UnknowTypeError", unknowTypeError, nil, true},
{"UnsupportedSliceType", unsupportedSlice, nil, true},
}

Expand Down
Loading