Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pokstad
Created April 10, 2017 04:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pokstad/af2dacfa8565592b116749422592f80f to your computer and use it in GitHub Desktop.
Save pokstad/af2dacfa8565592b116749422592f80f to your computer and use it in GitHub Desktop.
A Go test that demonstrates how to marshal and unmarshal a Protobuf any.Any type
package anything_test
//go:generate protoc --go_out=. anything.proto
import (
"reflect"
"testing"
proto "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
any "github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/pokstad/anything"
)
func TestAnything(t *testing.T) {
t1 := &timestamp.Timestamp{
Seconds: 5, // easy to verify
Nanos: 6, // easy to verify
}
serialized, err := proto.Marshal(t1)
if err != nil {
t.Fatal("could not serialize timestamp")
}
// Blue was a great album by 3EB, before Cadgogan got kicked out
// and Jenkins went full primadonna
a := anything.AnythingForYou{
Anything: &any.Any{
TypeUrl: "example.com/yaddayaddayadda/" + proto.MessageName(t1),
Value: serialized,
},
}
// marshal to simulate going on the wire:
serializedA, err := proto.Marshal(&a)
if err != nil {
t.Fatal("could not serialize anything")
}
// unmarshal to simulate coming off the wire
var a2 anything.AnythingForYou
if err := proto.Unmarshal(serializedA, &a2); err != nil {
t.Fatal("could not deserialize anything")
}
// unmarshal the timestamp
var t2 timestamp.Timestamp
if err := ptypes.UnmarshalAny(a2.Anything, &t2); err != nil {
t.Fatalf("Could not unmarshal timestamp from anything field: %s", err)
}
// Verify the values are as expected
if !reflect.DeepEqual(t1, &t2) {
t.Fatalf("Values don't match up:\n %+v \n %+v", t1, t2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment