Created
January 17, 2017 21:47
-
-
Save anonymous/1654592c6c289dd47e4f79af5d52bac0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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