Skip to content

Commit

Permalink
Remove more ficus code
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed Nov 15, 2024
1 parent dbe8da9 commit 907e71f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,20 +20,24 @@ 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
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"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -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 = {
Expand Down
7 changes: 0 additions & 7 deletions node/src/main/scala/com/wavesplatform/settings/package.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -46,33 +46,37 @@ 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)
}

def validatePercent(settingName: String): ErrorsListOr[Double] = {
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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,7 +30,6 @@ object PureconfigImplicits {
}
}


implicit val addressConfigReader: ConfigReader[Address] =
ConfigReader.fromString(s => Address.fromString(s).left.map(_ => CannotConvert(s, "Address", "invalid address")))

Expand Down

0 comments on commit 907e71f

Please sign in to comment.