Skip to content

Commit 92ca4d6

Browse files
authored
Merge pull request #185 from japgolly/topic/filter_by_key_and_source
Add ability to filter report tables by key & source
2 parents a3241a1 + f33ac51 commit 92ca4d6

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### 3.3.0
2+
3+
* When calling `report.map{Used,Unused}`:
4+
* Added `filterKeysAndSource(f: (String, SourceName) => Boolean)`
5+
* Added `filterKeysAndSourceNot(f: (String, SourceName) => Boolean)`
6+
* Added `sources: SourceName*` to `filterKeys` to only filter keys for a given set of sources
7+
* Added `sources: SourceName*` to `filterKeysNot` to only filter-not keys for a given set of sources
8+
* Added `withoutKeys(keys: Set[String], sources: SourceName*)` to remove keys for a given set of sources, or all if none specified
9+
110
### 3.2.0
211

312
* [#133](https://github.com/japgolly/clear-config/issues/133) Add `ConfigDef.logbackXmlOnClasspath` (available on JVM only) that scans logback xml files for environment variables so that they can appear in a config report.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ val (dbCfg, report) =
116116

117117
println(report
118118

119-
// Remove env & system columns from the unused section of the report
120-
.mapUnused(_.withoutSources(ConfigSourceName.environment, ConfigSourceName.system))
119+
// Only show unused env vars and properties that start with POSTGRES
120+
// (All unused keys in database.props will remain unfiltered in the report)
121+
.mapUnused(_.filterKeys(_.startsWith("POSTGRES"), ConfigSourceName.environment, ConfigSourceName.system))
121122

122123
// Show the full report
123124
.full

core/shared/src/main/scala/japgolly/clearconfig/internals/Report.scala

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,28 @@ object Report {
4545
trait HasTable[A <: HasTable[A]] {
4646
def map(f: Table => Table): A
4747

48-
final def filterKeys(f: String => Boolean): A =
49-
map(_.modByKey(_.iterator.filter(kv => f(kv._1.value)).toMap))
48+
final def filterKeysAndSource(f: (String, SourceName) => Boolean): A =
49+
map(_.modByKey(_.iterator.map { case (k, m) => (k, m.filter { case (s, _) => f(k.value, s) }) }.toMap))
5050

51-
final def filterKeysNot(f: String => Boolean): A =
52-
filterKeys(!f(_))
51+
final def filterKeysAndSourceNot(f: (String, SourceName) => Boolean): A =
52+
filterKeysAndSource(!f(_, _))
5353

54-
final def withoutKeys(keys: String*): A = {
55-
val keySet = keys.toSet
56-
filterKeys(!keySet.contains(_))
57-
}
54+
final def filterKeys(f: String => Boolean, sources: SourceName*): A =
55+
if (sources.isEmpty)
56+
map(_.modByKey(_.iterator.filter(kv => f(kv._1.value)).toMap))
57+
else {
58+
val sourceSet = sources.toSet
59+
filterKeysAndSource((k, s) => if (sourceSet.contains(s)) f(k) else true).withoutEmptyKeyRows
60+
}
61+
62+
final def filterKeysNot(f: String => Boolean, sources: SourceName*): A =
63+
filterKeys(!f(_), sources: _*)
64+
65+
final def withoutKeys(keys: String*): A =
66+
withoutKeys(keys.toSet)
67+
68+
final def withoutKeys(keys: Set[String], sources: SourceName*): A =
69+
filterKeys(!keys.contains(_), sources: _*)
5870

5971
final def filterSources(f: SourceName => Boolean): A =
6072
map(_.modBySource(_.iterator.filter(kv => f(kv._1)).toMap))

core/shared/src/test/scala/japgolly/clearconfig/external/ConfigReportTest.scala

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,63 @@ object ConfigReportTest extends TestSuite {
364364
!Unused keys (0):
365365
!No data to report.
366366
""".stripMargin('!').trim)
367+
368+
"filterKeysAndSourceNot" - {
369+
val r2 = r.mapUnused(_.filterKeysAndSourceNot(_.startsWith("s") && _ == s1.name))
370+
assertMultiline(r2.unused,
371+
s"""
372+
!Unused keys (3):
373+
!+-----+----+----+
374+
!| Key | S1 | S2 |
375+
!+-----+----+----+
376+
!| a | a1 | |
377+
!| b | | b2 |
378+
!| s | | s2 |
379+
!+-----+----+----+
380+
""".stripMargin('!').trim)
381+
}
382+
383+
"filterKeysNotWithSources" - {
384+
val r2 = r.mapUnused(_.filterKeysNot(_.startsWith("s"), s1.name))
385+
assertMultiline(r2.unused,
386+
s"""
387+
!Unused keys (3):
388+
!+-----+----+----+
389+
!| Key | S1 | S2 |
390+
!+-----+----+----+
391+
!| a | a1 | |
392+
!| b | | b2 |
393+
!| s | | s2 |
394+
!+-----+----+----+
395+
""".stripMargin('!').trim)
396+
}
397+
398+
"filterKeysWithSources1" - {
399+
val r2 = r.mapUnused(_.filterKeys(_.startsWith("s"), s1.name))
400+
assertMultiline(r2.unused,
401+
s"""
402+
!Unused keys (2):
403+
!+-----+----+----+
404+
!| Key | S1 | S2 |
405+
!+-----+----+----+
406+
!| b | | b2 |
407+
!| s | s1 | s2 |
408+
!+-----+----+----+
409+
""".stripMargin('!').trim)
410+
}
411+
412+
"filterKeysWithSources2" - {
413+
val r2 = r.mapUnused(_.filterKeys(_.startsWith("s"), s1.name, s2.name))
414+
assertMultiline(r2.unused,
415+
s"""
416+
!Unused keys (1):
417+
!+-----+----+----+
418+
!| Key | S1 | S2 |
419+
!+-----+----+----+
420+
!| s | s1 | s2 |
421+
!+-----+----+----+
422+
""".stripMargin('!').trim)
423+
}
367424
}
368425

369426
"colour" - {

0 commit comments

Comments
 (0)