golang 解决 protobuf 因为 XXX 插入数据库报

2020-03-17  本文已影响0人  hwholiday

起因

解决方案

开始搞事情

获取代码

go get github.com/golang/protobuf/protoc-gen-go

修改代码

// generateInternalStructFields just adds the XXX_<something> fields to the message struct.
func (g *Generator) generateInternalStructFields(mc *msgCtx, topLevelFields []topLevelField) {
    g.P("XXX_NoUnkeyedLiteral\tstruct{} `json:\"-\" xorm:\"-\"`") // prevent unkeyed struct literals
    if len(mc.message.ExtensionRange) > 0 {
        messageset := ""
        if opts := mc.message.Options; opts != nil && opts.GetMessageSetWireFormat() {
            messageset = "protobuf_messageset:\"1\" "
        }
        g.P(g.Pkg["proto"], ".XXX_InternalExtensions `", messageset, "json:\"-\" xorm:\"-\"`")
    }
    g.P("XXX_unrecognized\t[]byte `json:\"-\" xorm:\"-\"`")
    g.P("XXX_sizecache\tint32 `json:\"-\" xorm:\"-\"`")

}

编译代码得到 protoc-gen-go 并放到 /go/bin/ 下

protoc  --go_out=. *.proto

成果

type BoolValue struct {
    Value                bool     `protobuf:"varint,1,opt,name=value,proto3" json:"value,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-" xorm:"-"`
    XXX_unrecognized     []byte   `json:"-" xorm:"-"`
    XXX_sizecache        int32    `json:"-" xorm:"-"`
}

结语

上一篇 下一篇

猜你喜欢

热点阅读