1 Star 0 Fork 0

micro-tools / wf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
grpcutils.go 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 . 升级go-ole
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/micro-tools/wf/extend/utils.
package grpcutils
import (
"fmt"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)
var (
jsonPbMarshaller = &jsonpb.Marshaler{
EmitDefaults: true,
}
)
// MarshalPbMessageToJsonString marshals protobuf message to json string.
func MarshalPbMessageToJsonString(msg proto.Message) string {
msgJsonStr, _ := jsonPbMarshaller.MarshalToString(msg)
if len(msgJsonStr) > 10000 {
msgJsonStr = "内容太长,不显示"
}
return msgJsonStr
}
func MarshalMessageToJsonStringForTracing(value interface{}, msgType string, maxBytes int) string {
var messageContent string
if msg, ok := value.(proto.Message); ok {
if proto.Size(msg) <= maxBytes {
messageContent = MarshalPbMessageToJsonString(msg)
} else {
messageContent = fmt.Sprintf(
"[%s Message Too Large For Tracing, Max: %d bytes]",
msgType,
maxBytes,
)
}
} else {
messageContent = fmt.Sprintf("%v", value)
}
return messageContent
}
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891