From 8e3950564411c26983a33dd2ea1993876c59bfbf Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sat, 8 Jan 2022 01:36:50 +0530 Subject: [PATCH] Make config parameters optional with defaults Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/core/config.scala | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index cee03a4..68fe01a 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -42,15 +42,15 @@ class Config(configFilePath: String) { private val configJson = parse(configString) - val port: Int = (configJson \ AttributesEnum.PORT.toString).extract[Int] - val address: String = (configJson \ AttributesEnum.ADDRESS.toString).extract[String] - val throttle: Int = (configJson \ AttributesEnum.THROTTLE.toString).extract[Int] - val seed: Int = (configJson \ AttributesEnum.RANDOM_SEED.toString).extract[Int] - val captchaExpiryTimeLimit: Int = (configJson \ AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString).extract[Int] - val threadDelay: Int = (configJson \ AttributesEnum.THREAD_DELAY.toString).extract[Int] - val playgroundEnabled: Boolean = (configJson \ AttributesEnum.PLAYGROUND_ENABLED.toString).extract[Boolean] - val corsHeader: String = (configJson \ AttributesEnum.CORS_HEADER.toString).extract[String] - val maxAttempts: Int = (configJson \ AttributesEnum.MAX_ATTEMPTS.toString).extract[Int] + val port: Int = getOptionalConfigParam(AttributesEnum.PORT.toString, "8888").toInt + val address: String = getOptionalConfigParam(AttributesEnum.ADDRESS.toString, "0.0.0.0") + val throttle: Int = getOptionalConfigParam(AttributesEnum.THROTTLE.toString, "1000").toInt + val seed: Int = getOptionalConfigParam(AttributesEnum.RANDOM_SEED.toString, "375264328").toInt + val captchaExpiryTimeLimit: Int = getOptionalConfigParam(AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString, "5").toInt + val threadDelay: Int = getOptionalConfigParam(AttributesEnum.THREAD_DELAY.toString, "2").toInt + val playgroundEnabled: Boolean = getOptionalConfigParam(AttributesEnum.PLAYGROUND_ENABLED.toString, "true").toBoolean + val corsHeader: String = getOptionalConfigParam(AttributesEnum.CORS_HEADER.toString, "") + val maxAttempts: Int = getOptionalConfigParam(AttributesEnum.MAX_ATTEMPTS.toString, "10").toInt private val captchaConfigJson = (configJson \ "captchas") val captchaConfigTransform: JValue = captchaConfigJson transformField { case JField("config", JObject(config)) => @@ -63,6 +63,19 @@ class Config(configFilePath: String) { HelperFunctions.setSeed(seed) + private def getOptionalConfigParam(paramName: String, defaultValue: String): String = { + val param: Option[(String, JValue)] = (configJson findField({ + case JField(`paramName`, _) => true + case _ => false + })) + + if(param.isDefined){ + param.get._2.extract[String] + } else { + defaultValue + } + } + private def getDefaultConfig(): String = { val defaultConfigMap = (AttributesEnum.RANDOM_SEED.toString -> new ju.Random().nextInt()) ~