Skip to content

Commit

Permalink
feat(null): added nullable Null for sql/json
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Jun 12, 2024
1 parent 1e5d269 commit f04c56e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.5.1]
- !fix(orderby): use BuildOption instead of allowedColumns (#46)
- feat(string): added nullable String for sql/json (#47)
- feat(string): added nullable String/Null for sql/json (#47)

## [1.5.0] - 2024-04-30
### Changed
Expand Down
19 changes: 9 additions & 10 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
package sqle

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

type String struct {
sql.NullString
Null[string]
}

func NewString(s string) String {
return String{NullString: sql.NullString{String: s, Valid: true}}
return String{Null: NewNull(s, true)}
}

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

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

// Time returns the underlying time.Time value of the Time struct.
func (t *String) String() string { // skipcq: GO-W1029
return t.NullString.String
return t.TValue()
}

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

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

Expand All @@ -50,8 +49,8 @@ func (t *String) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029
return err

Check warning on line 49 in string.go

View check run for this annotation

Codecov / codecov/patch

string.go#L49

Added line #L49 was not covered by tests
}

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

return nil
}

0 comments on commit f04c56e

Please sign in to comment.