Handle byte slice in Scan

Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
Émile Ré
2025-12-15 17:38:44 +01:00
parent 2f8aef9927
commit 735d91ec18
2 changed files with 31 additions and 19 deletions

View File

@@ -94,22 +94,28 @@ func (gid GID) Timestamp() time.Time {
// Scan implements the database/sql/driver.Scanner interface // Scan implements the database/sql/driver.Scanner interface
func (gid *GID) Scan(value interface{}) error { func (gid *GID) Scan(value interface{}) error {
var str string
switch v := value.(type) { switch v := value.(type) {
case string: case string:
enc := base64.RawURLEncoding str = v
id, err := enc.DecodeString(v) case []byte:
if err != nil { str = string(v)
return err
}
if len(id) != GIDSize {
return fmt.Errorf("invalid length for GID: got %d, want %d", len(id), GIDSize)
}
copy((*gid)[:], id)
default: default:
return fmt.Errorf("invalid type for GID: expected string, got %T", value) return fmt.Errorf("invalid type %T for GID", value)
} }
enc := base64.RawURLEncoding
id, err := enc.DecodeString(str)
if err != nil {
return err
}
if len(id) != GIDSize {
return fmt.Errorf("invalid length for GID: got %d, want %d", len(id), GIDSize)
}
copy((*gid)[:], id)
return nil return nil
} }

View File

@@ -53,17 +53,23 @@ func (a *Addr) Scan(value any) error {
return nil return nil
} }
var str string
switch v := value.(type) { switch v := value.(type) {
case string: case string:
parsed, err := ParseAddr(v) str = v
if err != nil { case []byte:
return err str = string(v)
}
*a = parsed
default: default:
return fmt.Errorf("invalid type for mail.Addr: expected string, got %T", value) return fmt.Errorf("invalid type %T for mail.Addr", value)
} }
parsed, err := ParseAddr(str)
if err != nil {
return err
}
*a = parsed
return nil return nil
} }