feat: enhance Scan method in RetrieverEngines to support legacy and current formats

- Update Scan method to handle both legacy bare-array format (e.g. [{...}, {...}]) and current object-wrapped format (e.g. {"engines": [{...}, {...}]}).
- Improve error handling for unmarshalling failures, providing clearer feedback on format issues.

This change ensures backward compatibility while allowing for the new data structure.
This commit is contained in:
wizardchen
2026-04-28 16:38:52 +08:00
committed by lyingbug
parent c34f7b6254
commit dd524ffb1c
+16 -2
View File
@@ -165,7 +165,9 @@ func (c RetrieverEngines) Value() (driver.Value, error) {
return json.Marshal(c)
}
// Scan implements the sql.Scanner interface, used to convert database value to RetrieverEngines
// Scan implements the sql.Scanner interface, used to convert database value to RetrieverEngines.
// It supports both the legacy bare-array format (e.g. [{...}, {...}]) and the current
// object-wrapped format (e.g. {"engines": [{...}, {...}]}).
func (c *RetrieverEngines) Scan(value interface{}) error {
if value == nil {
return nil
@@ -174,7 +176,19 @@ func (c *RetrieverEngines) Scan(value interface{}) error {
if !ok {
return nil
}
return json.Unmarshal(b, c)
// Try the current object format first: {"engines": [...]}
if err := json.Unmarshal(b, c); err == nil {
return nil
}
// Fallback: legacy bare-array format: [{...}, {...}]
var engines []RetrieverEngineParams
if err := json.Unmarshal(b, &engines); err != nil {
return fmt.Errorf("retriever_engines: cannot unmarshal as object or array: %w", err)
}
c.Engines = engines
return nil
}
// ConversationConfig represents the conversation configuration for normal mode