diff --git a/unmarshal.go b/unmarshal.go index b38cef7..6e83f02 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -152,6 +152,21 @@ func (u *unmarshaler) StringValue(v []byte) { } } +func (u *unmarshaler) BoolValue(b bool) { + if u.err != nil { + return + } + + t := u.top() + if t.Type().Kind() == reflect.Slice { + s := reflect.ValueOf(b) + n := reflect.Append(t, s) + t.Set(n) + } else { + u.top().SetBool(b) + } +} + func (u *unmarshaler) SimpleKey(v []byte) { if u.err != nil { return @@ -197,6 +212,7 @@ type builder interface { KeyValEnd() StringValue(v []byte) + BoolValue(b bool) } type parser struct { @@ -381,11 +397,13 @@ func (p parser) parseVal(b []byte) ([]byte, error) { if !scanFollowsTrue(b) { return nil, fmt.Errorf("expected 'true'") } + p.builder.BoolValue(true) return b[4:], nil case 'f': if !scanFollowsFalse(b) { return nil, fmt.Errorf("expected 'false'") } + p.builder.BoolValue(false) return b[5:], nil case '[': return p.parseValArray(b) diff --git a/unmarshal_test.go b/unmarshal_test.go index 5e6bc68..af7a893 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -244,3 +244,24 @@ func TestArraySimple(t *testing.T) { require.NoError(t, err) assert.Equal(t, []string{"hello", "world"}, x.Foo) } + +func TestBool(t *testing.T) { + x := struct { + Truthy bool + Falsey bool + }{Falsey: true} + doc := `Truthy = true +Falsey = false` + err := toml.Unmarshal([]byte(doc), &x) + require.NoError(t, err) + assert.Equal(t, true, x.Truthy) + assert.Equal(t, false, x.Falsey) +} + +func TestBoolArray(t *testing.T) { + x := struct{ Bits []bool }{} + doc := `Bits = [true, false, true, true]` + err := toml.Unmarshal([]byte(doc), &x) + require.NoError(t, err) + assert.Equal(t, []bool{true, false, true, true}, x.Bits) +}