tendermint/go-amino

pointers to false round-trip as nil

Closed this issue · 1 comments

Description

We're using go-amino in cosmos. We have pointers to boolean values in our message structs in order to be able to differentiate whether a field was omitted or set to false.

However, when a pointer to false is marshalled then unmarshalled (binary only, the JSON methods work), the resulting value is nil.

Test program

Source

package main

import (
	"fmt"
	"github.com/tendermint/go-amino"
)

func main() {
	var cdc = amino.NewCodec()

	type SimpleStruct struct {
		BoolPtrTrue  *bool
		BoolPtrFalse *bool
	}

	t := true
	f := false

	s := SimpleStruct{
		BoolPtrTrue:  &t,
		BoolPtrFalse: &f,
	}

	b, _ := cdc.MarshalBinaryBare(s)
	
	var s2 SimpleStruct
	cdc.UnmarshalBinaryBare(b, &s2)
	
	fmt.Println(s2.BoolPtrTrue)
	fmt.Println(*s2.BoolPtrTrue)
	fmt.Println(s2.BoolPtrFalse)
	fmt.Println(*s2.BoolPtrFalse)
}

Output

0x4141ca
true
<nil>
panic: runtime error: invalid memory address or nil pointer dereference

Playground link

https://play.golang.org/p/x05ph3bq2zd

Thank you!

closed with #290