Skip to content

Commit

Permalink
Add more options for Booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Nov 21, 2016
1 parent e57c949 commit 6b307af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions conf/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 $.
Expand Down
6 changes: 3 additions & 3 deletions conf/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions conf/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b307af

Please sign in to comment.