英文:
Golang RethinkDB ChangeFeed Structure
问题
我想知道是否有人可以解释一下如何将我的 changefeed 游标值反序列化为特定的结构体类型。
var message map[string]interface{}
for chatFeedCursor.Next(&message) {
fmt.Println(message)
}
map[new_val:map[club_id:ea2eb6e2-755f-4dad-922d-e3693b6e55c6
date:2017-04-07 14:48:17.714 +0100 +01:00
id:e389ab54-963e-4b33-9b34-adcb6ec5b17e message:what is the meaning of life?
user_id:00ff679f-9421-4b8b-ae7f-d11cf2adaee2] old_val:]
然而,我希望将响应映射到 ChatMessage 结构体。
更新:
我尝试过:
var message ChatMessage
然而,似乎我的数据没有设置到结构体中。
{ 0001-01-01 00:00:00 +0000 UTC}
我的结构体定义如下:
type ChatMessage struct {
ID string `json:"id" gorethink:"id,omitempty"`
UserID string `json:"user_id" gorethink:"user_id"`
ClubID string `json:"club_id" gorethink:"club_id"`
Message string `json:"message" gorethink:"message"`
Date time.Time `json:"date" gorethink:"date"`
}
谢谢。
英文:
I was wondering if someone could explain how to unmarshal my changefeed cursor value to a specific struct type.
var message map[string]interface{}
for chatFeedCursor.Next(&message) {
fmt.Println(message)
}
> map[new_val:map[club_id:ea2eb6e2-755f-4dad-922d-e3693b6e55c6
> date:2017-04-07 14:48:17.714 +0100 +01:00
> id:e389ab54-963e-4b33-9b34-adcb6ec5b17e message:what is the meaning of life?
> user_id:00ff679f-9421-4b8b-ae7f-d11cf2adaee2] old_val:<nil>]
However, I would like the response to be mapped to struct ChatMessage.
Update:
I've tried:
var message ChatMessage
However, it doesn't seem like any of my data gets set in the struct.
> { 0001-01-01 00:00:00 +0000 UTC}
My struct:
type ChatMessage struct {
ID string `json:"id" gorethink:"id,omitempty"`
UserID string `json:"user_id" gorethink:"user_id"`
ClubID string `json:"club_id" gorethink:"club_id"`
Message string `json:"message" gorethink:"message"`
Date time.Time `json:"date" gorethink:"date"`
}
Thanks.
答案1
得分: 1
我明白了!
问题在于我没有在rethinkdb的change请求中指定一个字段。
之前的代码:
chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Run(gorethinkSession)
现在的代码:
chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Field("new_val").Run(gorethinkSession)
现在,.Next()的值可以正常映射到我的结构体了。
英文:
I figured it out!
The problem was that I didn't specify a field on the rethinkdb change request.
Previous code:
chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Run(gorethinkSession)
Working Code:
chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Field("new_val").Run(gorethinkSession)
Now the .Next() value maps to my struct with no issues.
答案2
得分: 0
如果你想将 changefeed 游标值反序列化为特定的结构体类型,并保留新旧值,你可以使用一个将通用映射值解码为本地 Go 结构体的库,比如 mapstructure,以获得一个整洁的结果:
导入 mapstructure:
import (
...
"github.com/mitchellh/mapstructure"
...
)
现在你可以这样做:
cursor, err := r.Table("club_chat").
Changes().
Run(db.Session)
if err != nil {
// 处理错误
}
var changeFeed map[string]interface{}
for cursor.Next(&changeFeed) {
var oldVal, newVal ChatMessage
if changeFeed["old_val"] != nil {
mapstructure.Decode(changeFeed["old_val"], &oldVal)
}
if changeFeed["new_val"] != nil {
mapstructure.Decode(changeFeed["new_val"], &newVal)
}
// 对值进行操作
}
英文:
If you want to unmarshal the changefeed cursor value to a specific struct type, but keep both the new and the old value, you could use a library for decoding generic map values into native Go structures, like mapstructure, to obtain a neat result:
Import mapstructure:
import (
...
"github.com/mitchellh/mapstructure"
...
)
Now you can do something like:
cursor, err := r.Table("club_chat").
Changes().
Run(db.Session)
if err != nil {
// manage error
}
var changeFeed map[string]interface{}
for cursor.Next(&changeFeed) {
var oldVal, newVal ChatMessage
if changeFeed["old_val"] != nil {
mapstructure.Decode(changeFeed["old_val"], &oldVal)
}
if changeFeed["new_val"] != nil {
mapstructure.Decode(changeFeed["new_val"], &newVal)
}
// Do what you want with the values
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论