mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
128 lines
3.3 KiB
Go
128 lines
3.3 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.
|
|
//
|
|
// Generic SQL Scanner / driver.Valuer for slice and map types stored as
|
|
// JSON in TEXT columns. Required because Go's database/sql can't scan a
|
|
// string straight into []string / map[string]any without the destination
|
|
// type implementing the Scanner interface.
|
|
|
|
package object
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// StringSlice is []string with JSON marshalling for SQL TEXT columns.
|
|
type StringSlice []string
|
|
|
|
func (s *StringSlice) Scan(src interface{}) error {
|
|
if src == nil {
|
|
*s = nil
|
|
return nil
|
|
}
|
|
var data []byte
|
|
switch v := src.(type) {
|
|
case string:
|
|
if v == "" {
|
|
*s = nil
|
|
return nil
|
|
}
|
|
data = []byte(v)
|
|
case []byte:
|
|
if len(v) == 0 {
|
|
*s = nil
|
|
return nil
|
|
}
|
|
data = v
|
|
default:
|
|
return fmt.Errorf("StringSlice.Scan: unsupported type %T", src)
|
|
}
|
|
return json.Unmarshal(data, s)
|
|
}
|
|
|
|
func (s StringSlice) Value() (driver.Value, error) {
|
|
if s == nil {
|
|
return "[]", nil
|
|
}
|
|
b, err := json.Marshal(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// JSONScan is a helper for fields that are scanned-from-JSON-text via a
|
|
// pointer-receiver Scan method. Use it inside per-type Scanner impls so
|
|
// they all share the same string/bytes/nil handling.
|
|
func JSONScan(dst interface{}, src interface{}) error {
|
|
if src == nil {
|
|
return nil
|
|
}
|
|
var data []byte
|
|
switch v := src.(type) {
|
|
case string:
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
data = []byte(v)
|
|
case []byte:
|
|
if len(v) == 0 {
|
|
return nil
|
|
}
|
|
data = v
|
|
default:
|
|
return fmt.Errorf("JSONScan: unsupported type %T", src)
|
|
}
|
|
return json.Unmarshal(data, dst)
|
|
}
|
|
|
|
// JSONValue marshals v as a JSON string for driver.Valuer impls.
|
|
func JSONValue(v interface{}) (driver.Value, error) {
|
|
if v == nil {
|
|
return "null", nil
|
|
}
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// ExampleQuestionList implements sql.Scanner for slice-of-ExampleQuestion,
|
|
// stored as JSON in a TEXT column.
|
|
type ExampleQuestionList []ExampleQuestion
|
|
|
|
func (l *ExampleQuestionList) Scan(src interface{}) error { return JSONScan(l, src) }
|
|
func (l ExampleQuestionList) Value() (driver.Value, error) { return JSONValue(l) }
|
|
|
|
// PropertiesMap implements sql.Scanner for map[string]*Properties.
|
|
type PropertiesMapJSON map[string]*Properties
|
|
|
|
func (m *PropertiesMapJSON) Scan(src interface{}) error { return JSONScan(m, src) }
|
|
func (m PropertiesMapJSON) Value() (driver.Value, error) { return JSONValue(m) }
|
|
|
|
// Scan on *TreeFile so Store.FileTree round-trips JSON<->struct.
|
|
func (t *TreeFile) Scan(src interface{}) error { return JSONScan(t, src) }
|
|
|
|
func (t *TreeFile) Value() (driver.Value, error) {
|
|
if t == nil {
|
|
return "null", nil
|
|
}
|
|
return JSONValue(t)
|
|
}
|