go - 将 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实际上是整个对象的散列。
解决方案
如果您有proto.Message
[1],那么您将免费获得 Marshal 函数 [2]。因此,在您编组消息之后,您可以将字节传递给base64
,
md5
或任何您想要的:
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=
}
推荐阅读
- c# - exe.config 没有正确更新
- swift - WKWebView 进入后台时停止调用 Javascript
- c++ - 如果我在 C++ 中有一个字符数组,它是如何工作的?
- vue.js - Vue Apollo instance option issues, can't configure properly
- python - 如何在没有令牌或访问控制的情况下从 userId(pk) 获取 Instagram 用户个人资料详细信息?
- excel - 从 A 列 Excel 数组公式中查找 C 列中最接近的值
- python - 仅打开和读取目录内的目录文件
- c++ - Openssl 二进制文件没有 lib 文件或包含 Qt 的头文件
- jquery - 通过模态传递 data-id 到 jQuery 验证
- javascript - Get Data from Array Without Quotes