go-restruct / restruct

Rich binary (de)serialization library for Golang
https://restruct.io/
ISC License
360 stars 17 forks source link

`if=Field == 1` evaluates to false only if 'Field' has derived type #45

Open joonas-fi opened 3 years ago

joonas-fi commented 3 years ago

This test passes, i.e. it correctly unpacks both fields:

type Foo struct {
    HasOptional uint8

    Optional uint8 `struct:"if=HasOptional == 1"`
}

func TestFoobar(t *testing.T) {
    item := &Foo{}
    restruct.Unpack([]byte{1, 2}, binary.LittleEndian, item)
    assert.EqualString(t, fmt.Sprintf("%v", item), "&{1 2}")
}

But this does not:

type HasOptionalFlag uint8

type Foo struct {
    HasOptional HasOptionalFlag

    Optional uint8 `struct:"if=HasOptional == 1"`
}

I get exp=&{1 2}; got=&{1 0}

The if= seems to evaluate to false. Might be related to #43