mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// SQL Scanner / driver.Valuer for the McpTools slice type so it round-trips
|
|
// through database/sql columns (TEXT-typed) as JSON.
|
|
//
|
|
// Without this, `Provider.McpTools []*McpTools` panics at startup with:
|
|
//
|
|
// sql: Scan error on column index 15, name "mcp_tools": unsupported Scan,
|
|
// storing driver.Value type string into type *[]*agent.McpTools
|
|
//
|
|
// xorm/dbx call Scan(stringValue) for TEXT columns; Go's database/sql can't
|
|
// directly populate a slice from a string, so we marshal/unmarshal JSON.
|
|
|
|
package agent
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// McpToolsList is a typed alias for []*McpTools that implements
|
|
// sql.Scanner + driver.Valuer. Use it in DB-mapped structs.
|
|
type McpToolsList []*McpTools
|
|
|
|
// Scan implements sql.Scanner for the slice. Accepts string, []byte, or nil.
|
|
func (l *McpToolsList) Scan(src any) error {
|
|
if src == nil {
|
|
*l = nil
|
|
return nil
|
|
}
|
|
var data []byte
|
|
switch v := src.(type) {
|
|
case string:
|
|
if v == "" {
|
|
*l = nil
|
|
return nil
|
|
}
|
|
data = []byte(v)
|
|
case []byte:
|
|
if len(v) == 0 {
|
|
*l = nil
|
|
return nil
|
|
}
|
|
data = v
|
|
default:
|
|
return fmt.Errorf("McpToolsList.Scan: unsupported type %T", src)
|
|
}
|
|
return json.Unmarshal(data, l)
|
|
}
|
|
|
|
// Value implements driver.Valuer. Returns JSON string (empty list as `[]`).
|
|
func (l McpToolsList) Value() (driver.Value, error) {
|
|
if l == nil {
|
|
return "[]", nil
|
|
}
|
|
b, err := json.Marshal(l)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return string(b), nil
|
|
}
|