Skip to content

Commit

Permalink
Fix json array scan issue
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Aug 4, 2024
1 parent d7b79f1 commit 6dde22e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions json_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (f JSONArray[T]) String() string {
func (f *JSONArray[T]) SetValue(value any) error {
switch vl := value.(type) {
case []T:
*f = vl
*f = append(*f, vl...)
case *[]T:
*f = *vl
*f = append(*f, *vl...)
case nil:
return ErrNullValueNotAllowed
default:
Expand Down Expand Up @@ -101,7 +101,7 @@ func (f *JSONArray[T]) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*f = (*f)[:0]
*f = append((*f)[:0], res...)
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions nullable_json_arrray.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gosql

import "database/sql/driver"
import (
"database/sql/driver"
)

// NullableJSONArray object
type NullableJSONArray[T any] JSONArray[T]
Expand Down Expand Up @@ -43,7 +45,7 @@ func (f NullableJSONArray[T]) Value() (driver.Value, error) {
}

// Scan value from database
func (f *NullableJSONArray[T]) Scan(value interface{}) error {
func (f *NullableJSONArray[T]) Scan(value any) error {
if value == nil {
*f = nil
return nil
Expand Down
7 changes: 7 additions & 0 deletions nullable_json_arrray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ func TestNullableJSONArray(t *testing.T) {
v, _ := js.Value()
assert.Nil(t, v)
})

t.Run("scan", func(t *testing.T) {
var js NullableJSONArray[int]
if assert.NoError(t, js.Scan([]byte("[1,2,3]"))) {
assert.ElementsMatch(t, []int{1, 2, 3}, js)
}
})
}

0 comments on commit 6dde22e

Please sign in to comment.