Skip to content

Instantly share code, notes, and snippets.

@inmyth
Created October 22, 2020 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inmyth/f0122e894274ac86a40f96a404dd74e8 to your computer and use it in GitHub Desktop.
Save inmyth/f0122e894274ac86a40f96a404dd74e8 to your computer and use it in GitHub Desktop.
Config to AWS Type Converter (Scala PureConfig)
import awscala.Region
import awscala.s3.Bucket
import com.typesafe.config.ConfigValueType
import pureconfig.error.{FailureReason, WrongType}
import pureconfig.{ConfigCursor, ConfigReader}
import scala.util.{Failure, Success, Try}
object ConfUtils {
implicit val awsS3BucketConverter = new ConfigReader[Bucket] {
def from(cur: ConfigCursor) = {
cur.asConfigValue.map(s => Bucket(s.unwrapped().asInstanceOf[String]))
}
}
case class AWSRegionUnrecognized(value: String) extends FailureReason {
override def description: String = s"""Unrecognized AWS Region: $value"""
}
implicit val awsRegionConverter = ConfigReader.fromCursor[Region] { cur =>
cur.asConfigValue.flatMap { s =>
{
val region = s.unwrapped().asInstanceOf[String]
Try(Region(region)) match {
case Success(n) if n != null => Right(n)
case Success(_) => cur.failed(AWSRegionUnrecognized(region))
case Failure(_) => cur.failed(WrongType(ConfigValueType.STRING, Set(ConfigValueType.STRING)))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment