Skip to content

Instantly share code, notes, and snippets.

Created January 17, 2017 21:47
Show Gist options
  • Save anonymous/1654592c6c289dd47e4f79af5d52bac0 to your computer and use it in GitHub Desktop.
Save anonymous/1654592c6c289dd47e4f79af5d52bac0 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