Skip to content

Instantly share code, notes, and snippets.

@susliko
Last active October 26, 2019 09:51
Show Gist options
  • Save susliko/136075e589eef78a67e2ed51d45eb666 to your computer and use it in GitHub Desktop.
Save susliko/136075e589eef78a67e2ed51d45eb666 to your computer and use it in GitHub Desktop.
import java.util.Properties
import cats.data.EitherNel
import cats.syntax.parallel._
import cats.syntax.either._
import cats.syntax.option._
import cats.instances.parallel._
import scala.util.Try
object PropertyLoader extends App {
case class GroupActor(groupId: Int, accessToken: Long)
def loadProperties(fileName: String): Either[Throwable, Properties] = {
val properties = new Properties()
Try(properties.load(Foo.getClass.getClassLoader.getResourceAsStream(fileName)))
.toEither
.map(_ => properties)
}
def typedProperty[A](properties: Properties, propertyName: String, parse: String => Try[A]) =
Try(properties.getProperty(propertyName))
.flatMap(parse)
.toEither
val properties = loadProperties("properties")
val groupActor: EitherNel[Throwable, GroupActor] =
(properties
.flatMap(props => typedProperty(props, "group_id", s => Try(s.toInt)))
.toEitherNel,
properties
.flatMap(props => typedProperty(props, "access_token", s => Try(s.toLong)))
.toEitherNel)
.parMapN(GroupActor)
println(groupActor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment