Golang RethinkDB ChangeFeed 结构

huangapple go评论86阅读模式
英文:

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:&quot;id&quot; gorethink:&quot;id,omitempty&quot;`
    UserID      string    `json:&quot;user_id&quot; gorethink:&quot;user_id&quot;`
    ClubID      string    `json:&quot;club_id&quot; gorethink:&quot;club_id&quot;`
    Message     string    `json:&quot;message&quot; gorethink:&quot;message&quot;`
    Date    time.Time     `json:&quot;date&quot; gorethink:&quot;date&quot;`
}

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(&quot;club_chat&quot;).Changes().Run(gorethinkSession)

Working Code:

chatFeedCursor, _ := gorethink.Table(&quot;club_chat&quot;).Changes().Field(&quot;new_val&quot;).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 (
   ...
	&quot;github.com/mitchellh/mapstructure&quot;
   ...
)

Now you can do something like:

	cursor, err := r.Table(&quot;club_chat&quot;).
		Changes().
		Run(db.Session)

	if err != nil {
		// manage error
	}

	var changeFeed map[string]interface{}
	for cursor.Next(&amp;changeFeed) {

		var oldVal, newVal ChatMessage
		if changeFeed[&quot;old_val&quot;] != nil {
			mapstructure.Decode(changeFeed[&quot;old_val&quot;], &amp;oldVal)
		}

		if changeFeed[&quot;new_val&quot;] != nil {
			mapstructure.Decode(changeFeed[&quot;new_val&quot;], &amp;newVal)
		}

      // Do what you want with the values

huangapple
  • 本文由 发表于 2017年4月7日 22:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/43280088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定