Skip to content

Commit

Permalink
wip: individual configs retained in appconfig, TODO: rewrite ConfigWr…
Browse files Browse the repository at this point in the history
…iter
  • Loading branch information
pshirshov committed Nov 17, 2023
1 parent dcbbdc6 commit 51001a1
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package izumi.distage.config.model

import izumi.distage.config.DistageConfigImpl

final case class AppConfig(
config: DistageConfigImpl
)

object AppConfig {
val empty: AppConfig = AppConfig(Map.empty[String, String])
def provided(config: DistageConfigImpl): AppConfig = AppConfig(config)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package izumi.distage.config.model

import com.typesafe.config.{Config, ConfigFactory}
import izumi.distage.config.DistageConfigImpl

import java.io.File

final case class AppConfig(
config: DistageConfigImpl,
shared: List[ConfigLoadResult.Success],
roles: List[LoadedRoleConfigs],
)

object AppConfig {
val empty: AppConfig = AppConfig(ConfigFactory.empty(), List.empty, List.empty)
def provided(config: DistageConfigImpl): AppConfig = AppConfig(config, List.empty, List.empty)
}

sealed trait GenericConfigSource

object GenericConfigSource {
case class ConfigFile(file: File) extends GenericConfigSource

case object ConfigDefault extends GenericConfigSource
}

case class RoleConfig(role: String, active: Boolean, configSource: GenericConfigSource)

case class LoadedRoleConfigs(roleConfig: RoleConfig, loaded: Seq[ConfigLoadResult.Success])

sealed trait ConfigLoadResult {
def clue: String

def src: ConfigSource

def toEither: Either[ConfigLoadResult.Failure, ConfigLoadResult.Success]
}

object ConfigLoadResult {
case class Success(clue: String, src: ConfigSource, config: Config) extends ConfigLoadResult {
override def toEither: Either[ConfigLoadResult.Failure, ConfigLoadResult.Success] = Right(this)
}

case class Failure(clue: String, src: ConfigSource, failure: Throwable) extends ConfigLoadResult {
override def toEither: Either[ConfigLoadResult.Failure, ConfigLoadResult.Success] = Left(this)
}
}

sealed trait ConfigSource

object ConfigSource {
final case class Resource(name: String, kind: ResourceConfigKind) extends ConfigSource {
override def toString: String = s"resource:$name"
}

final case class File(file: java.io.File) extends ConfigSource {
override def toString: String = s"file:$file"
}
}

sealed trait ResourceConfigKind

object ResourceConfigKind {
case object Primary extends ResourceConfigKind

case object Development extends ResourceConfigKind
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class ConfigTest extends AnyWordSpec {
}

def mkModule(config: Config): AppConfigModule = {
val appConfig = AppConfig(config)
val appConfig = AppConfig(config, List.empty, List.empty)
new AppConfigModule(appConfig)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import izumi.distage.model.definition.ModuleDef

class AppConfigModule(appConfig: AppConfig) extends ModuleDef {
make[AppConfig].fromValue(appConfig)

def this(config: DistageConfigImpl) = this(AppConfig(config))
}

object AppConfigModule {
def apply(appConfig: AppConfig): AppConfigModule = new AppConfigModule(appConfig)
def apply(config: DistageConfigImpl): AppConfigModule = new AppConfigModule(config)
def apply(config: DistageConfigImpl): AppConfigModule = new AppConfigModule(AppConfig.provided(config))
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ abstract class RoleCheckableApp[F[_]](override implicit val tagK: TagK[F]) exten
if (cfg.origin().resource() eq null) {
throw new DIConfigReadException(s"Couldn't find a config resource with name `$resourceName` - file not found", null)
}
AppConfig(cfg)
AppConfig(cfg, List.empty, List.empty)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package izumi.distage.framework.model

import distage.Injector
import izumi.distage.framework.services.ConfigMerger.ConfigMergerImpl
import izumi.distage.framework.services.{ConfigArgsProvider, ConfigLoader, ConfigLocationProvider}
import izumi.distage.model.definition.ModuleBase
import izumi.distage.model.plan.Roots
Expand All @@ -25,7 +26,11 @@ object PlanCheckInput {
module: ModuleBase,
roots: Roots,
roleNames: Set[String] = Set.empty,
configLoader: ConfigLoader = new ConfigLoader.LocalFSImpl(IzLogger(), ConfigLocationProvider.Default, ConfigArgsProvider.Empty),
configLoader: ConfigLoader = {
val logger = IzLogger()
val merger = new ConfigMergerImpl(logger)
new ConfigLoader.LocalFSImpl(logger, merger, ConfigLocationProvider.Default, ConfigArgsProvider.Empty)
},
appPlugins: LoadedPlugins = LoadedPlugins.empty,
bsPlugins: LoadedPlugins = LoadedPlugins.empty,
)(implicit effectType: TagK[F],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package izumi.distage.framework.services

import izumi.distage.config.model.{GenericConfigSource, RoleConfig}
import izumi.distage.roles.RoleAppMain
import izumi.distage.roles.model.meta.RolesInfo
import izumi.fundamentals.platform.cli.model.raw.RawAppArgs
Expand All @@ -12,8 +13,9 @@ trait ConfigArgsProvider {
}

object ConfigArgsProvider {

object Empty extends ConfigArgsProvider {
override def args(): ConfigLoader.Args = ConfigLoader.Args(None, Map.empty)
override def args(): ConfigLoader.Args = ConfigLoader.Args(None, List.empty)
}

@nowarn("msg=Unused import")
Expand All @@ -24,18 +26,23 @@ object ConfigArgsProvider {
override def args(): ConfigLoader.Args = {
import scala.collection.compat.*

val emptyRoleConfigs = rolesInfo.availableRoleNames.map(_ -> None).toMap

val maybeGlobalConfig = parameters.globalParameters.findValue(RoleAppMain.Options.configParam).asFile
val specifiedRoleConfigs = parameters.roles.iterator
val specifiedRoleConfigs: Map[String, Option[File]] = parameters.roles.iterator
.map(roleParams => roleParams.role -> roleParams.roleParameters.findValue(RoleAppMain.Options.configParam).asFile)
.toMap

// ConfigLoader.Args(maybeGlobalConfig, (emptyRoleConfigs ++ specifiedRoleConfigs).view.toMap)
val allConfigs: Map[String, Option[File]] = emptyRoleConfigs ++ specifiedRoleConfigs
ConfigLoader.Args(maybeGlobalConfig, allConfigs.view.filterKeys(rolesInfo.requiredRoleNames).toMap)
val roleConfigs = rolesInfo.availableRoleNames.toList.map {
roleName =>
val source = specifiedRoleConfigs.get(roleName).flatten match {
case Some(value) =>
GenericConfigSource.ConfigFile(value)
case _ =>
GenericConfigSource.ConfigDefault
}
RoleConfig(roleName, rolesInfo.requiredRoleNames.contains(roleName), source)
}
val maybeGlobalConfig = parameters.globalParameters.findValue(RoleAppMain.Options.configParam).asFile

ConfigLoader.Args(maybeGlobalConfig, roleConfigs)
}

}
}
Loading

0 comments on commit 51001a1

Please sign in to comment.