• Felix Lange's avatar
    rlp: improve nil pointer handling (#20064) · 96fb8391
    Felix Lange authored
    * rlp: improve nil pointer handling
    
    In both encoder and decoder, the rules for encoding nil pointers were a
    bit hard to understand, and didn't leave much choice. Since RLP allows
    two empty values (empty list, empty string), any protocol built on RLP
    must choose either of these values to represent the null value in a
    certain context.
    
    This change adds choice in the form of two new struct tags, "nilString"
    and "nilList". These can be used to specify how a nil pointer value is
    encoded. The "nil" tag still exists, but its implementation is now
    explicit and defines exactly how nil pointers are handled in a single
    place.
    
    Another important change in this commit is how nil pointers and the
    Encoder interface interact. The EncodeRLP method was previously called
    even on nil values, which was supposed to give users a choice of how
    their value would be handled when nil. It turns out this is a stupid
    idea. If you create a network protocol containing an object defined in
    another package, it's better to be able to say that the object should be
    a list or string when nil in the definition of the protocol message
    rather than defining the encoding of nil on the object itself.
    
    As of this commit, the encoding rules for pointers now take precedence
    over the Encoder interface rule. I think the "nil" tag will work fine
    for most cases. For special kinds of objects which are a struct in Go
    but strings in RLP, code using the object can specify the desired
    encoding of nil using the "nilString" and "nilList" tags.
    
    * rlp: propagate struct field type errors
    
    If a struct contained fields of undecodable type, the encoder and
    decoder would panic instead of returning an error. Fix this by
    propagating type errors in makeStruct{Writer,Decoder} and add a test.
    Unverified
    96fb8391
encoder_example_test.go 1.33 KB
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package rlp

import (
	"fmt"
	"io"
)

type MyCoolType struct {
	Name string
	a, b uint
}

// EncodeRLP writes x as RLP list [a, b] that omits the Name field.
func (x *MyCoolType) EncodeRLP(w io.Writer) (err error) {
	return Encode(w, []uint{x.a, x.b})
}

func ExampleEncoder() {
	var t *MyCoolType // t is nil pointer to MyCoolType
	bytes, _ := EncodeToBytes(t)
	fmt.Printf("%v → %X\n", t, bytes)

	t = &MyCoolType{Name: "foobar", a: 5, b: 6}
	bytes, _ = EncodeToBytes(t)
	fmt.Printf("%v → %X\n", t, bytes)

	// Output:
	// <nil> → C0
	// &{foobar 5 6} → C20506
}