prysm-pulse/shared/ssz/example_and_test.go
Jie Hou d38f3be8b9 SSZ: Support More Types (#1079)
* Add uint32 and uint64 support

* Add sample usage and its test

* BUILD file fix

* Add period in go doc comments.
2018-12-14 12:35:32 -06:00

94 lines
2.0 KiB
Go

package ssz
import (
"bytes"
"io"
"reflect"
"testing"
)
type exampleStruct1 struct {
Field1 uint8
Field2 []byte
}
func (e *exampleStruct1) EncodeSSZ(w io.Writer) error {
// Need to pass value of struct for Encode function
// Later we can enhance the ssz implementation to support passing pointer, if necessary
return Encode(w, *e)
}
func (e *exampleStruct1) DecodeSSZ(r io.Reader) error {
// Need to pass pointer of struct for Decode function
return Decode(r, e)
}
type exampleStruct2 struct {
Field1 uint8 // a volatile, or host-specific field that doesn't need to be exported
Field2 []byte
}
// You can use a helper struct to only encode/decode custom fields of your struct
type exampleStruct2Export struct {
Field2 []byte
}
func (e *exampleStruct2) EncodeSSZ(w io.Writer) error {
return Encode(w, exampleStruct2Export{
e.Field2,
})
}
func (e *exampleStruct2) DecodeSSZ(r io.Reader) error {
ee := new(exampleStruct2Export)
if err := Decode(r, ee); err != nil {
return err
}
e.Field2 = ee.Field2
return nil
}
func TestEncodeDecodeExampleStruct1(t *testing.T) {
e1 := &exampleStruct1{
Field1: 10,
Field2: []byte{1, 2, 3, 4},
}
wBuf := new(bytes.Buffer)
if err := e1.EncodeSSZ(wBuf); err != nil {
t.Fatalf("failed to encode: %v", err)
}
encoding := wBuf.Bytes()
e2 := new(exampleStruct1)
rBuf := bytes.NewReader(encoding)
if err := e2.DecodeSSZ(rBuf); err != nil {
t.Fatalf("failed to decode: %v", err)
}
if !reflect.DeepEqual(*e1, *e2) {
t.Error("encode/decode algorithm don't match")
}
}
func TestEncodeDecodeExampleStruct2(t *testing.T) {
e1 := &exampleStruct2{
Field1: 10,
Field2: []byte{1, 2, 3, 4},
}
wBuf := new(bytes.Buffer)
if err := e1.EncodeSSZ(wBuf); err != nil {
t.Fatalf("failed to encode: %v", err)
}
encoding := wBuf.Bytes()
e2 := new(exampleStruct2)
rBuf := bytes.NewReader(encoding)
if err := e2.DecodeSSZ(rBuf); err != nil {
t.Fatalf("failed to decode: %v", err)
}
if !reflect.DeepEqual(e1.Field2, e2.Field2) {
t.Error("encode/decode algorithm don't match")
}
}