Skip to content

Instantly share code, notes, and snippets.

@susliko
Created October 26, 2019 09:54
Show Gist options
  • Save susliko/21a0c4da2a89b172a2534366a947cbd9 to your computer and use it in GitHub Desktop.
Save susliko/21a0c4da2a89b172a2534366a947cbd9 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 Foo 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("propeties")
val groupActor = for {
props <- properties
groupId <- typedProperty(props, "group_id", s => Try(s.toInt))
accessToken <- typedProperty(props, "access_token", s => Try(s.toLong))
} yield GroupActor(groupId, accessToken)
println(groupActor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment