首页 > 解决方案 > 将 protobuf 对象哈希为字符串作为 redis 数据库的键

问题描述

我有某种复杂的 protobuf 对象。这是发送到我的 GRPC 端点的请求。如果我以前没有,我只想处理它。所以我想将对象散列到某个字符串并将其存储在我的 Redis 数据库中。我使用了 ObjectHash-Proto,但使用新版本的 protobuf-compiler 我得到了这个错误:

got an unexpected struct of type 'impl.MessageState' for field {Name:state PkgPath:... Type:impl.MessageState Tag: Offset:0 Index:[0] Anonymous:false}

似乎它不支持结构和新版本的 protobuf-compiler 生成包含结构的代码。

我无法为每个请求生成某种 ID。ID实际上是整个对象的散列。

标签: gohashprotobuf-go

解决方案


如果您有proto.Message[1],那么您将免费获得 Marshal 函数 [2]。因此,在您编组消息之后,您可以将字节传递给base64md5或任何您想要的:

package main

import (
   "encoding/base64"
   "google.golang.org/protobuf/proto"
   "google.golang.org/protobuf/types/known/structpb"
)

func hash(m proto.Message) (string, error) {
   b, err := proto.Marshal(m)
   if err != nil {
      return "", err
   }
   return base64.StdEncoding.EncodeToString(b), nil
}

func main() {
   m, err := structpb.NewStruct(map[string]interface{}{
      "month": 12, "day": 31,
   })
   if err != nil {
      panic(err)
   }
   s, err := hash(m)
   if err != nil {
      panic(err)
   }
   println(s) // ChIKBW1vbnRoEgkRAAAAAAAAKEAKEAoDZGF5EgkRAAAAAAAAP0A=
}
  1. https://godocs.io/google.golang.org/protobuf/proto#Message
  2. https://godocs.io/google.golang.org/protobuf/proto#Marshal

推荐阅读