Skip to content

Instantly share code, notes, and snippets.

@johanbrandhorst
Created October 25, 2016 11:54
Show Gist options
  • Save johanbrandhorst/783e0908940a2f81d7aef7b1ac93472e to your computer and use it in GitHub Desktop.
Save johanbrandhorst/783e0908940a2f81d7aef7b1ac93472e 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: data/data.proto
// DO NOT EDIT!
/*
Package data is a generated protocol buffer package.
It is generated from these files:
data/data.proto
It has these top-level messages:
Value
*/
package data
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 fileDescriptorData, []int{0} }
type isValue_Type interface {
isValue_Type()
}
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), "data.Value")
}
func (this *Value) Compare(that interface{}) int {
if that == nil {
if this == nil {
return 0
}
return 1
}
that1, ok := that.(*Value)
if !ok {
that2, ok := that.(Value)
if ok {
that1 = &that2
} else {
return 1
}
}
if that1 == nil {
if this == nil {
return 0
}
return 1
} else if this == nil {
return -1
}
if that1.Type == nil {
if this.Type != nil {
return 1
}
} else if this.Type == nil {
return -1
} else if c := this.Type.Compare(that1.Type); c != 0 {
return c
}
return 0
}
func (this *Value_TypeOne) Compare(that interface{}) int {
if that == nil {
if this == nil {
return 0
}
return 1
}
that1, ok := that.(*Value_TypeOne)
if !ok {
that2, ok := that.(Value_TypeOne)
if ok {
that1 = &that2
} else {
return 1
}
}
if that1 == nil {
if this == nil {
return 0
}
return 1
} else if this == nil {
return -1
}
if this.TypeOne != that1.TypeOne {
if this.TypeOne < that1.TypeOne {
return -1
}
return 1
}
return 0
}
func (this *Value_TypeTwo) Compare(that interface{}) int {
if that == nil {
if this == nil {
return 0
}
return 1
}
that1, ok := that.(*Value_TypeTwo)
if !ok {
that2, ok := that.(Value_TypeTwo)
if ok {
that1 = &that2
} else {
return 1
}
}
if that1 == nil {
if this == nil {
return 0
}
return 1
} else if this == nil {
return -1
}
if this.TypeTwo != that1.TypeTwo {
if this.TypeTwo < that1.TypeTwo {
return -1
}
return 1
}
return 0
}
func init() { proto.RegisterFile("data/data.proto", fileDescriptorData) }
var fileDescriptorData = []byte{
// 140 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4f, 0x49, 0x2c, 0x49,
0xd4, 0x07, 0x11, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x2c, 0x20, 0xb6, 0x94, 0x6e, 0x7a,
0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0x7e, 0x7a, 0x7e, 0x7a, 0xbe, 0x3e, 0x58,
0x32, 0xa9, 0x34, 0x0d, 0xcc, 0x03, 0x73, 0xc0, 0x2c, 0x88, 0x26, 0x25, 0x4f, 0x2e, 0xd6, 0xb0,
0xc4, 0x9c, 0xd2, 0x54, 0x21, 0x69, 0x2e, 0x8e, 0x92, 0xca, 0x82, 0xd4, 0xf8, 0xfc, 0xbc, 0x54,
0x09, 0x46, 0x05, 0x46, 0x0d, 0x66, 0x0f, 0x86, 0x20, 0x76, 0x90, 0x88, 0x7f, 0x1e, 0x42, 0xb2,
0xa4, 0x3c, 0x5f, 0x82, 0x49, 0x81, 0x51, 0x83, 0x05, 0x26, 0x19, 0x52, 0x9e, 0xef, 0xc4, 0xc6,
0xc5, 0x02, 0x62, 0x3a, 0xb1, 0xac, 0x78, 0x2c, 0xc7, 0x98, 0xc4, 0x06, 0x36, 0xd7, 0x18, 0x10,
0x00, 0x00, 0xff, 0xff, 0x7e, 0x3d, 0x15, 0xa4, 0x9f, 0x00, 0x00, 0x00,
}
syntax = "proto3";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.compare_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