Esempio n. 1
0
// This example demonstrates how to use Marshal to automatically XDR encode
// data using reflection.
func ExampleMarshal() {
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}

	// Sample image header data.
	h := ImageHeader{[3]byte{0xAB, 0xCD, 0xEF}, 2, true, 10}

	// Use marshal to automatically determine the appropriate underlying XDR
	// types and encode.
	encodedData, err := xdr.Marshal(&h)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("encodedData:", encodedData)

	// Output:
	// encodedData: [171 205 239 0 0 0 0 2 0 0 0 1 0 0 0 10]
}
Esempio n. 2
0
func BenchmarkReflMarshal(b *testing.B) {
	var err error
	for i := 0; i < b.N; i++ {
		res, err = refl.Marshal(s)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Esempio n. 3
0
func TestCompareMarshals(t *testing.T) {
	e0 := s.MarshalXDR()
	e1, err := refl.Marshal(s)
	if err != nil {
		t.Fatal(err)
	}
	if bytes.Compare(e0, e1) != 0 {
		t.Fatalf("Encoding mismatch;\n\t%x (this)\n\t%x (refl)", e0, e1)
	}
}
Esempio n. 4
0
func BenchmarkMarshal(b *testing.B) {
	b.StopTimer()
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}
	h := ImageHeader{[3]byte{0xAB, 0xCD, 0xEF}, 2, true, 10}
	size := unsafe.Sizeof(h)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_, _ = xdr.Marshal(&h)
	}
	b.SetBytes(int64(size))
}
func (x XdrSerializer) Marshal(o interface{}) []byte {
	d, _ := xdr.Marshal(o)
	return d
}