将结构传递给redigo Send函数会破坏它,并且数据丢失
This is my struct, when I get a socket message I readJson and the structs gets filled with the data and all is fine. 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?
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
这是我的结构,当我收到套接字消息时,我读了Json,结构中充满了数据,所有 精细。 它通过一些函数,但是一旦通过发送函数,它就会以一种怪异的方式对其进行序列化,最终我得到了一堆数字,当我将其转换为字符串时,数据丢失了。 p>
type回复结构{
主题字符串`redis:“ topic” json:“ topic”`
引用字符串`redis:“ ref” json:“ ref”`
有效载荷结构{
状态字符串`redis:“ status” json:“ status”`
响应图[string] interface {}`redis:“ response” json:“ response”`
}`json:“ 有效载荷”`
}
code> pre>
我只想广播这种格式的消息。 p>
这是获取修改过的有问题的数据的地方 p>
func(rr * redisReceiver)run()error {
l:= log.WithField(“ channel”,Channel)
conn:= rr.pool.Get()
延迟conn.Close()
psc:= redis.PubSubConn {Conn:conn}
psc.Subscribe(Channel )
前往rr.connHandler()
{{n switch v:= psc.Receive()。(type){
case redis.Message:
rr.broadcast(v.Data)
case redis 订阅:
l.WithFields(logrus.Fields {
“ kind”:v.Kind,
“ count”:v.Count,
})。Println(“ Redis Subscription Received”)
日志。 Println(“接收到Redis订阅”)
大小写错误:
返回错误。New(“订阅Redis频道时出错”)
默认值:
l.WithField(“ v”,v).Info(“未知 Redis在订阅过程中接收“)
log.Println(”未知Redis在订阅过程中接收“)
}
}
}
code> pre>
不支持Redigo 那种数据结构? p>
这是我的格式 获取和我应该获取的格式。 p>
//获取
“ {{spr_reply sketchpad map [] 1} {ok map [成功:成功加入]} }“
//应该获得
{事件:” spr_reply“,主题:” sketchpad“,参考:” 45“,有效载荷:{状态:
”确定“,响应:{}}}
pre>
在第55行中,我可以获取“损坏的”数据- https://play.golang.org/p/TOzJuvewlP p>
div>
Redigo supports the following conversions to Redis bulk strings:
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)
.
It looks like you want to encode the value as JSON. If so, do the encoding in the application. You can remove the redis
field tags.
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
}