Skip to content
Open
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
10 changes: 10 additions & 0 deletions json_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func NewJSONSlice[T any](s []T) JSONSlice[T] {

// Value return json value, implement driver.Valuer interface
func (j JSONSlice[T]) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
data, err := json.Marshal(j)
if err != nil {
return nil, err
Expand All @@ -112,6 +115,10 @@ func (j JSONSlice[T]) Value() (driver.Value, error) {

// Scan scan value into JSONType[T], implements sql.Scanner interface
func (j *JSONSlice[T]) Scan(value interface{}) error {
if value == nil {
*j = nil
return nil
}
var bytes []byte
switch v := value.(type) {
case []byte:
Expand Down Expand Up @@ -143,6 +150,9 @@ func (JSONSlice[T]) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

func (j JSONSlice[T]) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
if j == nil {
return gorm.Expr("NULL")
}
data, _ := json.Marshal(j)

switch db.Dialector.Name() {
Expand Down
Loading