From 473f3b6f3e698f4cd00ca657d26450ea9566df3a Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Fri, 8 Aug 2025 04:09:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(model):=20replace?= =?UTF-8?q?=20gorm.io/datatypes=20with=20JSONValue=20for=20PrefillGroup.It?= =?UTF-8?q?ems;=20fix=20JSON=20scan=20across=20drivers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Why: - Avoid introducing `gorm.io/datatypes` for a single field. - Align with existing pattern (`ChannelInfo`, `Properties`) using `Scanner`/`Valuer`. - Fix runtime error when drivers return JSON as string. - What: - Introduced `JSONValue` (based on `json.RawMessage`) implementing `sql.Scanner` and `driver.Valuer`, with `MarshalJSON`/`UnmarshalJSON` to preserve raw JSON in API. - Updated `PrefillGroup.Items` to use `JSONValue` with `gorm:"type:json"`. - Localized comments in `model/prefill_group.go` to Chinese. - Impact: - Resolves “unsupported Scan, storing driver.Value type string into type *json.RawMessage”. - Works with MySQL/Postgres/SQLite whether JSON is returned as `[]byte` or `string`. - API and DB schema remain unchanged; no `go.mod` changes; lints pass. Files changed: - model/prefill_group.go --- model/prefill_group.go | 60 ++++++++++++++++++- web/src/components/common/ui/JSONEditor.js | 2 +- .../channels/modals/EditChannelModal.jsx | 18 +----- .../table/models/modals/EditModelModal.jsx | 2 +- 4 files changed, 64 insertions(+), 18 deletions(-) diff --git a/model/prefill_group.go b/model/prefill_group.go index 5db78cb2..d9e92faa 100644 --- a/model/prefill_group.go +++ b/model/prefill_group.go @@ -2,6 +2,7 @@ package model import ( "encoding/json" + "database/sql/driver" "one-api/common" "gorm.io/gorm" @@ -14,11 +15,68 @@ import ( // ["gpt-4o", "gpt-3.5-turbo"] // 设计遵循 3NF,避免冗余,提供灵活扩展能力。 +// JSONValue 基于 json.RawMessage 实现,支持从数据库的 []byte 和 string 两种类型读取 +type JSONValue json.RawMessage + +// Value 实现 driver.Valuer 接口,用于数据库写入 +func (j JSONValue) Value() (driver.Value, error) { + if j == nil { + return nil, nil + } + return []byte(j), nil +} + +// Scan 实现 sql.Scanner 接口,兼容不同驱动返回的类型 +func (j *JSONValue) Scan(value interface{}) error { + switch v := value.(type) { + case nil: + *j = nil + return nil + case []byte: + // 拷贝底层字节,避免保留底层缓冲区 + b := make([]byte, len(v)) + copy(b, v) + *j = JSONValue(b) + return nil + case string: + *j = JSONValue([]byte(v)) + return nil + default: + // 其他类型尝试序列化为 JSON + b, err := json.Marshal(v) + if err != nil { + return err + } + *j = JSONValue(b) + return nil + } +} + +// MarshalJSON 确保在对外编码时与 json.RawMessage 行为一致 +func (j JSONValue) MarshalJSON() ([]byte, error) { + if j == nil { + return []byte("null"), nil + } + return j, nil +} + +// UnmarshalJSON 确保在对外解码时与 json.RawMessage 行为一致 +func (j *JSONValue) UnmarshalJSON(data []byte) error { + if data == nil { + *j = nil + return nil + } + b := make([]byte, len(data)) + copy(b, data) + *j = JSONValue(b) + return nil +} + type PrefillGroup struct { Id int `json:"id"` Name string `json:"name" gorm:"size:64;not null;uniqueIndex:uk_prefill_name,where:deleted_at IS NULL"` Type string `json:"type" gorm:"size:32;index;not null"` - Items json.RawMessage `json:"items" gorm:"type:json"` + Items JSONValue `json:"items" gorm:"type:json"` Description string `json:"description,omitempty" gorm:"type:varchar(255)"` CreatedTime int64 `json:"created_time" gorm:"bigint"` UpdatedTime int64 `json:"updated_time" gorm:"bigint"` diff --git a/web/src/components/common/ui/JSONEditor.js b/web/src/components/common/ui/JSONEditor.js index bc256970..e5ecd0e1 100644 --- a/web/src/components/common/ui/JSONEditor.js +++ b/web/src/components/common/ui/JSONEditor.js @@ -637,7 +637,7 @@ const JSONEditor = ({ {/* 额外文本显示在卡片底部 */} {extraText && ( - {extraText} + {extraText} )} {extraFooter && ( diff --git a/web/src/components/table/channels/modals/EditChannelModal.jsx b/web/src/components/table/channels/modals/EditChannelModal.jsx index 259553d0..a971b094 100644 --- a/web/src/components/table/channels/modals/EditChannelModal.jsx +++ b/web/src/components/table/channels/modals/EditChannelModal.jsx @@ -1247,11 +1247,7 @@ const EditChannelModal = (props) => { templateLabel={t('填入模板')} editorType="region" formApi={formApiRef.current} - extraText={ - - {t('设置默认地区和特定模型的专用地区')} - - } + extraText={t('设置默认地区和特定模型的专用地区')} /> )} @@ -1520,11 +1516,7 @@ const EditChannelModal = (props) => { templateLabel={t('填入模板')} editorType="keyValue" formApi={formApiRef.current} - extraText={ - - {t('键为请求中的模型名称,值为要替换的模型名称')} - - } + extraText={t('键为请求中的模型名称,值为要替换的模型名称')} /> @@ -1628,11 +1620,7 @@ const EditChannelModal = (props) => { templateLabel={t('填入模板')} editorType="keyValue" formApi={formApiRef.current} - extraText={ - - {t('键为原状态码,值为要复写的状态码,仅影响本地判断')} - - } + extraText={t('键为原状态码,值为要复写的状态码,仅影响本地判断')} /> diff --git a/web/src/components/table/models/modals/EditModelModal.jsx b/web/src/components/table/models/modals/EditModelModal.jsx index 47173c77..7e368aac 100644 --- a/web/src/components/table/models/modals/EditModelModal.jsx +++ b/web/src/components/table/models/modals/EditModelModal.jsx @@ -390,7 +390,7 @@ const EditModelModal = (props) => { editorType='object' template={ENDPOINT_TEMPLATE} templateLabel={t('填入模板')} - extraText={({t('留空则使用默认端点;支持 {path, method}')})} + extraText={t('留空则使用默认端点;支持 {path, method}')} extraFooter={endpointGroups.length > 0 && ( {endpointGroups.map(group => (