Skip to content

Instantly share code, notes, and snippets.

@johanbrandhorst
Created October 25, 2016 14:15
Show Gist options
  • Save johanbrandhorst/880326d70e69d191d7560fd498031605 to your computer and use it in GitHub Desktop.
Save johanbrandhorst/880326d70e69d191d7560fd498031605 to your computer and use it in GitHub Desktop.
# Install protoc
# Left as exercise to reader
# Install protoc-gen-gogo and friends
go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/jsonpb
go get github.com/gogo/protobuf/protoc-gen-gogo
go get github.com/gogo/protobuf/gogoproto
protoc ./my/my.proto --gogo_out=:./ -I./ -I.$GOPATH/src
// Code generated by protoc-gen-gogo.
// source: my/my.proto
// DO NOT EDIT!
/*
Package my is a generated protocol buffer package.
It is generated from these files:
my/my.proto
It has these top-level messages:
Value
*/
package my
import proto "github.com/gogo/protobuf/proto"
import fmt "fmt"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
type Value struct {
// Types that are valid to be assigned to Type:
// *Value_TypeOne
// *Value_TypeTwo
Type isValue_Type `protobuf_oneof:"type"`
}
func (m *Value) Reset() { *m = Value{} }
func (m *Value) String() string { return proto.CompactTextString(m) }
func (*Value) ProtoMessage() {}
func (*Value) Descriptor() ([]byte, []int) { return fileDescriptorMy, []int{0} }
type isValue_Type interface {
isValue_Type()
Size() int
ProtoSize() int
}
type Value_TypeOne struct {
TypeOne int64 `protobuf:"varint,1,opt,name=type_one,json=typeOne,proto3,oneof"`
}
type Value_TypeTwo struct {
TypeTwo uint64 `protobuf:"varint,2,opt,name=type_two,json=typeTwo,proto3,oneof"`
}
func (*Value_TypeOne) isValue_Type() {}
func (*Value_TypeTwo) isValue_Type() {}
func (m *Value) GetType() isValue_Type {
if m != nil {
return m.Type
}
return nil
}
func (m *Value) GetTypeOne() int64 {
if x, ok := m.GetType().(*Value_TypeOne); ok {
return x.TypeOne
}
return 0
}
func (m *Value) GetTypeTwo() uint64 {
if x, ok := m.GetType().(*Value_TypeTwo); ok {
return x.TypeTwo
}
return 0
}
// XXX_OneofFuncs is for the internal use of the proto package.
func (*Value) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
return _Value_OneofMarshaler, _Value_OneofUnmarshaler, _Value_OneofSizer, []interface{}{
(*Value_TypeOne)(nil),
(*Value_TypeTwo)(nil),
}
}
func _Value_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
m := msg.(*Value)
// type
switch x := m.Type.(type) {
case *Value_TypeOne:
_ = b.EncodeVarint(1<<3 | proto.WireVarint)
_ = b.EncodeVarint(uint64(x.TypeOne))
case *Value_TypeTwo:
_ = b.EncodeVarint(2<<3 | proto.WireVarint)
_ = b.EncodeVarint(uint64(x.TypeTwo))
case nil:
default:
return fmt.Errorf("Value.Type has unexpected type %T", x)
}
return nil
}
func _Value_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
m := msg.(*Value)
switch tag {
case 1: // type.type_one
if wire != proto.WireVarint {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeVarint()
m.Type = &Value_TypeOne{int64(x)}
return true, err
case 2: // type.type_two
if wire != proto.WireVarint {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeVarint()
m.Type = &Value_TypeTwo{x}
return true, err
default:
return false, nil
}
}
func _Value_OneofSizer(msg proto.Message) (n int) {
m := msg.(*Value)
// type
switch x := m.Type.(type) {
case *Value_TypeOne:
n += proto.SizeVarint(1<<3 | proto.WireVarint)
n += proto.SizeVarint(uint64(x.TypeOne))
case *Value_TypeTwo:
n += proto.SizeVarint(2<<3 | proto.WireVarint)
n += proto.SizeVarint(uint64(x.TypeTwo))
case nil:
default:
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
}
return n
}
func init() {
proto.RegisterType((*Value)(nil), "Value")
}
func (m *Value) Size() (n int) {
var l int
_ = l
if m.Type != nil {
n += m.Type.Size()
}
return n
}
func (m *Value_TypeOne) Size() (n int) {
var l int
_ = l
n += 1 + sovMy(uint64(m.TypeOne))
return n
}
func (m *Value_TypeTwo) Size() (n int) {
var l int
_ = l
n += 1 + sovMy(uint64(m.TypeTwo))
return n
}
func sovMy(x uint64) (n int) {
for {
n++
x >>= 7
if x == 0 {
break
}
}
return n
}
func sozMy(x uint64) (n int) {
return sovMy(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func init() { proto.RegisterFile("my/my.proto", fileDescriptorMy) }
var fileDescriptorMy = []byte{
// 139 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0xad, 0xd4, 0xcf,
0xad, 0xd4, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x97, 0xd2, 0x4d, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2,
0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xcf, 0x4f, 0xcf, 0xd7, 0x07, 0x0b, 0x27, 0x95, 0xa6, 0x81, 0x79,
0x60, 0x0e, 0x98, 0x05, 0x51, 0xae, 0xe4, 0xc9, 0xc5, 0x1a, 0x96, 0x98, 0x53, 0x9a, 0x2a, 0x24,
0xcd, 0xc5, 0x51, 0x52, 0x59, 0x90, 0x1a, 0x9f, 0x9f, 0x97, 0x2a, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1,
0xec, 0xc1, 0x10, 0xc4, 0x0e, 0x12, 0xf1, 0xcf, 0x43, 0x48, 0x96, 0x94, 0xe7, 0x4b, 0x30, 0x29,
0x30, 0x6a, 0xb0, 0xc0, 0x24, 0x43, 0xca, 0xf3, 0x9d, 0xd8, 0xb8, 0x58, 0x40, 0x4c, 0x27, 0x8e,
0x07, 0x8f, 0xe4, 0x18, 0x17, 0x3c, 0x96, 0x63, 0x4c, 0x62, 0x03, 0x9b, 0x6d, 0x0c, 0x08, 0x00,
0x00, 0xff, 0xff, 0xa7, 0x38, 0xaf, 0xb8, 0x99, 0x00, 0x00, 0x00,
}
syntax = "proto3";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.sizer_all) = true;
option (gogoproto.protosizer_all) = true;
message Value {
oneof type {
int64 type_one = 1;
uint64 type_two = 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment