Bool value

This commit is contained in:
Thomas Pelletier
2021-02-08 20:44:23 -05:00
parent 3488a91eff
commit 2790964270
2 changed files with 39 additions and 0 deletions
+18
View File
@@ -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)
+21
View File
@@ -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)
}