Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(null): added nullable Null for sql/json
Browse files Browse the repository at this point in the history
cnlangzi committed Jun 12, 2024
1 parent 7be2a78 commit 1e5d269
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sqle

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

var nullJsonBytes = []byte("null")

const nullJson = "null"

type Null[T any] struct {
sql.Null[T]
}

func NewNull[T any](v T, valid bool) Null[T] {
return Null[T]{Null: sql.Null[T]{V: v, Valid: valid}}
}

// Scan implements the [sql.Scanner] interface.
func (t *Null[T]) Scan(value any) error { // skipcq: GO-W1029
return t.Null.Scan(value)
}

// Value implements the [driver.Valuer] interface.
func (t Null[T]) Value() (driver.Value, error) { // skipcq: GO-W1029
return t.Null.Value()
}

// TValue returns the underlying value of the Null struct.
func (t *Null[T]) TValue() T { // skipcq: GO-W1029
return t.Null.V
}

// MarshalJSON implements the json.Marshaler interface
func (t Null[T]) MarshalJSON() ([]byte, error) { // skipcq: GO-W1029
if t.Valid {
return json.Marshal(t.Null.V)
}
return nullJsonBytes, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (t *Null[T]) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029
if len(data) == 0 || string(data) == nullJson {
t.Null.Valid = false
return nil
}

var v T
err := json.Unmarshal(data, &v)
if err != nil {
return err
}

t.Null.V = v
t.Null.Valid = true

return nil
}

0 comments on commit 1e5d269

Please sign in to comment.