Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
fix!: use Slice as type
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed May 9, 2023
1 parent 14185cd commit 72f2d2f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
22 changes: 11 additions & 11 deletions jsonarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"gorm.io/gorm/schema"
)

// JSONArray defined JSON data type, need to implements driver.Valuer, sql.Scanner interface
type JSONArray[T any] []T
// Slice defined JSON data type, need to implements driver.Valuer, sql.Scanner interface
type Slice[T any] []T

// Value return json value, implement driver.Valuer interface
func (m JSONArray[T]) Value() (driver.Value, error) {
func (m Slice[T]) Value() (driver.Value, error) {
if m == nil {
return nil, nil
}
Expand All @@ -26,9 +26,9 @@ func (m JSONArray[T]) Value() (driver.Value, error) {
}

// Scan scan value into Jsonb, implements sql.Scanner interface
func (m *JSONArray[T]) Scan(val interface{}) error {
func (m *Slice[T]) Scan(val interface{}) error {
if val == nil {
*m = make(JSONArray[T], 0)
*m = make(Slice[T], 0)
return nil
}
var ba []byte
Expand All @@ -49,7 +49,7 @@ func (m *JSONArray[T]) Scan(val interface{}) error {
}

// MarshalJSON to output non base64 encoded []byte
func (m JSONArray[T]) MarshalJSON() ([]byte, error) {
func (m Slice[T]) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
Expand All @@ -64,25 +64,25 @@ func (m JSONArray[T]) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON to deserialize []byte
func (m *JSONArray[T]) UnmarshalJSON(b []byte) error {
func (m *Slice[T]) UnmarshalJSON(b []byte) error {
t := []T{}

err := json.Unmarshal(b, &t)
if err != nil {
return fmt.Errorf("failed to unmarshal JSONB value: %w", err)
}

*m = JSONArray[T](t)
*m = Slice[T](t)
return nil
}

// GormDataType gorm common data type
func (m JSONArray[T]) GormDataType() string {
func (m Slice[T]) GormDataType() string {
return "jsonarray"
}

// GormDBDataType gorm db data type
func (JSONArray[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
func (Slice[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "JSON"
Expand All @@ -96,7 +96,7 @@ func (JSONArray[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return ""
}

func (m JSONArray[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
func (m Slice[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := m.MarshalJSON()
if db.Dialector.Name() == "mysql" {
if v, ok := db.Dialector.(*mysql.Dialector); ok && !strings.Contains(v.ServerVersion, "MariaDB") {
Expand Down
42 changes: 21 additions & 21 deletions jsonarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,22 @@ func openTestConnection() (db *gorm.DB, err error) {
return
}

func TestJSONArray(t *testing.T) {
type Dummy struct {
Values []string
}

a1 := jsonarray.JSONArray[int]{1, 2, 3}
a1 = append(a1, 4)
assert.Len(t, a1, 4)

a2 := jsonarray.JSONArray[string]{"a", "b", "c"}
a2 = append(a2, "d")
assert.Len(t, a2, 4)

d := Dummy{Values: a2}
assert.Len(t, d.Values, 4)
}

func TestJSONArrayI(t *testing.T) {
func TestSlice(t *testing.T) {
type UserWithJSON struct {
gorm.Model
Name string
Tags jsonarray.JSONArray[int]
Tags jsonarray.Slice[int]
}

DB.Migrator().DropTable(&UserWithJSON{})
assert.NoError(t, DB.Migrator().AutoMigrate(&UserWithJSON{}))

users := []UserWithJSON{{
Name: "json-1",
Tags: jsonarray.JSONArray[int]{1, 2},
Tags: jsonarray.Slice[int]{1, 2},
}, {
Name: "json-2",
Tags: jsonarray.JSONArray[int]([]int{3, 4, 5}),
Tags: jsonarray.Slice[int]([]int{3, 4, 5}),
}}
assert.NoError(t, DB.Create(&users).Error)

Expand All @@ -88,3 +71,20 @@ func TestJSONArrayI(t *testing.T) {
assert.Equal(t, users[0].Name, result.Name)
assert.Equal(t, users[0].Tags[0], result.Tags[0])
}

func TestDummySlice(t *testing.T) {
type Dummy struct {
Values []string
}

a1 := jsonarray.Slice[int]{1, 2, 3}
a1 = append(a1, 4)
assert.Len(t, a1, 4)

a2 := jsonarray.Slice[string]{"a", "b", "c"}
a2 = append(a2, "d")
assert.Len(t, a2, 4)

d := Dummy{Values: a2}
assert.Len(t, d.Values, 4)
}

0 comments on commit 72f2d2f

Please sign in to comment.