简体   繁体   English

将结构传递给redigo Send函数会破坏它,并且数据丢失

[英]Passing a Struct to the redigo Send function breaks it and data is missing

This is my struct, when I get a socket message I readJson and the structs gets filled with the data and all is fine. 这是我的结构,当我收到套接字消息时,我读了Json,结构中充满了数据,一切都很好。 It goes through some functions, but once it goes through the Send function it serializes it in a weird way that eventually I get back a bunch of numbers and when I convert it to string, data is missing. 它通过一些函数,但是一旦通过发送函数,它就会以一种怪异的方式对其进行序列化,最终我得到了一堆数字,当我将其转换为字符串时,数据丢失了。

type Reply struct {
  Topic   string `redis:"topic" json:"topic"`
  Ref string `redis:"ref" json:"ref"`
  Payload struct {
      Status string `redis:"status" json:"status"`
      Response map[string]interface{} `redis:"response" json:"response"`
  } `json:"payload"`
}

I just want to broadcast messages in this format. 我只想以这种格式广播消息。

This is where I get the modified and problematic data 这是我获取修改过的有问题的数据的地方

func (rr *redisReceiver) run() error {
  l := log.WithField("channel", Channel)
  conn := rr.pool.Get()
  defer conn.Close()
  psc := redis.PubSubConn{Conn: conn}
  psc.Subscribe(Channel)
  go rr.connHandler()
  for {
    switch v := psc.Receive().(type) {
    case redis.Message:
        rr.broadcast(v.Data)
    case redis.Subscription:
        l.WithFields(logrus.Fields{
            "kind":  v.Kind,
            "count": v.Count,
        }).Println("Redis Subscription Received")
        log.Println("Redis Subscription Received")
    case error:
        return errors.New("Error while subscribed to Redis channel")
    default:
        l.WithField("v", v).Info("Unknown Redis receive during subscription")
        log.Println("Unknown Redis receive during subscription")
    }
  }
}

Does Redigo not support that type of data structure? Redigo是否不支持这种类型的数据结构?

This is the format I get and the format I'm supposed to get. 这是我得到的格式,也是我应该得到的格式。

//Get
"{{spr_reply sketchpad map[] 1} {ok map[success:Joined successfully]}}"
//Supposed to get
{event: "spr_reply", topic: "sketchpad", ref: "45", payload: {status: 
"ok", response: {}}}

On line 55 is where I get back the "corrupted" data - https://play.golang.org/p/TOzJuvewlP 在55行上,我可以获取“损坏的”数据-https://play.golang.org/p/TOzJuvewlP

Redigo supports the following conversions to Redis bulk strings : Redigo 支持以下到Redis批量字符串的转换

Go Type                 Conversion
[]byte                  Sent as is
string                  Sent as is
int, int64              strconv.FormatInt(v)
float64                 strconv.FormatFloat(v, 'g', -1, 64)
bool                    true -> "1", false -> "0"
nil                     ""
all other types         fmt.Print(v)

The Reply type is encoding using fmt.Print(v) . Reply类型使用fmt.Print(v)编码。

It looks like you want to encode the value as JSON. 看起来您想将值编码为JSON。 If so, do the encoding in the application. 如果是这样,请在应用程序中进行编码。 You can remove the redis field tags. 您可以删除redis字段标签。

writeToRedis(conn redis.Conn, data Reply) error {
    p, err := json.Marshl(data)
    if err != nil {
        return errors.Wrap(err, "Unable to encode message to json")
    }
    if err := conn.Send("PUBLISH", Channel, p); err != nil {
        return errors.Wrap(err, "Unable to publish message to Redis")
    }
    if err := conn.Flush(); err != nil {
        return errors.Wrap(err, "Unable to flush published message to Redis")
    }
    return nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM