From 1d862c01d558d8fb1c96c401c92321c038e49991 Mon Sep 17 00:00:00 2001 From: Ruoxi Wang Date: Fri, 29 Mar 2024 22:52:14 +0800 Subject: [PATCH] Implement custom Value and Scan methods for PrivateString type (#231) --- internal/domain/base.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/domain/base.go b/internal/domain/base.go index e5de0b92..a58ddb29 100644 --- a/internal/domain/base.go +++ b/internal/domain/base.go @@ -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"