Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mirrors
biopet.biopet
Commits
6ae5046c
Commit
6ae5046c
authored
May 18, 2016
by
bow
Browse files
Merge branch 'feature-check_unused_keys' into 'develop'
Added check on unused config values Fixes #327 See merge request !400
parents
4df69c64
8b93a99d
Changes
3
Hide whitespace changes
Inline
Side-by-side
biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/ConfigUtils.scala
View file @
6ae5046c
...
...
@@ -30,6 +30,34 @@ import scala.collection.JavaConversions._
*
*/
object
ConfigUtils
extends
Logging
{
/**
* This method give back all nested values that does exist in map1 but not in map2
*
* @param map1 input map
* @param map2 input map
* @return Uniqe map1
*/
def
uniqueKeys
(
map1
:
Map
[
String
,
Any
],
map2
:
Map
[
String
,
Any
])
:
Map
[
String
,
Any
]
=
{
filterEmtpyMapValues
(
map1
.
flatMap
{
case
(
key
,
value
:
Map
[
_
,
_
])
=>
Some
(
key
->
uniqueKeys
(
value
.
asInstanceOf
[
Map
[
String
,
Any
]],
map2
.
getOrElse
(
key
,
Map
()).
asInstanceOf
[
Map
[
String
,
Any
]]))
case
(
key
,
value
)
if
!
map2
.
contains
(
key
)
=>
Some
(
key
->
value
)
case
_
=>
None
})
}
/**
* Filter values that are a map but are empty
* @param map input map
* @return output map
*/
def
filterEmtpyMapValues
(
map
:
Map
[
String
,
Any
])
:
Map
[
String
,
Any
]
=
{
map
.
filter
{
case
(
key
,
value
:
Map
[
_
,
_
])
=>
value
.
nonEmpty
case
_
=>
true
}
}
/**
* Merge 2 maps, when value is in a map in map1 and map2 the value calls recursively this function
* @param map1 Prio over map2
...
...
biopet-utils/src/main/scala/nl/lumc/sasc/biopet/utils/config/Config.scala
View file @
6ae5046c
...
...
@@ -215,6 +215,17 @@ class Config(protected var _map: Map[String, Any],
// Positions where values are found
val
found
=
convertIndexValuesToMap
(
foundCache
.
filter
(!
_
.
_2
.
default
).
toList
.
map
(
x
=>
(
x
.
_2
.
foundIndex
,
x
.
_2
.
value
)))
val
fixed
=
convertIndexValuesToMap
(
fixedCache
.
filter
(!
_
.
_2
.
default
).
toList
.
map
(
x
=>
(
x
.
_2
.
foundIndex
,
x
.
_2
.
value
)))
val
unused
=
uniqueKeys
(
map
,
found
)
def
reportUnused
(
map
:
Map
[
String
,
Any
],
path
:
List
[
String
]
=
Nil
)
:
Unit
=
{
map
.
foreach
{
case
(
key
,
value
:
Map
[
_
,
_
])
=>
reportUnused
(
value
.
asInstanceOf
[
Map
[
String
,
Any
]],
path
:+
key
)
case
(
key
,
value
)
=>
logger
.
warn
(
s
"config key in user config is never used, key: $key"
+
(
if
(
path
.
nonEmpty
)
s
", path: ${path.mkString("
->
")}"
else
""
))
}
}
reportUnused
(
unused
)
// Positions where to start searching
val
effectiveFound
=
convertIndexValuesToMap
(
foundCache
.
filter
(!
_
.
_2
.
default
).
toList
.
map
(
x
=>
(
x
.
_2
.
requestIndex
,
x
.
_2
.
value
)),
Some
(
false
))
...
...
@@ -226,7 +237,8 @@ class Config(protected var _map: Map[String, Any],
val
fullEffective
=
ConfigUtils
.
mergeMaps
(
effectiveFound
,
effectiveDefaultFound
)
val
fullEffectiveWithNotFound
=
ConfigUtils
.
mergeMaps
(
fullEffective
,
notFound
)
writeMapToJsonFile
(
_map
,
"input"
)
writeMapToJsonFile
(
map
,
"input"
)
writeMapToJsonFile
(
unused
,
"unused"
)
writeMapToJsonFile
(
_defaults
,
"defaults"
)
writeMapToJsonFile
(
found
,
"found"
)
writeMapToJsonFile
(
fixed
,
"fixed"
)
...
...
@@ -238,7 +250,7 @@ class Config(protected var _map: Map[String, Any],
writeMapToJsonFile
(
fullEffectiveWithNotFound
,
"effective.full.notfound"
)
}
override
def
toString
:
String
=
_
map
.
toString
()
override
def
toString
:
String
=
map
.
toString
()
}
object
Config
extends
Logging
{
...
...
biopet-utils/src/test/scala/nl/lumc/sasc/biopet/utils/ConfigUtilsTest.scala
View file @
6ae5046c
...
...
@@ -223,6 +223,21 @@ class ConfigUtilsTest extends TestNGSuite with Matchers {
mergeMaps
(
map2
,
map1
,
(
a
,
b
,
k
)
=>
a
.
toString
+
b
.
toString
)
shouldBe
Map
(
"c"
->
Map
(
"x"
->
"21"
))
mergeMaps
(
map2
,
map2
,
(
a
,
b
,
k
)
=>
a
.
toString
+
b
.
toString
)
shouldBe
Map
(
"c"
->
Map
(
"x"
->
"22"
))
}
@Test
def
testFilterEmtpyMapValues
:
Unit
=
{
ConfigUtils
.
filterEmtpyMapValues
(
Map
(
"bla"
->
"bla"
))
shouldBe
Map
(
"bla"
->
"bla"
)
ConfigUtils
.
filterEmtpyMapValues
(
Map
(
"bla"
->
Map
()))
shouldBe
Map
()
ConfigUtils
.
filterEmtpyMapValues
(
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
)))
shouldBe
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
))
ConfigUtils
.
filterEmtpyMapValues
(
Map
(
"bla"
->
Map
(
"bla"
->
Map
())))
shouldBe
Map
(
"bla"
->
Map
(
"bla"
->
Map
()))
ConfigUtils
.
filterEmtpyMapValues
(
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
),
"bla2"
->
"bla"
))
shouldBe
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
),
"bla2"
->
"bla"
)
}
@Test
def
testUniqeKeys
:
Unit
=
{
ConfigUtils
.
uniqueKeys
(
Map
(
"bla"
->
"bla"
),
Map
(
"bla"
->
"bla"
))
shouldBe
Map
()
ConfigUtils
.
uniqueKeys
(
Map
(
"bla"
->
"bla"
),
Map
())
shouldBe
Map
(
"bla"
->
"bla"
)
ConfigUtils
.
uniqueKeys
(
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
)),
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
)))
shouldBe
Map
()
ConfigUtils
.
uniqueKeys
(
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
)),
Map
(
"bla"
->
Map
()))
shouldBe
Map
(
"bla"
->
Map
(
"bla"
->
"bla"
))
}
}
object
ConfigUtilsTest
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment