Skip to content

Commit 4b5f672

Browse files
committed
fix for handling colons in keys
1 parent 16c3a23 commit 4b5f672

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main/scala/args4c/RichConfigOps.scala

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,37 @@ trait RichConfigOps extends Dynamic with LowPriorityArgs4cImplicits {
8181
* @return a configuration with the given user-argument overrides applied over top
8282
*/
8383
def withUserArgs(args: Array[String], unrecognizedArg: String => Config = ParseArg.Throw): Config = {
84-
def isSimpleList(key: String) = {
85-
def isList = Try(config.getStringList(key)).isSuccess
84+
def isValidKey(key: String): Boolean = {
85+
try {
86+
config.hasPath(key)
87+
} catch {
88+
case _: ConfigException => false
89+
}
90+
}
8691

92+
def isSimpleList(key: String): Boolean = {
93+
def isList = Try(config.getStringList(key)).isSuccess
8794
config.hasPath(key) && isList
8895
}
8996

9097
def isObjectList(key: String) = {
9198
def isList = Try(config.getObjectList(key)).isSuccess
92-
9399
config.hasPath(key) && isList
94100
}
95101

96102
val configs: Array[Config] = args.map {
97-
case KeyValue(k, v) if isSimpleList(k) =>
103+
case KeyValue(k, v) if isValidKey(k) && isSimpleList(k) =>
98104
asConfig(k, java.util.Arrays.asList(v.split(",", -1): _*))
99-
case KeyValue(k, v) if isObjectList(k) =>
105+
case KeyValue(k, v) if isValidKey(k) && isObjectList(k) =>
100106
sys.error(s"Path '$k' tried to override an object list with '$v'")
101-
case KeyValue(k, v) => asConfig(k, v)
102-
case FilePathConfig(c) => c
103-
case UrlPathConfig(c) => c
104-
case other => unrecognizedArg(other)
107+
case KeyValue(k, v) if isValidKey(k) => asConfig(k, v)
108+
case FilePathConfig(c) => c
109+
case UrlPathConfig(c) => c
110+
case other @ KeyValue(k, v) =>
111+
val safeKey = ConfigUtil.quoteString(k)
112+
asConfig(safeKey, v)
113+
case other =>
114+
unrecognizedArg(other)
105115
}
106116

107117
(configs :+ config).reduce(_ withFallback _)

src/test/scala/args4c/Args4cPackageTest.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.nio.file.{Files, Paths}
44
import java.util.UUID
55

66
import args4c.RichConfig.ParseArg
7-
import com.typesafe.config.ConfigFactory
7+
import com.typesafe.config.{Config, ConfigFactory}
88

99
class Args4cPackageTest extends BaseSpec {
1010

@@ -16,6 +16,13 @@ class Args4cPackageTest extends BaseSpec {
1616
}
1717
}
1818

19+
"args4c" should {
20+
"handled colons in keys" in {
21+
val config: Config = configForArgs(Array("affinity:container=hello"))
22+
config.getString("\"affinity:container\"") shouldBe "hello"
23+
}
24+
}
25+
1926
"unquote" should {
2027
List(
2128
"foo" -> "foo",

0 commit comments

Comments
 (0)