As a brief note today, this post includes an example that uses Scala 3, scala-cli
, and a Typesafe HOCON configuration file. As a bit of an aside, I’m using Scala code like this to create “video” tags for the HTML files in my Scala 3 and ZIO 2 video training courses.
First, here’s the HOCON configuration file, which I named videos.conf:
videos = [ { uri = "/video-course/functional-programming-2/a-small-zio-2-example-application/" name = "Video: Creating A Small Application with ZIO 2" description = "This video shows the basics of how to create a small application with ZIO 2 and Scala 3." thumbnailUrl = "https://alvinalexander.com/videos/courses/functional-programming-in-depth/240-ZIO-zMakeInt-Example.jpg" contentUrl = "https://alvinalexander.com/videos/courses/functional-programming-in-depth/240-ZIO-zMakeInt-Example.mp4" uploadDate = "2024-03-21T08:00:00+08:00" }, { uri = "/video-course/advanced-scala-3/higher-order-functions-2/" name = "Video: Higher-Order Functions in Scala, Part 2" description = "A video on how to create and use higher-order functions in Scala 3, including functions like filter and map." thumbnailUrl = "https://alvinalexander.com/videos/courses/advanced-scala-3/001--Scala-HOFs--Part2-filter.jpg" contentUrl = "https://alvinalexander.com/videos/courses/advanced-scala-3/001--Scala-HOFs--Part2-filter.mp4" uploadDate = "2024-03-21T08:00:00+08:00" } // Add more video objects here... ]
Second, here’s the Scala 3 and scala-cli
file I use to read that HOCON file:
//> using scala "3" //> using dep "com.typesafe:config:1.4.3" import com.typesafe.config.{Config, ConfigFactory} import scala.jdk.CollectionConverters.* case class Video( uri: String, name: String, description: String, thumbnailUrl: String, contentUrl: String, uploadDate: String ) @main def ReadVideosConfiguration: Unit = val config: Config = ConfigFactory.load() val videosList = config.getConfigList("videos").asScala.toList val videos = videosList.map { videoConfig => Video( uri = videoConfig.getString("uri"), name = videoConfig.getString("name"), description = videoConfig.getString("description"), thumbnailUrl = videoConfig.getString("thumbnailUrl"), contentUrl = videoConfig.getString("contentUrl"), uploadDate = videoConfig.getString("uploadDate") ) }
Lastly, with that source code file named HoconVideos.scala, I run that code using this scala-cli
command at my macOS Terminal command line:
scala-cli run HoconVideos.scala '-Dconfig.file=videos.conf'
When that runs I see this successful output:
Video: Video: Creating A Small Application with ZIO 2 URI: /video-course/functional-programming-2/a-small-zio-2-example-application/ Description: This video shows the basics of how to create a small application with ZIO 2 and Scala 3. Thumbnail: https://alvinalexander.com/videos/courses/functional-programming-in-depth/240-ZIO-zMakeInt-Example.jpg Content: https://alvinalexander.com/videos/courses/functional-programming-in-depth/240-ZIO-zMakeInt-Example.mp4 Upload Date: 2024-03-21T08:00:00+08:00 Video: Video: Higher-Order Functions in Scala, Part 2 URI: /video-course/advanced-scala-3/higher-order-functions-2/ Description: A video on how to create and use higher-order functions in Scala 3, including functions like filter and map. Thumbnail: https://alvinalexander.com/videos/courses/advanced-scala-3/001--Scala-HOFs--Part2-filter.jpg Content: https://alvinalexander.com/videos/courses/advanced-scala-3/001--Scala-HOFs--Part2-filter.mp4 Upload Date: 2024-03-21T08:00:00+08:00
So as a little summary, if you ever wanted to use Scala 3 and scala-cli to read a Typesafe HOCON configuration file, I hope this example is helpful.
Scala-CLI videos
If you’d like more information on Scala CLI, here are two videos where I show how to set it up and then use it: