-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e367a3c
commit b1015ca
Showing
12 changed files
with
595 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ go.work.sum | |
|
||
# env file | ||
.env | ||
|
||
# Vscode | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package enum | ||
|
||
import "database/sql/driver" | ||
|
||
// Nullable allows handling nullable enums in both JSON and SQL. | ||
type Nullable[Enum any] struct { | ||
Enum Enum | ||
Valid bool | ||
} | ||
|
||
func (e Nullable[Enum]) MarshalJSON() ([]byte, error) { | ||
if !e.Valid { | ||
return []byte("null"), nil | ||
} | ||
|
||
return MarshalJSON(e.Enum) | ||
} | ||
|
||
func (e *Nullable[Enum]) UnmarshalJSON(data []byte) error { | ||
if string(data) == "null" { | ||
var defaultEnum Enum | ||
e.Enum, e.Valid = defaultEnum, false | ||
return nil | ||
} | ||
|
||
return UnmarshalJSON(data, &e.Enum) | ||
} | ||
|
||
func (e Nullable[Enum]) Value() (driver.Value, error) { | ||
if !e.Valid { | ||
return nil, nil | ||
} | ||
|
||
return ValueSQL(e.Enum) | ||
} | ||
|
||
func (e *Nullable[Enum]) Scan(a any) error { | ||
if a == nil { | ||
var defaultEnum Enum | ||
e.Enum, e.Valid = defaultEnum, false | ||
return nil | ||
} | ||
|
||
e.Valid = true | ||
return ScanSQL(a, &e.Enum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.