我有一個包含字段 JSONB 的簡單表:CREATE TABLE IF NOT EXISTS "test_table" ( "id" text NOT NULL, "user_id" text NOT NULL, "content" jsonb NOT NULL, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, PRIMARY KEY ("id"));我使用一個簡單的查詢來生成帶有 SQLC 的樣板文件。-- name: GetTestData :oneSELECT * FROM test_tableWHERE id = $1 LIMIT 1;但是該content屬性生成為json.RawMessage.type TestTable struct { ID string `json:"id"` UserId string `json:"user_id"` Content json.RawMessage `json:"content"` CreateTime time.Time `json:"create_time"` UpdateTime time.Time `json:"update_time"`}下面是存儲在內容列中的 JSON 示例:{ "static": { "product": [ { "id": "string", "elements": { "texts": [ { "id": "string", "value": "string" } ], "colors": [ { "id": "string", "value": "string" } ], "images": [ { "id": "string", "values": [ { "id": "string", "value": "string" } ] } ] } } ] }, "dynamic": { "banner": [ { "id": "string", "elements": { "texts": [ { "id": "string", "value": "string" } ], "colors": [ { "id": "string", "value": "string" } ], "images": [ { "id": "string", "values": [ { "id": "string", "value": "string" } ] } ] } } ] }}Static 或 Dynamic 中的嵌套屬性是數組。content 屬性應該包含一個嵌套對象,我似乎無法提取其中的數據。json.Unrmarshall()似乎只獲得頂級屬性。有沒有辦法轉換 map[string]interface{}為內容或幫助 SQLC 生成屬性作為接口而不是 RawMessage?
- 1 回答
- 0 關注
- 141 瀏覽
添加回答
舉報
0/150
提交
取消