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:
@@ -165,7 +165,9 @@ func (c RetrieverEngines) Value() (driver.Value, error) {
|
|||||||
return json.Marshal(c)
|
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 {
|
func (c *RetrieverEngines) Scan(value interface{}) error {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -174,7 +176,19 @@ func (c *RetrieverEngines) Scan(value interface{}) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
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
|
// ConversationConfig represents the conversation configuration for normal mode
|
||||||
|
|||||||
Reference in New Issue
Block a user