Created
October 22, 2020 04:34
-
-
Save inmyth/f0122e894274ac86a40f96a404dd74e8 to your computer and use it in GitHub Desktop.
Config to AWS Type Converter (Scala PureConfig)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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