Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from anonymous/MyPersonalHell.scala
Created January 17, 2017 21:51
Show Gist options
  • Save Jacoby6000/66bde0e4aca05a6ca4dfaee5495a45ec to your computer and use it in GitHub Desktop.
Save Jacoby6000/66bde0e4aca05a6ca4dfaee5495a45ec to your computer and use it in GitHub Desktop.
class IndexProcess private (config: Config) {
// e.g.
// index {
// entities {
// slide {
// products: {to: many, is: product}
// sites: {to: many, is: site}
// }
// site: {
// architects: {to: many, is: architect}
// engineers: {to: many, is: engineer}
// }
// product: {}
// architect: {}
// engineer: {}
// }
// }
val entities: Config = config.getConfig("entities")
// Flatten a configuration, extracting child objects.
// Config(...) => Seq(child1, child2, ...original - (child1..child2))
def normalize(config: Config, as: String): List[Config] = {
config.root.keySet.asScala filter { key =>
entities.hasPath(joinPath(as, key))
} match {
case Map.empty => List.apply(config)
case relKeys => {
val parent: Config = relKeys.foldRight(config) {
(key, config) => config.withoutPath(key)
}
val children: List[Config] = relKeys.flatMap {
(key) => normalize(
config.getConfig(key),
entities.getString(joinPath(as, key, "is"))
)
}.toList
children :+ parent
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment