Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save philipstanislaus/506d7ed40f5424caeb3f861786f0803c to your computer and use it in GitHub Desktop.
Save philipstanislaus/506d7ed40f5424caeb3f861786f0803c to your computer and use it in GitHub Desktop.

Revisions

  1. philipstanislaus created this gist Sep 3, 2019.
    62 changes: 62 additions & 0 deletions optional_values_with_empty_field_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    package playground_optional_values_with_nil_pointers

    import (
    "fmt"
    "testing"

    "github.com/ChainSafe/gossamer/codec"
    )

    // Bool represents boolean values
    type Bool struct {
    Value bool
    Empty bool
    }

    // NewBool creates a new Bool
    func NewBool(v bool) Bool {
    return Bool{v, false}
    }

    func NewEmptyBool() Bool {
    return Bool{false, true}
    }

    func (b *Bool) Encode() ([]byte, error) {
    return codec.Encode(b.Value)
    }

    func (b *Bool) String() string {
    if b.Empty {
    return fmt.Sprintf("nil")
    }
    return fmt.Sprintf("%t", b.Value)
    }

    func (b *Bool) Get() (empty bool, value bool) {
    return b.Empty, b.Value
    }

    func (b *Bool) Set(v bool) {
    b.Value = v
    b.Empty = false
    }

    func (b *Bool) SetEmpty() {
    b.Value = false
    b.Empty = true
    }

    func TestNo3(t *testing.T) {
    b := NewBool(true)
    c := NewEmptyBool()

    fmt.Println(b.String())
    fmt.Println(c.String())

    b.SetEmpty()
    c.Set(false)

    fmt.Println(b.String())
    fmt.Println(c.String())
    }