本文整理汇总了Golang中limbo/services/protobuf/proto.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestOldUnoM
func TestOldUnoM(t *testing.T) {
popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
older := NewPopulatedOldUnoM(popr, true)
// need optional field to be always initialized, to check it's lost in this test
older.Field1 = proto.String(randStringUnrecognized(popr))
data1, err := proto.Marshal(older)
if err != nil {
panic(err)
}
newer := &UnoM{}
if err = proto.Unmarshal(data1, newer); err != nil {
panic(err)
}
data2, err := proto.Marshal(newer)
if err != nil {
panic(err)
}
older2 := &OldUnoM{}
if err := proto.Unmarshal(data2, older2); err != nil {
panic(err)
}
// check that Field1 is lost
if older2.Field1 != nil {
t.Fatalf("field must be lost, but it's not, older: %#v, older2: %#v", older, older2)
}
// now restore Field1 and messages should be equal now
older2.Field1 = older.Field1
if err := older.VerboseEqual(older2); err != nil {
t.Fatalf("%#v !VerboseProto %#v, since %v", older, older2, err)
}
}
示例2: TestRepeatedExtensionsFieldsIssue161
func TestRepeatedExtensionsFieldsIssue161(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rep := 10
ints := make([]int64, rep)
for i := range ints {
ints[i] = r.Int63()
}
input := &MyExtendable{}
if err := proto.SetExtension(input, E_FieldD, ints); err != nil {
t.Fatal(err)
}
data, err := proto.Marshal(input)
if err != nil {
t.Fatal(err)
}
output := &MyExtendable{}
if err := proto.Unmarshal(data, output); err != nil {
t.Fatal(err)
}
if !input.Equal(output) {
t.Fatal("expected equal")
}
data2, err2 := proto.Marshal(output)
if err2 != nil {
t.Fatal(err2)
}
if len(data) != len(data2) {
t.Fatal("expected equal length buffers")
}
}
示例3: WriteMsg
func (this *fullWriter) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
_, err = m.MarshalTo(this.buffer)
if err != nil {
return err
}
data = this.buffer[:n]
} else {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
_, err = this.w.Write(data)
return err
}
示例4: WriteMsg
func (this *uint32Writer) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
_, err = m.MarshalTo(this.buffer)
if err != nil {
return err
}
data = this.buffer[:n]
} else {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
length := uint32(len(data))
if err = binary.Write(this.w, this.byteOrder, &length); err != nil {
return err
}
_, err = this.w.Write(data)
return err
}
示例5: WriteMsg
func (this *varintWriter) WriteMsg(msg proto.Message) (err error) {
var data []byte
if m, ok := msg.(marshaler); ok {
n, ok := getSize(m)
if !ok {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
if n >= len(this.buffer) {
this.buffer = make([]byte, n)
}
_, err = m.MarshalTo(this.buffer)
if err != nil {
return err
}
data = this.buffer[:n]
} else {
data, err = proto.Marshal(msg)
if err != nil {
return err
}
}
length := uint64(len(data))
n := binary.PutUvarint(this.lenBuf, length)
_, err = this.w.Write(this.lenBuf[:n])
if err != nil {
return err
}
_, err = this.w.Write(data)
return err
}
示例6: TestRepeatedExtensionsMsgsIssue161
func TestRepeatedExtensionsMsgsIssue161(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rep := 10
nins := make([]*NinOptNative, rep)
for i := range nins {
nins[i] = NewPopulatedNinOptNative(r, true)
}
input := &MyExtendable{}
if err := proto.SetExtension(input, E_FieldE, nins); err != nil {
t.Fatal(err)
}
data, err := proto.Marshal(input)
if err != nil {
t.Fatal(err)
}
output := &MyExtendable{}
if err := proto.Unmarshal(data, output); err != nil {
t.Fatal(err)
}
if !input.Equal(output) {
t.Fatal("expected equal")
}
data2, err2 := proto.Marshal(output)
if err2 != nil {
t.Fatal(err2)
}
if len(data) != len(data2) {
t.Fatal("expected equal length buffers")
}
}
示例7: testSize
func testSize(m interface {
proto.Message
Size() int
}, desc string, expected int) ([]byte, error) {
data, err := proto.Marshal(m)
if err != nil {
return nil, err
}
protoSize := proto.Size(m)
mSize := m.Size()
lenData := len(data)
if protoSize != mSize || protoSize != lenData || mSize != lenData {
return nil, fmt.Errorf("%s proto.Size(m){%d} != m.Size(){%d} != len(data){%d}", desc, protoSize, mSize, lenData)
}
if got := protoSize; got != expected {
return nil, fmt.Errorf("%s proto.Size(m) got %d expected %d", desc, got, expected)
}
if got := mSize; got != expected {
return nil, fmt.Errorf("%s m.Size() got %d expected %d", desc, got, expected)
}
if got := lenData; got != expected {
return nil, fmt.Errorf("%s len(data) got %d expected %d", desc, got, expected)
}
return data, nil
}
示例8: TestIndexQueryProto
func TestIndexQueryProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedIndexQuery(popr, false)
data, err := github_com_gogo_protobuf_proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &IndexQuery{}
if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(data))
copy(littlefuzz, data)
for i := range data {
data[i] = byte(popr.Intn(256))
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg)
}
}
示例9: TestRoundTripProto3
func TestRoundTripProto3(t *testing.T) {
m := &pb.Message{
Name: "David", // (2 | 1<<3): 0x0a 0x05 "David"
Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01
HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01
Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
ResultCount: 47, // (0 | 7<<3): 0x38 0x2f
TrueScotsman: true, // (0 | 8<<3): 0x40 0x01
Score: 8.1, // (5 | 9<<3): 0x4d <8.1>
Key: []uint64{1, 0xdeadbeef},
Nested: &pb.Nested{
Bunny: "Monty",
},
}
t.Logf(" m: %v", m)
b, err := proto.Marshal(m)
if err != nil {
t.Fatalf("proto.Marshal: %v", err)
}
t.Logf(" b: %q", b)
m2 := new(pb.Message)
if err := proto.Unmarshal(b, m2); err != nil {
t.Fatalf("proto.Unmarshal: %v", err)
}
t.Logf("m2: %v", m2)
if !proto.Equal(m, m2) {
t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
}
}
示例10: TestUnmarshalPartiallyPopulatedOptionalFieldsFails
func TestUnmarshalPartiallyPopulatedOptionalFieldsFails(t *testing.T) {
// Fill in all fields, then randomly remove one.
dataOut := &test.NinOptNative{
Field1: proto.Float64(0),
Field2: proto.Float32(0),
Field3: proto.Int32(0),
Field4: proto.Int64(0),
Field5: proto.Uint32(0),
Field6: proto.Uint64(0),
Field7: proto.Int32(0),
Field8: proto.Int64(0),
Field9: proto.Uint32(0),
Field10: proto.Int32(0),
Field11: proto.Uint64(0),
Field12: proto.Int64(0),
Field13: proto.Bool(false),
Field14: proto.String("0"),
Field15: []byte("0"),
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
fieldName := "Field" + strconv.Itoa(r.Intn(15)+1)
field := reflect.ValueOf(dataOut).Elem().FieldByName(fieldName)
fieldType := field.Type()
field.Set(reflect.Zero(fieldType))
encodedMessage, err := proto.Marshal(dataOut)
if err != nil {
t.Fatalf("Unexpected error when marshalling dataOut: %v", err)
}
dataIn := NidOptNative{}
err = proto.Unmarshal(encodedMessage, &dataIn)
if err.Error() != `proto: required field "`+fieldName+`" not set` {
t.Fatalf(`err.Error() != "proto: required field "`+fieldName+`" not set"; was "%s" instead`, err.Error())
}
}
示例11: TestZeroLengthOptionalBytes
func TestZeroLengthOptionalBytes(t *testing.T) {
roundtrip := func(f *Foo) *Foo {
data, err := proto.Marshal(f)
if err != nil {
panic(err)
}
newF := &Foo{}
err = proto.Unmarshal(data, newF)
if err != nil {
panic(err)
}
return newF
}
f := &Foo{}
roundtrippedF := roundtrip(f)
if roundtrippedF.Bar != nil {
t.Fatalf("should be nil")
}
f.Bar = []byte{}
roundtrippedF = roundtrip(f)
if roundtrippedF.Bar == nil {
t.Fatalf("should not be nil")
}
if len(roundtrippedF.Bar) != 0 {
t.Fatalf("should be empty")
}
}
示例12: TestGetExtensionStability
func TestGetExtensionStability(t *testing.T) {
check := func(m *pb.MyMessage) bool {
ext1, err := proto.GetExtension(m, pb.E_Ext_More)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
ext2, err := proto.GetExtension(m, pb.E_Ext_More)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
return ext1 == ext2
}
msg := &pb.MyMessage{Count: proto.Int32(4)}
ext0 := &pb.Ext{}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
t.Fatalf("Could not set ext1: %s", ext0)
}
if !check(msg) {
t.Errorf("GetExtension() not stable before marshaling")
}
bb, err := proto.Marshal(msg)
if err != nil {
t.Fatalf("Marshal() failed: %s", err)
}
msg1 := &pb.MyMessage{}
err = proto.Unmarshal(bb, msg1)
if err != nil {
t.Fatalf("Unmarshal() failed: %s", err)
}
if !check(msg1) {
t.Errorf("GetExtension() not stable after unmarshaling")
}
}
示例13: TestGetExtensionStability
//See another version of this test in proto/extensions_test.go
func TestGetExtensionStability(t *testing.T) {
check := func(m *NoExtensionsMap) bool {
ext1, err := proto.GetExtension(m, E_FieldB1)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
ext2, err := proto.GetExtension(m, E_FieldB1)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
return ext1.(*NinOptNative).Equal(ext2)
}
msg := &NoExtensionsMap{Field1: proto.Int64(2)}
ext0 := &NinOptNative{Field1: proto.Float64(1)}
if err := proto.SetExtension(msg, E_FieldB1, ext0); err != nil {
t.Fatalf("Could not set ext1: %s", ext0)
}
if !check(msg) {
t.Errorf("GetExtension() not stable before marshaling")
}
bb, err := proto.Marshal(msg)
if err != nil {
t.Fatalf("Marshal() failed: %s", err)
}
msg1 := &NoExtensionsMap{}
err = proto.Unmarshal(bb, msg1)
if err != nil {
t.Fatalf("Unmarshal() failed: %s", err)
}
if !check(msg1) {
t.Errorf("GetExtension() not stable after unmarshaling")
}
}
示例14: Marshal
func (this *codec) Marshal(v interface{}) ([]byte, error) {
if m, ok := v.(marshaler); ok {
n, ok := getSize(v)
if !ok {
return proto.Marshal(v.(proto.Message))
}
if n > len(this.buf) {
this.buf = make([]byte, n)
}
_, err := m.MarshalTo(this.buf)
if err != nil {
return nil, err
}
return this.buf[:n], nil
}
return proto.Marshal(v.(proto.Message))
}
示例15: TestIssue42Order
func TestIssue42Order(t *testing.T) {
unordered := NewPopulatedUnorderedFields(math_rand.New(math_rand.NewSource(time.Now().UnixNano())), false)
udata, err := proto.Marshal(unordered)
if err != nil {
t.Fatal(err)
}
ordered := &OrderedFields{}
if err = proto.Unmarshal(udata, ordered); err != nil {
t.Fatal(err)
}
data, err := proto.Marshal(ordered)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(udata, data) {
t.Fatalf("expected data to be marshaled in the same order, please sort fields before marshaling")
}
}