Implement custom Value and Scan methods for PrivateString type (#231)

This commit is contained in:
Ruoxi Wang 2024-03-29 22:52:14 +08:00 committed by GitHub
parent 38310d6ff2
commit 1d862c01d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,10 @@
package domain
import (
"errors"
"time"
"database/sql/driver"
)
type BaseModel struct {
@ -21,6 +24,26 @@ func (PrivateString) String() string {
return ""
}
func (ps PrivateString) Value() (driver.Value, error) {
if len(ps) == 0 {
return nil, nil
}
return string(ps), nil
}
func (ps *PrivateString) Scan(value interface{}) error {
if value == nil {
*ps = ""
return nil
}
strValue, ok := value.(string)
if !ok {
return errors.New("invalid type for PrivateString")
}
*ps = PrivateString(strValue)
return nil
}
const (
DisabledReasonExpired = "expired"
DisabledReasonDeleted = "deleted"