From 907e71fb5b66a33e355f030c89ac17edf6e551ae Mon Sep 17 00:00:00 2001 From: Vladimir Logachev Date: Fri, 15 Nov 2024 11:55:24 +0400 Subject: [PATCH] Remove more ficus code --- .../generator/TransactionsGeneratorApp.scala | 26 ++++++----- .../settings/BlockchainSettings.scala | 4 -- .../com/wavesplatform/settings/package.scala | 7 --- .../utils/ConfigSettingsValidator.scala | 46 +++++++++++-------- .../runner/input/PureconfigImplicits.scala | 5 +- 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/node-generator/src/main/scala/com/wavesplatform/generator/TransactionsGeneratorApp.scala b/node-generator/src/main/scala/com/wavesplatform/generator/TransactionsGeneratorApp.scala index 50a651b6394..6854b83f448 100644 --- a/node-generator/src/main/scala/com/wavesplatform/generator/TransactionsGeneratorApp.scala +++ b/node-generator/src/main/scala/com/wavesplatform/generator/TransactionsGeneratorApp.scala @@ -1,17 +1,15 @@ package com.wavesplatform.generator import java.io.File -import java.net.InetSocketAddress +import java.net.{InetSocketAddress, URI} import java.util.concurrent.Executors - -import scala.concurrent._ -import scala.concurrent.duration._ +import scala.concurrent.* +import scala.concurrent.duration.* import scala.util.{Failure, Random, Success} - import cats.implicits.showInterpolator -import com.typesafe.config.ConfigFactory +import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.account.AddressScheme -import com.wavesplatform.features.EstimatorProvider._ +import com.wavesplatform.features.EstimatorProvider.* import com.wavesplatform.generator.GeneratorSettings.NodeAddress import com.wavesplatform.generator.Preconditions.{PGenSettings, UniverseHolder} import com.wavesplatform.generator.cli.ScoptImplicits @@ -22,9 +20,9 @@ import com.wavesplatform.transaction.Transaction import com.wavesplatform.utils.{LoggerFacade, NTP} import com.wavesplatform.Application import monix.execution.Scheduler -import net.ceedubs.ficus.Ficus._ +import net.ceedubs.ficus.Ficus.* import net.ceedubs.ficus.readers.{EnumerationReader, NameMapper, ValueReader} -import net.ceedubs.ficus.readers.ArbitraryTypeReader._ +import net.ceedubs.ficus.readers.ArbitraryTypeReader.* import org.asynchttpclient.AsyncHttpClient import org.asynchttpclient.Dsl.asyncHttpClient import org.slf4j.LoggerFactory @@ -32,10 +30,14 @@ import scopt.OptionParser object TransactionsGeneratorApp extends App with ScoptImplicits with FicusImplicits with EnumerationReader { + implicit val inetSocketAddressReader: ValueReader[InetSocketAddress] = { (config: Config, path: String) => + val uri = new URI(s"my://${config.getString(path)}") + new InetSocketAddress(uri.getHost, uri.getPort) + } + // IDEA bugs - implicit val inetSocketAddressReader: ValueReader[InetSocketAddress] = com.wavesplatform.settings.inetSocketAddressReader - implicit val readConfigInHyphen: NameMapper = net.ceedubs.ficus.readers.namemappers.implicits.hyphenCase - implicit val httpClient: AsyncHttpClient = asyncHttpClient() + implicit val readConfigInHyphen: NameMapper = net.ceedubs.ficus.readers.namemappers.implicits.hyphenCase + implicit val httpClient: AsyncHttpClient = asyncHttpClient() val log = LoggerFacade(LoggerFactory.getLogger("generator")) diff --git a/node/src/main/scala/com/wavesplatform/settings/BlockchainSettings.scala b/node/src/main/scala/com/wavesplatform/settings/BlockchainSettings.scala index ddb73fba41b..51a84df39a4 100644 --- a/node/src/main/scala/com/wavesplatform/settings/BlockchainSettings.scala +++ b/node/src/main/scala/com/wavesplatform/settings/BlockchainSettings.scala @@ -7,7 +7,6 @@ import com.wavesplatform.account.Address import com.wavesplatform.common.state.ByteStr import pureconfig.* import pureconfig.generic.auto.* -import net.ceedubs.ficus.readers.ValueReader import scala.concurrent.duration.* @@ -245,9 +244,6 @@ private[settings] object BlockchainType { } object BlockchainSettings { - implicit val valueReader: ValueReader[BlockchainSettings] = - (cfg: Config, path: String) => fromConfig(cfg.getConfig(path)) - def fromRootConfig(config: Config): BlockchainSettings = fromConfig(config.getConfig("waves.blockchain")) def fromConfig(config: Config): BlockchainSettings = { diff --git a/node/src/main/scala/com/wavesplatform/settings/package.scala b/node/src/main/scala/com/wavesplatform/settings/package.scala index ce7c7e4a865..b7d96b4f113 100644 --- a/node/src/main/scala/com/wavesplatform/settings/package.scala +++ b/node/src/main/scala/com/wavesplatform/settings/package.scala @@ -1,10 +1,8 @@ package com.wavesplatform -import java.net.{InetSocketAddress, URI} import com.typesafe.config.{Config, ConfigFactory} import com.wavesplatform.account.PrivateKey import com.wavesplatform.common.state.ByteStr -import net.ceedubs.ficus.readers.ValueReader import pureconfig.ConfigReader import pureconfig.ConvertHelpers.catchReadError import pureconfig.configurable.genericMapReader @@ -18,11 +16,6 @@ package object settings { ConfigReader.fromString(str => ByteStr.decodeBase58(str).toEither.left.map(e => CannotConvert(str, "ByteStr", e.getMessage))) implicit val preactivatedFeaturesReader: ConfigReader[Map[Short, Int]] = genericMapReader(catchReadError(_.toShort)) - implicit val inetSocketAddressReader: ValueReader[InetSocketAddress] = { (config: Config, path: String) => - val uri = new URI(s"my://${config.getString(path)}") - new InetSocketAddress(uri.getHost, uri.getPort) - } - implicit val privateKeyReader: ConfigReader[PrivateKey] = ConfigReader[ByteStr].map(PrivateKey(_)) object SizeInBytes extends TaggedType[Long] diff --git a/node/src/main/scala/com/wavesplatform/settings/utils/ConfigSettingsValidator.scala b/node/src/main/scala/com/wavesplatform/settings/utils/ConfigSettingsValidator.scala index 33a3de7608b..8db2e7da4a2 100644 --- a/node/src/main/scala/com/wavesplatform/settings/utils/ConfigSettingsValidator.scala +++ b/node/src/main/scala/com/wavesplatform/settings/utils/ConfigSettingsValidator.scala @@ -1,15 +1,15 @@ package com.wavesplatform.settings.utils import cats.data.{NonEmptyList, Validated, ValidatedNel} -import cats.instances.list._ -import cats.syntax.foldable._ -import cats.syntax.traverse._ -import com.typesafe.config.{Config, ConfigException} +import cats.instances.list.* +import cats.syntax.foldable.* +import cats.syntax.traverse.* +import com.typesafe.config.{Config, ConfigException, ConfigFactory, ConfigValue} import com.wavesplatform.transaction.assets.exchange.AssetPair -import net.ceedubs.ficus.Ficus._ -import net.ceedubs.ficus.readers.ValueReader +import pureconfig.* -import scala.jdk.CollectionConverters._ +import scala.jdk.CollectionConverters.* +import scala.reflect.ClassTag import scala.util.Try object ConfigSettingsValidator { @@ -46,11 +46,13 @@ class ConfigSettingsValidator(config: Config) { NonEmptyList.one(s"Invalid setting $settingName value: $msg") } - def validate[T: ValueReader](settingName: String, showError: Boolean = false): ErrorsListOr[T] = { - Validated fromTry Try(config.as[T](settingName)) leftMap (ex => createError(settingName, ex.getMessage, showError)) + def validate[T: ConfigReader: ClassTag](settingName: String, showError: Boolean = false): ErrorsListOr[T] = { + Validated fromTry Try(ConfigSource.fromConfig(config).at(settingName).loadOrThrow[T]) leftMap (ex => + createError(settingName, ex.getMessage, showError) + ) } - def validateByPredicate[T: ValueReader](settingName: String)(predicate: T => Boolean, errorMsg: String): ErrorsListOr[T] = { + def validateByPredicate[T: ConfigReader: ClassTag](settingName: String)(predicate: T => Boolean, errorMsg: String): ErrorsListOr[T] = { validate[T](settingName, showError = true).ensure(createError(settingName, errorMsg))(predicate) } @@ -58,21 +60,23 @@ class ConfigSettingsValidator(config: Config) { validateByPredicate[Double](settingName)(p => 0 < p && p <= 100, "required 0 < percent <= 100") } - def validateList[T: ValueReader](settingName: String): ErrorsListOr[List[T]] = { + private def configFromConfigValue(configValue: ConfigValue) = + ConfigFactory.empty().withValue("stubKey", configValue).getConfig("stubKey") + + def validateList[T: ConfigReader: ClassTag](settingName: String): ErrorsListOr[List[T]] = { config .getList(settingName) .asScala .toList .zipWithIndex - .traverse { - case (cfg, index) => - val elemPath = s"$settingName.$index" - Validated fromTry Try(cfg.atPath(elemPath).as[T](elemPath)) leftMap (ex => List(ex.getMessage)) + .traverse { case (cfg, index) => + val elemPath = s"$settingName.$index" + Validated fromTry Try(ConfigSource.fromConfig(configFromConfigValue(cfg)).at(elemPath).loadOrThrow[T]) leftMap (ex => List(ex.getMessage)) } .leftMap(errorsInList => createError(settingName, errorsInList.mkString(", "), showValue = false)) } - def validateMap[K, V: ValueReader](settingName: String)(keyValidator: String => Validated[String, K]): ErrorsListOr[Map[K, V]] = { + def validateMap[K, V: ConfigReader: ClassTag](settingName: String)(keyValidator: String => Validated[String, K]): ErrorsListOr[Map[K, V]] = { config .getConfig(settingName) .root() @@ -82,20 +86,22 @@ class ConfigSettingsValidator(config: Config) { .traverse { entry => val elemPath = s"$settingName.${entry.getKey}" val k = keyValidator(entry.getKey).leftMap(List(_)) - val v = Validated fromTry Try(entry.getValue.atPath(elemPath).as[V](elemPath)) leftMap (ex => List(ex.getMessage)) + val v = Validated fromTry Try(ConfigSource.fromConfig(configFromConfigValue(entry.getValue)).at(elemPath).loadOrThrow[V]) leftMap (ex => + List(ex.getMessage) + ) k.product(v) } .map(_.toMap) .leftMap(errorsInList => createError(settingName, errorsInList.mkString(", "), showValue = false)) } - def validateWithDefault[T: ValueReader](settingName: String, defaultValue: T, showError: Boolean = false): ErrorsListOr[T] = { + def validateWithDefault[T: ConfigReader: ClassTag](settingName: String, defaultValue: T, showError: Boolean = false): ErrorsListOr[T] = { Validated - .fromTry(Try(config.as[T](settingName)).recover { case _: ConfigException.Missing => defaultValue }) + .fromTry(Try(ConfigSource.fromConfig(config).at(settingName).loadOrThrow[T]).recover { case _: ConfigException.Missing => defaultValue }) .leftMap(ex => createError(settingName, ex.getMessage, showError)) } - def validateByPredicateWithDefault[T: ValueReader]( + def validateByPredicateWithDefault[T: ConfigReader: ClassTag]( settingName: String )(predicate: T => Boolean, errorMsg: String, defaultValue: T): ErrorsListOr[T] = { validateWithDefault[T](settingName, defaultValue, showError = true).ensure(createError(settingName, errorMsg))(predicate) diff --git a/ride-runner/src/main/scala/com/wavesplatform/ride/runner/input/PureconfigImplicits.scala b/ride-runner/src/main/scala/com/wavesplatform/ride/runner/input/PureconfigImplicits.scala index 9ee5cf2420c..14bf9ade5c2 100644 --- a/ride-runner/src/main/scala/com/wavesplatform/ride/runner/input/PureconfigImplicits.scala +++ b/ride-runner/src/main/scala/com/wavesplatform/ride/runner/input/PureconfigImplicits.scala @@ -16,9 +16,9 @@ object PureconfigImplicits { private def playJsonConfigReader[T: Reads]: ConfigReader[T] = ConfigReader.fromCursor { cur => for { configValue <- cur.asConfigValue - stubKey = "stubKey" - config = ConfigFactory.empty().withValue(stubKey, configValue) } yield { + val stubKey = "stubKey" + val config = ConfigFactory.empty().withValue(stubKey, configValue) val jsonStr = config.root().render(ConfigRenderOptions.concise()) JsonManipulations .pick(Json.parse(jsonStr), stubKey) @@ -30,7 +30,6 @@ object PureconfigImplicits { } } - implicit val addressConfigReader: ConfigReader[Address] = ConfigReader.fromString(s => Address.fromString(s).left.map(_ => CannotConvert(s, "Address", "invalid address")))