Skip to content

Instantly share code, notes, and snippets.

@spraints
Last active January 26, 2018 21:46
Show Gist options
  • Save spraints/3cbd71e5f1ccbcd30dc1d266ec19ac78 to your computer and use it in GitHub Desktop.
Save spraints/3cbd71e5f1ccbcd30dc1d266ec19ac78 to your computer and use it in GitHub Desktop.
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: ex.proto
/*
Package ex is a generated protocol buffer package.
It is generated from these files:
ex.proto
It has these top-level messages:
Person
Example
Thing
X
Y
*/
package ex
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// 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.ProtoPackageIsVersion2 // please upgrade the proto package
type Person struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Age int32 `protobuf:"varint,2,opt,name=age" json:"age,omitempty"`
Married bool `protobuf:"varint,3,opt,name=married" json:"married,omitempty"`
// Types that are valid to be assigned to Number:
// *Person_I
// *Person_F
Number isPerson_Number `protobuf_oneof:"number"`
}
func (m *Person) Reset() { *m = Person{} }
func (m *Person) String() string { return proto.CompactTextString(m) }
func (*Person) ProtoMessage() {}
func (*Person) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type isPerson_Number interface {
isPerson_Number()
}
type Person_I struct {
I int32 `protobuf:"varint,5,opt,name=i,oneof"`
}
type Person_F struct {
F float32 `protobuf:"fixed32,8,opt,name=f,oneof"`
}
func (*Person_I) isPerson_Number() {}
func (*Person_F) isPerson_Number() {}
func (m *Person) GetNumber() isPerson_Number {
if m != nil {
return m.Number
}
return nil
}
func (m *Person) GetName() string {
if m != nil {
return m.Name
}
return ""
}
func (m *Person) GetAge() int32 {
if m != nil {
return m.Age
}
return 0
}
func (m *Person) GetMarried() bool {
if m != nil {
return m.Married
}
return false
}
func (m *Person) GetI() int32 {
if x, ok := m.GetNumber().(*Person_I); ok {
return x.I
}
return 0
}
func (m *Person) GetF() float32 {
if x, ok := m.GetNumber().(*Person_F); ok {
return x.F
}
return 0
}
// XXX_OneofFuncs is for the internal use of the proto package.
func (*Person) 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 _Person_OneofMarshaler, _Person_OneofUnmarshaler, _Person_OneofSizer, []interface{}{
(*Person_I)(nil),
(*Person_F)(nil),
}
}
func _Person_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
m := msg.(*Person)
// number
switch x := m.Number.(type) {
case *Person_I:
b.EncodeVarint(5<<3 | proto.WireVarint)
b.EncodeVarint(uint64(x.I))
case *Person_F:
b.EncodeVarint(8<<3 | proto.WireFixed32)
b.EncodeFixed32(uint64(math.Float32bits(x.F)))
case nil:
default:
return fmt.Errorf("Person.Number has unexpected type %T", x)
}
return nil
}
func _Person_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
m := msg.(*Person)
switch tag {
case 5: // number.i
if wire != proto.WireVarint {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeVarint()
m.Number = &Person_I{int32(x)}
return true, err
case 8: // number.f
if wire != proto.WireFixed32 {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeFixed32()
m.Number = &Person_F{math.Float32frombits(uint32(x))}
return true, err
default:
return false, nil
}
}
func _Person_OneofSizer(msg proto.Message) (n int) {
m := msg.(*Person)
// number
switch x := m.Number.(type) {
case *Person_I:
n += proto.SizeVarint(5<<3 | proto.WireVarint)
n += proto.SizeVarint(uint64(x.I))
case *Person_F:
n += proto.SizeVarint(8<<3 | proto.WireFixed32)
n += 4
case nil:
default:
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
}
return n
}
type Example struct {
Thing *Thing `protobuf:"bytes,1,opt,name=thing" json:"thing,omitempty"`
}
func (m *Example) Reset() { *m = Example{} }
func (m *Example) String() string { return proto.CompactTextString(m) }
func (*Example) ProtoMessage() {}
func (*Example) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *Example) GetThing() *Thing {
if m != nil {
return m.Thing
}
return nil
}
type Thing struct {
// Types that are valid to be assigned to Shapeshifter:
// *Thing_X
// *Thing_Y
Shapeshifter isThing_Shapeshifter `protobuf_oneof:"shapeshifter"`
}
func (m *Thing) Reset() { *m = Thing{} }
func (m *Thing) String() string { return proto.CompactTextString(m) }
func (*Thing) ProtoMessage() {}
func (*Thing) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
type isThing_Shapeshifter interface {
isThing_Shapeshifter()
}
type Thing_X struct {
X *X `protobuf:"bytes,1,opt,name=x,oneof"`
}
type Thing_Y struct {
Y *Y `protobuf:"bytes,2,opt,name=y,oneof"`
}
func (*Thing_X) isThing_Shapeshifter() {}
func (*Thing_Y) isThing_Shapeshifter() {}
func (m *Thing) GetShapeshifter() isThing_Shapeshifter {
if m != nil {
return m.Shapeshifter
}
return nil
}
func (m *Thing) GetX() *X {
if x, ok := m.GetShapeshifter().(*Thing_X); ok {
return x.X
}
return nil
}
func (m *Thing) GetY() *Y {
if x, ok := m.GetShapeshifter().(*Thing_Y); ok {
return x.Y
}
return nil
}
// XXX_OneofFuncs is for the internal use of the proto package.
func (*Thing) 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 _Thing_OneofMarshaler, _Thing_OneofUnmarshaler, _Thing_OneofSizer, []interface{}{
(*Thing_X)(nil),
(*Thing_Y)(nil),
}
}
func _Thing_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
m := msg.(*Thing)
// shapeshifter
switch x := m.Shapeshifter.(type) {
case *Thing_X:
b.EncodeVarint(1<<3 | proto.WireBytes)
if err := b.EncodeMessage(x.X); err != nil {
return err
}
case *Thing_Y:
b.EncodeVarint(2<<3 | proto.WireBytes)
if err := b.EncodeMessage(x.Y); err != nil {
return err
}
case nil:
default:
return fmt.Errorf("Thing.Shapeshifter has unexpected type %T", x)
}
return nil
}
func _Thing_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
m := msg.(*Thing)
switch tag {
case 1: // shapeshifter.x
if wire != proto.WireBytes {
return true, proto.ErrInternalBadWireType
}
msg := new(X)
err := b.DecodeMessage(msg)
m.Shapeshifter = &Thing_X{msg}
return true, err
case 2: // shapeshifter.y
if wire != proto.WireBytes {
return true, proto.ErrInternalBadWireType
}
msg := new(Y)
err := b.DecodeMessage(msg)
m.Shapeshifter = &Thing_Y{msg}
return true, err
default:
return false, nil
}
}
func _Thing_OneofSizer(msg proto.Message) (n int) {
m := msg.(*Thing)
// shapeshifter
switch x := m.Shapeshifter.(type) {
case *Thing_X:
s := proto.Size(x.X)
n += proto.SizeVarint(1<<3 | proto.WireBytes)
n += proto.SizeVarint(uint64(s))
n += s
case *Thing_Y:
s := proto.Size(x.Y)
n += proto.SizeVarint(2<<3 | proto.WireBytes)
n += proto.SizeVarint(uint64(s))
n += s
case nil:
default:
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
}
return n
}
type X struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
}
func (m *X) Reset() { *m = X{} }
func (m *X) String() string { return proto.CompactTextString(m) }
func (*X) ProtoMessage() {}
func (*X) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
func (m *X) GetName() string {
if m != nil {
return m.Name
}
return ""
}
type Y struct {
Title string `protobuf:"bytes,1,opt,name=title" json:"title,omitempty"`
}
func (m *Y) Reset() { *m = Y{} }
func (m *Y) String() string { return proto.CompactTextString(m) }
func (*Y) ProtoMessage() {}
func (*Y) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
func (m *Y) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func init() {
proto.RegisterType((*Person)(nil), "ex.Person")
proto.RegisterType((*Example)(nil), "ex.Example")
proto.RegisterType((*Thing)(nil), "ex.Thing")
proto.RegisterType((*X)(nil), "ex.X")
proto.RegisterType((*Y)(nil), "ex.Y")
}
func init() { proto.RegisterFile("ex.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 235 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x4d, 0x4b, 0x03, 0x31,
0x10, 0x86, 0x9d, 0xad, 0xd9, 0x6e, 0x47, 0x29, 0x12, 0x14, 0xe3, 0xc9, 0x25, 0xa7, 0xc5, 0xc3,
0x1e, 0xf4, 0x1f, 0x14, 0x0a, 0x1e, 0x25, 0x78, 0x68, 0x8f, 0x29, 0xce, 0x76, 0x03, 0xdd, 0x0f,
0xb2, 0x11, 0xd2, 0x7f, 0x2f, 0x93, 0x76, 0x6f, 0xde, 0xde, 0xe7, 0xc9, 0x4b, 0x92, 0x19, 0x2c,
0x28, 0xd6, 0xa3, 0x1f, 0xc2, 0x20, 0x33, 0x8a, 0xda, 0x63, 0xfe, 0x45, 0x7e, 0x1a, 0x7a, 0x29,
0xf1, 0xb6, 0xb7, 0x1d, 0x29, 0x28, 0xa1, 0x5a, 0x99, 0x94, 0xe5, 0x03, 0x2e, 0xec, 0x91, 0x54,
0x56, 0x42, 0x25, 0x0c, 0x47, 0xa9, 0x70, 0xd9, 0x59, 0xef, 0x1d, 0xfd, 0xa8, 0x45, 0x09, 0x55,
0x61, 0x66, 0x94, 0x6b, 0x04, 0xa7, 0x04, 0x37, 0x3f, 0x6f, 0x0c, 0x38, 0xe6, 0x46, 0x15, 0x25,
0x54, 0x19, 0x73, 0xb3, 0x29, 0x30, 0xef, 0x7f, 0xbb, 0x03, 0x79, 0xfd, 0x86, 0xcb, 0x6d, 0xb4,
0xdd, 0x78, 0x22, 0xf9, 0x8a, 0x22, 0xb4, 0xae, 0x3f, 0xa6, 0x57, 0xef, 0xde, 0x57, 0x35, 0xc5,
0xfa, 0x9b, 0x85, 0xb9, 0x78, 0xbd, 0x45, 0x91, 0x58, 0x3e, 0x21, 0xc4, 0x6b, 0x4b, 0x70, 0x6b,
0xc7, 0xb7, 0x46, 0xd6, 0xe7, 0xf4, 0xbf, 0xab, 0xde, 0xb3, 0x3e, 0x6f, 0xd6, 0x78, 0x3f, 0xb5,
0x76, 0xa4, 0xa9, 0x75, 0x4d, 0x20, 0xaf, 0x9f, 0x11, 0x76, 0xff, 0x4d, 0xa8, 0x5f, 0x10, 0xf6,
0xf2, 0x11, 0x45, 0x70, 0xe1, 0x34, 0x9f, 0x5c, 0xe0, 0x90, 0xa7, 0x2d, 0x7d, 0xfc, 0x05, 0x00,
0x00, 0xff, 0xff, 0x16, 0x47, 0xc7, 0xfd, 0x31, 0x01, 0x00, 0x00,
}
syntax = "proto3";
package ex;
message Person {
string name = 1;
int32 age = 2;
bool married = 3;
oneof number {
int32 i = 5;
float f = 8;
}
}
message Example {
Thing thing = 1;
}
message Thing {
oneof shapeshifter {
X x = 1;
Y y = 2;
}
}
message X {
string name = 1;
}
message Y {
string title = 1;
}
package ex
import (
"bytes"
"testing"
"github.com/golang/protobuf/jsonpb"
)
func TestRoundTrip(t *testing.T) {
msg := &Example{
Thing: &Thing{
Shapeshifter: &Thing_X{&X{"anything"}},
},
}
var buf bytes.Buffer
marshaler := &jsonpb.Marshaler{}
err := marshaler.Marshal(&buf, msg)
if err != nil {
t.Fatalf("Could not marshal an Example")
}
var msg2 Example
err = jsonpb.Unmarshal(bytes.NewBuffer(buf.Bytes()), &msg2)
if err != nil {
t.Fatalf("Could not unmarshal %s: %s", buf.String(), err.Error())
}
msg2Name := msg2.GetThing().GetX().GetName()
if "anything" != msg2Name {
t.Errorf("unmarshaled name: Expected %q, got %q", "anything", msg2Name)
}
t.Logf("json: %s\nloaded: %#v\nThing: %#v\nX: %#v", buf.String(), msg2, msg2.GetThing(), msg2.GetThing().GetX())
}
func TestRoundTripPerson(t *testing.T) {
var expected int32 = 99
msg := &Person{
Number: &Person_I{expected},
}
var buf bytes.Buffer
marshaler := &jsonpb.Marshaler{}
err := marshaler.Marshal(&buf, msg)
if err != nil {
t.Fatalf("Could not marshal a Person")
}
var msg2 Example
err = jsonpb.Unmarshal(bytes.NewBuffer(buf.Bytes()), &msg2)
if err != nil {
t.Fatalf("Could not unmarshal %s: %s", buf.String(), err.Error())
}
actual := msg.GetI()
if expected != actual {
t.Errorf("unmarshaled name: Expected %q, got %q", expected, actual)
}
}
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/golang/protobuf"
packages = ["jsonpb","proto","ptypes/struct"]
revision = "925541529c1fa6821df4e44ce2723319eb2be768"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "e88128f0b62082f7e1420f87839c41963ddafc9c77a52494d9613b2fc7b92dc7"
solver-name = "gps-cdcl"
solver-version = 1
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
[[constraint]]
branch = "master"
name = "github.com/golang/protobuf"
#!/bin/bash
set -e
dep ensure
protoc ex.proto --go_out=.
go test .
@spraints
Copy link
Author

$ bash try.sh
--- FAIL: TestRoundTrip (0.00s)
        ex_test.go:26: Could not unmarshal {"thing":{"x":{"name":"anything"}}}
FAIL
FAIL    github.com/github/launch/ex     0.003s

@puellanivis
Copy link

o.O I cannot reproduce.

$ bash try.sh
ok  	github.com/github/launch/ex	0.008s

Also, you should include the err in your t.Fatal call so that you can see why it is failing to marshal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment