From 20b75542e4e8aba4f7f93f8223e2c67a1c9a9bdb Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Wed, 16 Nov 2011 10:34:40 -0500 Subject: [PATCH] support "on" and "off" when converting string to boolean --- HOCON.md | 10 +++++----- .../typesafe/config/impl/DefaultTransformer.java | 5 +++-- .../com/typesafe/config/impl/ConfigValueTest.scala | 13 +++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/HOCON.md b/HOCON.md index 17394286..10114487 100644 --- a/HOCON.md +++ b/HOCON.md @@ -652,11 +652,11 @@ implementation should attempt to convert types as follows: representation that would be a valid number in JSON. - boolean to string: should become the string "true" or "false" - string to number: parse the number with the JSON rules - - string to boolean: the strings "true", "yes", "false", "no" - should be converted to boolean values. It's tempting to - support a long list of other ways to write a boolean, but - for interoperability and keeping it simple, it's recommended to - stick to these four. + - string to boolean: the strings "true", "yes", "on", "false", + "no", "off" should be converted to boolean values. It's + tempting to support a long list of other ways to write a + boolean, but for interoperability and keeping it simple, it's + recommended to stick to these six. - string to null: the string `"null"` should be converted to a null value if the application specifically asks for a null value, though there's probably no reason an app would do this. diff --git a/src/main/java/com/typesafe/config/impl/DefaultTransformer.java b/src/main/java/com/typesafe/config/impl/DefaultTransformer.java index e3657520..62913a1c 100644 --- a/src/main/java/com/typesafe/config/impl/DefaultTransformer.java +++ b/src/main/java/com/typesafe/config/impl/DefaultTransformer.java @@ -31,9 +31,10 @@ final class DefaultTransformer { return new ConfigNull(value.origin()); break; case BOOLEAN: - if (s.equals("true") || s.equals("yes")) { + if (s.equals("true") || s.equals("yes") || s.equals("on")) { return new ConfigBoolean(value.origin(), true); - } else if (s.equals("false") || s.equals("no")) { + } else if (s.equals("false") || s.equals("no") + || s.equals("off")) { return new ConfigBoolean(value.origin(), false); } break; diff --git a/src/test/scala/com/typesafe/config/impl/ConfigValueTest.scala b/src/test/scala/com/typesafe/config/impl/ConfigValueTest.scala index b86bb4a4..d4d482a0 100644 --- a/src/test/scala/com/typesafe/config/impl/ConfigValueTest.scala +++ b/src/test/scala/com/typesafe/config/impl/ConfigValueTest.scala @@ -422,4 +422,17 @@ class ConfigValueTest extends TestUtils { assertEquals(Int.MaxValue + 1L, nD(Int.MaxValue + 1.0).unwrapped()) assertEquals(Int.MinValue - 1L, nD(Int.MinValue - 1.0).unwrapped()) } + + @Test + def automaticBooleanConversions() { + val trues = parseObject("{ a=true, b=yes, c=on }") + assertEquals(true, trues.getBoolean("a")) + assertEquals(true, trues.getBoolean("b")) + assertEquals(true, trues.getBoolean("c")) + + val falses = parseObject("{ a=false, b=no, c=off }") + assertEquals(false, falses.getBoolean("a")) + assertEquals(false, falses.getBoolean("b")) + assertEquals(false, falses.getBoolean("c")) + } }