-
Notifications
You must be signed in to change notification settings - Fork 0
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
891220f
commit b3c0da4
Showing
5 changed files
with
121 additions
and
2 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
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,72 @@ | ||
package nullish | ||
|
||
import ( | ||
"bytes" | ||
"database/sql/driver" | ||
"errors" | ||
|
||
"github.com/goccy/go-json" | ||
"github.com/oklog/ulid/v2" | ||
) | ||
|
||
type NullULID struct { | ||
ULID ulid.ULID | ||
Valid bool | ||
} | ||
|
||
// Value method | ||
func (nl NullULID) Value() (driver.Value, error) { | ||
|
||
if !nl.Valid { | ||
return nil, nil | ||
} | ||
|
||
return nl.ULID.Value() | ||
} | ||
|
||
// Scan method | ||
func (nl *NullULID) Scan(value interface{}) error { | ||
|
||
if value == nil { | ||
nl.ULID, nl.Valid = ulid.ULID{}, false | ||
return nil | ||
} | ||
|
||
err := nl.ULID.Scan(value) | ||
if err != nil { | ||
nl.ULID, nl.Valid = ulid.ULID{}, false | ||
return errors.New("scan ulid is failed") | ||
|
||
} | ||
|
||
nl.Valid = true | ||
|
||
return nil | ||
} | ||
|
||
// MarshalJSON method | ||
func (nl NullULID) MarshalJSON() ([]byte, error) { | ||
|
||
if !nl.Valid { | ||
return NullType, nil | ||
} | ||
|
||
return json.Marshal(nl.ULID) | ||
} | ||
|
||
// UnmarshalJSON method | ||
func (nl *NullULID) UnmarshalJSON(data []byte) error { | ||
if bytes.Equal(data, NullType) { | ||
*nl = NullULID{} | ||
return nil | ||
} | ||
|
||
err := json.Unmarshal(data, &nl.ULID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nl.Valid = true | ||
|
||
return nil | ||
} |