refactor(model): support string type in JSON scan methods
All checks were successful
Build Backend / build (push) Successful in 5m57s
Build Backend / build-docker (push) Successful in 1m16s

Enhance Scan methods in StringArray, ExtraData, MessageSegments, and
SystemNotificationExtra to accept both []byte and string types from database.
Uses type switch for more flexible type handling.
This commit is contained in:
lafay
2026-04-24 15:58:28 +08:00
parent 813e54e5b2
commit 496c8bb1aa
3 changed files with 35 additions and 15 deletions

View File

@@ -71,11 +71,16 @@ func (a *StringArray) Scan(value any) error {
*a = nil *a = nil
return nil return nil
} }
bytes, ok := value.([]byte) var data []byte
if !ok { switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
default:
return nil return nil
} }
return json.Unmarshal(bytes, a) return json.Unmarshal(data, a)
} }
// MaterialFile 文件资料 // MaterialFile 文件资料

View File

@@ -101,11 +101,16 @@ func (e *ExtraData) Scan(value any) error {
if value == nil { if value == nil {
return nil return nil
} }
bytes, ok := value.([]byte) var data []byte
if !ok { switch v := value.(type) {
return errors.New("type assertion to []byte failed") case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
} }
return json.Unmarshal(bytes, e) return json.Unmarshal(data, e)
} }
// MessageSegmentData 单个消息段的数据 // MessageSegmentData 单个消息段的数据
@@ -131,11 +136,16 @@ func (s *MessageSegments) Scan(value any) error {
*s = nil *s = nil
return nil return nil
} }
bytes, ok := value.([]byte) var data []byte
if !ok { switch v := value.(type) {
return errors.New("type assertion to []byte failed") case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
} }
return json.Unmarshal(bytes, s) return json.Unmarshal(data, s)
} }
// Message 消息实体 // Message 消息实体

View File

@@ -79,11 +79,16 @@ func (e *SystemNotificationExtra) Scan(value any) error {
if value == nil { if value == nil {
return nil return nil
} }
bytes, ok := value.([]byte) var data []byte
if !ok { switch v := value.(type) {
return errors.New("type assertion to []byte failed") case []byte:
data = v
case string:
data = []byte(v)
default:
return errors.New("type assertion to []byte or string failed")
} }
return json.Unmarshal(bytes, e) return json.Unmarshal(data, e)
} }
// SystemNotification 系统通知(独立表,与消息完全分离) // SystemNotification 系统通知(独立表,与消息完全分离)