Skip to content

Commit 9e07b8c

Browse files
committed
minor enhancement for map keys detecting
1 parent cbb056c commit 9e07b8c

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/main/scala/com/github/tminglei/bind/FrameworkUtils.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ object FrameworkUtils {
193193
// Computes the available indexes for the given key in this set of data.
194194
def indexes(name: String, data: Map[String, String]): Seq[Int] = {
195195
logger.debug(s"get indexes for $name")
196-
196+
// matches: 'prefix[index]...'
197197
val KeyPattern = ("^" + Pattern.quote(name) + """\[(\d+)\].*$""").r
198198
data.toSeq.collect { case (KeyPattern(index), _) => index.toInt }.sorted.distinct
199199
}
200200

201201
// Computes the available keys for the given prefix in this set of data.
202202
def keys(prefix: String, data: Map[String, String]): Seq[String] = {
203203
logger.debug(s"get keys for $prefix")
204-
205-
val KeyPattern = ("^" + Pattern.quote(prefix) + """\.("?[^."]+"?).*$""").r
204+
// matches: 'prefix.xxx...' | 'prefix."xxx.t"...'
205+
val KeyPattern = ("^" + Pattern.quote(prefix) + """\.("[^"]+"|[^.]+).*$""").r
206206
data.toSeq.collect { case (KeyPattern(key), _) => key }.distinct
207207
}
208208

src/main/scala/com/github/tminglei/bind/Mappings.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,16 @@ trait Mappings {
154154
logger.debug(s"map - converting $name")
155155
Map.empty ++ keys(name, data).map { key =>
156156
val keyName = if (isEmptyStr(name)) key else name + "." + key
157-
(keyBinding.convert(key, Map(key -> key)), valueBinding.convert(keyName, data))
157+
val unquotedKey = key.replaceAll("^\"?([^\"]+)\"?$", "$1")
158+
(keyBinding.convert(key, Map(key -> unquotedKey)), valueBinding.convert(keyName, data))
158159
}
159160
},
160161
moreValidate = (name, data, messages, theOptions) => {
161162
logger.debug(s"map - validating $name")
162163
keys(name, data).map { key =>
163164
val keyName = if (isEmptyStr(name)) key else name + "." + key
164-
keyBinding.validate(key, Map(key -> key), messages, theOptions).map {
165+
val unquotedKey = key.replaceAll("^\"?([^\"]+)\"?$", "$1")
166+
keyBinding.validate(key, Map(key -> unquotedKey), messages, theOptions).map {
165167
case (name, err) => (name, err)
166168
} ++ valueBinding.validate(keyName, data, messages, theOptions)
167169
}.flatten

src/test/scala/com/github/tminglei/bind/GeneralMappingsSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class GeneralMappingsSpec extends FunSpec with ShouldMatchers with Constraints {
240240
case Nil => {
241241
base.validate("map.aa", validData, messages, Options.apply()) should be (Nil)
242242
base.validate("map.\"b-1\"", validData, messages, Options.apply()) should be (Nil)
243-
map.convert("map", validData) should be (Map("aa" -> 122345, "\"b-1\"" -> 754))
243+
map.convert("map", validData) should be (Map("aa" -> 122345, "b-1" -> 754))
244244
}
245245
case err => err should be (Nil)
246246
}
@@ -260,7 +260,7 @@ class GeneralMappingsSpec extends FunSpec with ShouldMatchers with Constraints {
260260
case Nil => {
261261
base.validate("map.aa", emptyData, messages, Options.apply()) should be (Nil)
262262
base.validate("map.\"b-1\"", emptyData, messages, Options.apply()) should be (Nil)
263-
map.convert("map", emptyData) should be (Map("aa" -> 0, "\"b-1\"" -> 133))
263+
map.convert("map", emptyData) should be (Map("aa" -> 0, "b-1" -> 133))
264264
}
265265
case err => err should be (Nil)
266266
}

0 commit comments

Comments
 (0)