Add more options for Booleans

This commit is contained in:
Derek Collison
2016-11-21 15:06:15 -08:00
parent e57c949658
commit 6b307af1eb
3 changed files with 18 additions and 5 deletions

View File

@@ -684,8 +684,10 @@ func lexMapEnd(lx *lexer) stateFn {
// Checks if the unquoted string was actually a boolean
func (lx *lexer) isBool() bool {
str := lx.input[lx.start:lx.pos]
return str == "true" || str == "false" || str == "TRUE" || str == "FALSE"
str := strings.ToLower(lx.input[lx.start:lx.pos])
return str == "true" || str == "false" ||
str == "on" || str == "off" ||
str == "yes" || str == "no"
}
// Check if the unquoted string is a variable reference, starting with $.

View File

@@ -183,10 +183,10 @@ func (p *parser) processItem(it item) error {
}
p.setValue(num)
case itemBool:
switch it.val {
case "true":
switch strings.ToLower(it.val) {
case "true", "yes", "on":
p.setValue(true)
case "false":
case "false", "no", "off":
p.setValue(false)
default:
return fmt.Errorf("Expected boolean value, but got '%s'.", it.val)

View File

@@ -35,6 +35,17 @@ func TestSimpleTopLevel(t *testing.T) {
test(t, "foo='1'; bar=2.2; baz=true; boo=22", ex)
}
func TestBools(t *testing.T) {
ex := map[string]interface{}{
"foo": true,
}
test(t, "foo=true", ex)
test(t, "foo=TRUE", ex)
test(t, "foo=true", ex)
test(t, "foo=yes", ex)
test(t, "foo=on", ex)
}
var varSample = `
index = 22
foo = $index