Guest User

Untitled

a guest
Dec 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. social {
  2. twitter {
  3. url="https://twitter.com"
  4. logo="images/twitter.png"
  5. }
  6. facebook {
  7. url="https://www.facebook.com"
  8. logo="images/facebook.png"
  9. }
  10. }
  11.  
  12. <table border="0" cellspacing="0" cellpadding="2"><tr>
  13. @configuration.getConfig("social").map { config =>
  14. @for(item <- config.entrySet) {
  15. <td><a href="item.getString("url")">
  16. <img src="@routes.Assets.at("item.getString("logo")").absoluteURL()" width="24" height="24"/></a></td>
  17. }
  18. }
  19. </table>
  20.  
  21. "social" : [
  22. {
  23. name="twitter"
  24. url="https://twitter.com",
  25. logo="images/twitter.png"
  26. },
  27. {
  28. name="facebook"
  29. url="https://www.facebook.com",
  30. logo="images/facebook.png"
  31. }
  32. ]
  33.  
  34. @(message: String)(implicit request: RequestHeader)
  35. @import play.api.Play.current
  36.  
  37. <table border="0" cellspacing="0" cellpadding="2"><tr>
  38. @current.configuration.getConfigList("social").get.map { config =>
  39. <td><a href="@config.getString("url")">
  40. <img src="@routes.Assets.at(config.getString("logo").get).absoluteURL()" width="24" height="24"/></a></td>
  41. }
  42. </table>
  43.  
  44. import collection.JavaConversions._
  45. val socialConfig = ConfigFactory.load.getConfig("social")
  46. socialConfig.root.map { case (name: String, configObject: ConfigObject) =>
  47. val config = configObject.toConfig
  48. println(config.getString("url"))
  49. println(config.getString("logo"))
  50. }
  51.  
  52. ConfigList socials = ConfigFactory().load.getList("social")
  53.  
  54. for (ConfigValue cv : socials) {
  55. Config c = ((ConfigObject)cv).toConfig();
  56. System.out.println(c.getString("url"));
  57. System.out.println(c.getString("logo"));
  58. }
  59.  
  60. private val firstSegmentRE = """^(w+)[.*].*$""".r
  61.  
  62. // convert "aaa.bbb.ccc" to "aaa"
  63. private def parseFirstSegment(fullPath: String) : Option[String] = {
  64. if (fullPath.contains("."))
  65. fullPath match {
  66. case firstSegmentRE(segment) => Some(segment)
  67. case _ => None
  68. }
  69. else
  70. Some(fullPath)
  71. }
  72.  
  73. // for all keys in white list get a map of key -> config
  74. protected def subConfigMap(config: Config, whiteList: List[String],
  75. configName: String) : ErrorOr[Map[String, Config]] = {
  76. // This will traverse the whole config and flatten down to the leaves..
  77. val leafKeys : List[String] =
  78. config.entrySet()
  79. .asScala
  80. .map(e => e.getKey)
  81. .toList
  82. // Remove all after the first dot
  83. val nextLevelKeys : List[String] =
  84. leafKeys.map(parseFirstSegment)
  85. .collect {
  86. case Some(firstSegment) => firstSegment
  87. }
  88. .distinct
  89. val keysToSearch = nextLevelKeys.filter(whiteList.contains)
  90. // we have a list of valid first level children
  91. // parse out subconfigs and convert to map
  92. keysToSearch.traverseErrorOr( key =>
  93. extractSubConfig(config, key, configName).map((key, _))
  94. )
  95. .map(_.toMap)
  96. }
Add Comment
Please, Sign In to add comment