Permalink
Browse files

Fixed issue #12

  • Loading branch information...
1 parent 19bb22d commit a17f0b0367a27c8c624d8bb20c3d6e482541853e JLF committed May 6, 2012
Showing with 64 additions and 4 deletions.
  1. +7 −0 CHANGELOG.md
  2. +5 −4 core/src/Configuration.scala
  3. +35 −0 core/src/io/Utils.scala
  4. +17 −0 core/test/ConfigurationSpec.scala
View
@@ -1,6 +1,13 @@
Changelog
=========
+Next Version
+------------
+
+### Bug fix
+
+ - List values are sanitized when added to a configuration (issue #12).
+
Version 0.10.0 - 2012-02-12
---------------------------
@@ -1,5 +1,5 @@
/*
- Copyright (C) 2011, Paradigmatic <paradigmatic@streum.org>
+ Copyright (C) 2011-2012, Paradigmatic <paradigmatic@streum.org>
This file is part of Configrity.
@@ -73,7 +73,7 @@ case class Configuration( data: Map[String,String] ) {
* previous value is replaced.
*/
def set[A]( key: String, as: List[A] ) = {
- val str = as.mkString( "[", ",", "]" )
+ val str = io.Utils.sanitize(as).mkString( "[", ",", "]" )
Configuration( data + ( key -> str ) )
}
@@ -179,8 +179,9 @@ object Configuration {
Configuration(
entries.map {
case (k,v) => v match {
- case l: List[_] => (k, l.mkString("[",",","]") )
- case _ => (k,v.toString)
+ case l: List[_] =>
+ (k, io.Utils.sanitize(l).mkString("[",",","]") )
+ case _ => (k,v.toString)
}
}.toMap
)
View
@@ -0,0 +1,35 @@
+/*
+ Copyright (C) 2012, Paradigmatic <paradigmatic@streum.org>
+
+ This file is part of Configrity.
+
+ Configrity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Configrity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with Configrity. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package org.streum.configrity.io
+
+object Utils {
+
+ val Alphanum = """\w+""".r
+
+ def sanitize( in: String ): String = in match {
+ case Alphanum(out) => out
+ case out => "\"" + out + "\""
+ }
+
+ def sanitize[A]( as: List[A] ): List[String] =
+ as.map( a => sanitize( a.toString ) )
+
+
+}
@@ -79,6 +79,14 @@ class ConfigurationSpec extends FlatSpec with ShouldMatchers with DefaultConvert
lst should be (lst2)
}
+ it should "format lists with empty spaces in values correctly" in {
+ val lst = List( "hello world" )
+ val c2 = config.set( "list", lst )
+ val lst2 = c2[List[String]]( "list" )
+ lst should be (lst2)
+ }
+
+
it can "be nicely formatted" in {
val out = new ExportFormat {
def toText( c: Configuration ) = "FOOBAR"
@@ -174,6 +182,15 @@ class ConfigurationObjectSpec extends FlatSpec with ShouldMatchers with io.IOHel
config[List[String]]("bar") should be (List("hello","world"))
}
+ it can "support lists directly with empty spaces in values" in {
+ val config = Configuration(
+ "foo"-> List(10),
+ "bar"-> ("hello world"::Nil)
+ )
+ config[List[Int]]("foo") should be (List(10))
+ config[List[String]]("bar") should be (List("hello world"))
+ }
+
it can "be created from a string using a given format" in {
val s =

0 comments on commit a17f0b0

Please sign in to comment.