Bool value
This commit is contained in:
@@ -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) {
|
func (u *unmarshaler) SimpleKey(v []byte) {
|
||||||
if u.err != nil {
|
if u.err != nil {
|
||||||
return
|
return
|
||||||
@@ -197,6 +212,7 @@ type builder interface {
|
|||||||
KeyValEnd()
|
KeyValEnd()
|
||||||
|
|
||||||
StringValue(v []byte)
|
StringValue(v []byte)
|
||||||
|
BoolValue(b bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
type parser struct {
|
type parser struct {
|
||||||
@@ -381,11 +397,13 @@ func (p parser) parseVal(b []byte) ([]byte, error) {
|
|||||||
if !scanFollowsTrue(b) {
|
if !scanFollowsTrue(b) {
|
||||||
return nil, fmt.Errorf("expected 'true'")
|
return nil, fmt.Errorf("expected 'true'")
|
||||||
}
|
}
|
||||||
|
p.builder.BoolValue(true)
|
||||||
return b[4:], nil
|
return b[4:], nil
|
||||||
case 'f':
|
case 'f':
|
||||||
if !scanFollowsFalse(b) {
|
if !scanFollowsFalse(b) {
|
||||||
return nil, fmt.Errorf("expected 'false'")
|
return nil, fmt.Errorf("expected 'false'")
|
||||||
}
|
}
|
||||||
|
p.builder.BoolValue(false)
|
||||||
return b[5:], nil
|
return b[5:], nil
|
||||||
case '[':
|
case '[':
|
||||||
return p.parseValArray(b)
|
return p.parseValArray(b)
|
||||||
|
|||||||
@@ -244,3 +244,24 @@ func TestArraySimple(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, []string{"hello", "world"}, x.Foo)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user