Skip to content

Commit

Permalink
Implement custom Value and Scan methods for PrivateString type (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeseWang authored Mar 29, 2024
1 parent 38310d6 commit 1d862c0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/domain/base.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package domain

import (
"errors"
"time"

"database/sql/driver"
)

type BaseModel struct {
Expand All @@ -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"
Expand Down

0 comments on commit 1d862c0

Please sign in to comment.