From 72f2d2fd34b2348282d9a1cdabfe356924c933cc Mon Sep 17 00:00:00 2001 From: Ivan Stasiuk Date: Tue, 9 May 2023 16:02:38 +0100 Subject: [PATCH] fix!: use `Slice` as type --- jsonarray.go | 22 +++++++++++----------- jsonarray_test.go | 42 +++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/jsonarray.go b/jsonarray.go index a8b8ae3..a83bf9e 100644 --- a/jsonarray.go +++ b/jsonarray.go @@ -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 } @@ -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 @@ -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 } @@ -64,7 +64,7 @@ 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) @@ -72,17 +72,17 @@ func (m *JSONArray[T]) UnmarshalJSON(b []byte) error { 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" @@ -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") { diff --git a/jsonarray_test.go b/jsonarray_test.go index 72eb510..ca4699e 100644 --- a/jsonarray_test.go +++ b/jsonarray_test.go @@ -47,28 +47,11 @@ 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{}) @@ -76,10 +59,10 @@ func TestJSONArrayI(t *testing.T) { 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) @@ -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) +}