From f1856fe4d2e9e56322ec65879856507a33a29a56 Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Mon, 7 Jul 2025 01:31:41 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20**fix(model,=20controller):=20ro?= =?UTF-8?q?bust=20serialization=20for=20`ChannelInfo`=20&=20correct=20Vert?= =?UTF-8?q?ex-AI=20key=20storage**?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary 1. **model/channel.go** • Replaced the pointer-only `Value()` with a value-receiver implementation • GORM can now marshal both `ChannelInfo` and `*ChannelInfo`, eliminating `unsupported type model.ChannelInfo` runtime error. 2. **controller/channel.go** • Refactored `getVertexArrayKeys()` – every element is now - passed through `json.Marshal` when not already a string - trimmed & validated before insertion • Guarantees each service-account key is persisted as a **pure JSON string** instead of the previous `map[...]` dump. Result • Channel creation / update succeeds without SQL driver errors. • Vertex-AI batch & multi-key uploads are stored in canonical JSON, ready for downstream SDKs to consume. --- controller/channel.go | 14 ++++++++++++-- model/channel.go | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index bf735779..8e5ae40a 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -394,9 +394,19 @@ func getVertexArrayKeys(keys string) ([]string, error) { } cleanKeys := make([]string, 0, len(keyArray)) for _, key := range keyArray { - keyStr := fmt.Sprintf("%v", key) + var keyStr string + switch v := key.(type) { + case string: + keyStr = strings.TrimSpace(v) + default: + bytes, err := json.Marshal(v) + if err != nil { + return nil, fmt.Errorf("Vertex AI key JSON 编码失败: %w", err) + } + keyStr = string(bytes) + } if keyStr != "" { - cleanKeys = append(cleanKeys, strings.TrimSpace(keyStr)) + cleanKeys = append(cleanKeys, keyStr) } } if len(cleanKeys) == 0 { diff --git a/model/channel.go b/model/channel.go index 0b8d9ba2..4144075c 100644 --- a/model/channel.go +++ b/model/channel.go @@ -53,8 +53,8 @@ type ChannelInfo struct { } // Value implements driver.Valuer interface -func (c *ChannelInfo) Value() (driver.Value, error) { - return common.EncodeJson(c) +func (c ChannelInfo) Value() (driver.Value, error) { + return common.EncodeJson(&c) } // Scan implements sql.Scanner interface