Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config refactoring #2040

Merged
merged 10 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,6 +1,7 @@
package izumi.distage.roles

import distage.Injector
import distage.config.AppConfig
import izumi.distage.framework.config.PlanningOptions
import izumi.distage.model.Locator
import izumi.distage.model.definition.{Activation, Axis, Module, ModuleDef}
Expand All @@ -9,6 +10,7 @@ import izumi.distage.plugins.PluginConfig
import izumi.distage.roles.RoleAppMain.ArgV
import izumi.distage.roles.launcher.AppResourceProvider.AppResource
import izumi.distage.roles.launcher.AppShutdownStrategy
import izumi.distage.roles.launcher.ActivationParser
import izumi.functional.lifecycle.Lifecycle
import izumi.functional.quasi.QuasiIO
import izumi.fundamentals.platform.cli.model.raw.{RawAppArgs, RawEntrypointParams, RawRoleParams, RequiredRoles}
Expand Down Expand Up @@ -107,7 +109,12 @@ abstract class RoleAppMain[F[_]](
) ++ new ModuleDef {
make[RawAppArgs].fromValue(RawAppArgs(RawEntrypointParams.empty, additionalRoles.requiredRoles))
make[PlanningOptions].fromValue(planningOptions())
make[Activation].named("roleapp").fromValue(activation())
make[ActivationParser].from[ActivationParser.Impl]
make[Activation].named("entrypoint").fromValue(activation())
make[Activation].named("roleapp").from {
(parser: ActivationParser, config: AppConfig) =>
parser.parseActivation(config)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package izumi.distage.roles.launcher

import distage.config.AppConfig
import izumi.distage.model.definition.{Activation, Id}
import scala.annotation.unused

trait ActivationParser extends AbstractActivationParser {}

object ActivationParser {

class Impl(
defaultActivations: Activation@Id("default"),
additionalActivations: Activation@Id("additional"),
activation: Activation@Id("entrypoint"),
) extends ActivationParser {
def parseActivation(@unused config: AppConfig): Activation = {
defaultActivations ++
additionalActivations ++
activation
}
}

}
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,8 +1,8 @@
package izumi.distage.framework.model

import distage.Injector
import izumi.distage.framework.services.ConfigLoader
import izumi.distage.framework.services.ConfigLoader.ConfigLocation
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
import izumi.distage.model.reflection.DIKey
Expand All @@ -26,7 +26,11 @@ object PlanCheckInput {
module: ModuleBase,
roots: Roots,
roleNames: Set[String] = Set.empty,
configLoader: ConfigLoader = new ConfigLoader.LocalFSImpl(IzLogger(), ConfigLocation.Default, ConfigLoader.Args.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
@@ -0,0 +1,48 @@
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

import java.io.File
import scala.annotation.nowarn

trait ConfigArgsProvider {
def args(): ConfigLoader.Args
}

object ConfigArgsProvider {

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

@nowarn("msg=Unused import")
class Default(
parameters: RawAppArgs,
rolesInfo: RolesInfo,
) extends ConfigArgsProvider {
override def args(): ConfigLoader.Args = {
import scala.collection.compat.*

val specifiedRoleConfigs: Map[String, Option[File]] = parameters.roles.iterator
.map(roleParams => roleParams.role -> roleParams.roleParameters.findValue(RoleAppMain.Options.configParam).asFile)
.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
Loading