Skip to content

Commit

Permalink
Fix inspection hints (#378)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam <sam@sksamuel.com>
  • Loading branch information
sschuberth and sksamuel committed May 20, 2023
1 parent 3371f99 commit 3a8259f
Show file tree
Hide file tree
Showing 81 changed files with 237 additions and 244 deletions.
2 changes: 1 addition & 1 deletion example-native/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ nativeBuild {
"-H:ReflectionConfigurationFiles=${projectDir}/META-INF/native-image/generated/reflect-config.json",
"""-H:ResourceConfigurationFiles=
|${projectDir}/META-INF/native-image/kotlin-resource.json,
|${projectDir}/META-INF/native-image/generated/resource-config.json""".trimMargin().replace(System.lineSeparator(), ""),
|${projectDir}/META-INF/native-image/generated/resource-config.json""".trimMargin().replace(System.lineSeparator(), "")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import java.nio.file.Path

data class MyConfig(
val name: String,
val nested: NestedConfig,
val nested: NestedConfig
)

data class NestedConfig(
val nums: List<Int>,
val nums: List<Int>
)

fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AwsOps(private val client: AWSSecretsManager) {
"Name" to result.name,
"Arn" to result.arn,
"Created Date" to result.createdDate.toString(),
"Version Id" to result.versionId,
"Version Id" to result.versionId
)
)
}
Expand All @@ -56,12 +56,10 @@ class AwsOps(private val client: AWSSecretsManager) {
secret.valid()
} else {
val map = runCatching { Json.Default.decodeFromString<Map<String, String>>(secret) }.getOrElse { emptyMap() }
val indexedValue = map[index]
if (indexedValue == null)
ConfigFailure.ResolverError(
map[index]?.valid()
?: ConfigFailure.ResolverError(
"Index '$index' not present in AWS secret '${result.name}'. Present keys are ${map.keys.joinToString(",")}"
).invalid()
else indexedValue.valid()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder
*/
class AwsSecretsManagerContextResolver(
report: Boolean = false,
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() },
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() }
) : AwsSecretsManagerRegexResolver(report, createClient) {
override val contextKey: String = "aws-secrets-manager"
override val default: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlinx.serialization.json.Json
*/
class AwsSecretsManagerPreprocessor(
private val report: Boolean = false,
private val createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() },
private val createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() }
) : TraversingPrimitivePreprocessor() {

private val client by lazy { createClient() }
Expand Down Expand Up @@ -69,7 +69,7 @@ class AwsSecretsManagerPreprocessor(
"Name" to value.name,
"Arn" to value.arn,
"Created Date" to value.createdDate.toString(),
"Version Id" to value.versionId,
"Version Id" to value.versionId
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.sksamuel.hoplite.resolver.ContextResolver

abstract class AwsSecretsManagerRegexResolver(
private val report: Boolean = false,
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() },
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() }
) : ContextResolver() {

// should stay lazy so still be added to config even when not used, eg locally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import com.sksamuel.hoplite.resolver.Resolver

fun createAwsSecretsManagerResolver(
report: Boolean = false,
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() },
createClient: () -> AWSSecretsManager = { AWSSecretsManagerClientBuilder.standard().build() }
): Resolver = CompositeResolver(AwsSecretsManagerContextResolver(report, createClient))
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class AwsSecretsManagerPreprocessorTest : FunSpec() {
DotPath.root,
emptyMap()
),
DecoderContext.zero,
DecoderContext.zero
).shouldBeInstanceOf<Validated.Invalid<ConfigFailure>>().error.description().shouldContain("unkunk")
}

Expand All @@ -91,9 +91,9 @@ class AwsSecretsManagerPreprocessorTest : FunSpec() {
"secretsmanager://bibblebobble",
Pos.NoPos,
DotPath.root,
emptyMap(),
emptyMap()
),
DecoderContext.zero,
DecoderContext.zero
).shouldBeInstanceOf<Validated.Invalid<ConfigFailure>>().error.description().shouldContain("Empty secret")
}

Expand All @@ -103,7 +103,7 @@ class AwsSecretsManagerPreprocessorTest : FunSpec() {
"secretsmanager://unkunk", Pos.NoPos, DotPath.root,
emptyMap()
),
DecoderContext.zero,
DecoderContext.zero
).shouldBeInstanceOf<Validated.Invalid<ConfigFailure>>().error.description()
.shouldNotContain("secretsmanager://")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import software.amazon.awssdk.services.secretsmanager.model.SecretsManagerExcept
*/
class AwsSecretsManagerPreprocessor(
private val report: Boolean = false,
private val createClient: () -> SecretsManagerClient = { SecretsManagerClient.create() },
private val createClient: () -> SecretsManagerClient = { SecretsManagerClient.create() }
) : TraversingPrimitivePreprocessor() {

private val client by lazy { createClient() }
Expand Down Expand Up @@ -67,7 +67,7 @@ class AwsSecretsManagerPreprocessor(
"Name" to value.name(),
"Arn" to value.arn(),
"Created Date" to value.createdDate().toString(),
"Version Id" to value.versionId(),
"Version Id" to value.versionId()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import com.sksamuel.hoplite.resolver.ContextResolver

class AzureKeyVaultResolver(
private val report: Boolean = false,
private val createClient: () -> SecretClient,
private val createClient: () -> SecretClient
) : ContextResolver() {

constructor(url: String) : this(url, false)
constructor(url: String, report: Boolean) : this(report = report, {
SecretClientBuilder()
.vaultUrl(url)
.credential(DefaultAzureCredentialBuilder().build())
.buildClient();
.buildClient()
})

private val client = lazy { createClient() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.util.Optional
class ConsulConfigPreprocessor(
private val url: String,
private val report: Boolean = false,
private val configure: (Consul.Builder) -> Unit = {},
private val configure: (Consul.Builder) -> Unit = {}
) : TraversingPrimitivePreprocessor() {

private val client by lazy { createClient() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.util.Optional
class ConsulConfigResolver(
private val url: String,
private val report: Boolean = false,
private val configure: (Consul.Builder) -> Unit = {},
private val configure: (Consul.Builder) -> Unit = {}
) : ContextResolver() {

private val client by lazy { createClient() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ sealed interface ConfigFailure {
}

data class PropertySourceFailure(val msg: String, val cause: Throwable?) : ConfigFailure {
override fun description(): String = msg + " " + cause
override fun description(): String = "$msg $cause"
}

data class DataClassWithoutConstructor(val kclass: KClass<*>) : ConfigFailure {
Expand Down Expand Up @@ -220,12 +220,12 @@ sealed interface ConfigFailure {

data class CollectionElementErrors(val node: Node, val errors: NonEmptyList<ConfigFailure>) : ConfigFailure {
override fun description(): String = "Collection element decode failure ${node.pos.loc()}:\n\n" +
errors.list.joinToString("\n\n") { it.description().indent(Constants.indent) }
errors.list.joinToString("\n\n") { it.description().indent() }
}

data class TupleErrors(val node: Node, val errors: NonEmptyList<ConfigFailure>) : ConfigFailure {
override fun description(): String = "- Could not instantiate Tuple because:\n\n" +
errors.list.joinToString("\n\n") { it.description().indent(Constants.indent) }
errors.list.joinToString("\n\n") { it.description().indent() }
}

data class InvalidEnumConstant(
Expand All @@ -243,7 +243,7 @@ sealed interface ConfigFailure {
val pos: Pos
) : ConfigFailure {
override fun description(): String = "- Could not instantiate '$type' because:\n\n" +
errors.list.joinToString("\n\n") { it.description().indent(Constants.indent) }
errors.list.joinToString("\n\n") { it.description().indent() }
}

data class ParamFailure(val param: KParameter, val error: ConfigFailure) : ConfigFailure {
Expand All @@ -264,7 +264,7 @@ data class ThrowableFailure(val throwable: Throwable) : ConfigFailure {
override fun description() = "${throwable.message}.${throwable.stackTrace.toList()}"
}

fun String.indent(indent: String = " "): String {
fun String.indent(indent: String = Constants.indent): String {
val lines = lineSequence()
.map {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ConfigLoader(
val obfuscator: Obfuscator? = null,
val reportPrintFn: Print? = null,
val flattenArraysToString: Boolean = false,
val resolvers: List<Resolver> = emptyList(),
val resolvers: List<Resolver> = emptyList()
) {

companion object {
Expand Down Expand Up @@ -90,7 +90,7 @@ class ConfigLoader(
*/
inline fun <reified A : Any> loadConfigOrThrow(
resourceOrFiles: List<String>,
classpathResourceLoader: ClasspathResourceLoader = ConfigSource.Companion::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = ConfigSource.Companion::class.java.toClasspathResourceLoader()
): A = loadConfig<A>(resourceOrFiles, classpathResourceLoader).returnOrThrow()

/**
Expand All @@ -111,7 +111,7 @@ class ConfigLoader(
*/
inline fun <reified A : Any> loadConfig(
vararg resourceOrFiles: String,
classpathResourceLoader: ClasspathResourceLoader = ConfigSource.Companion::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = ConfigSource.Companion::class.java.toClasspathResourceLoader()
): ConfigResult<A> = loadConfig(resourceOrFiles.toList(), classpathResourceLoader)

/**
Expand All @@ -124,7 +124,7 @@ class ConfigLoader(
*/
inline fun <reified A : Any> loadConfig(
resourceOrFiles: List<String>,
classpathResourceLoader: ClasspathResourceLoader = Companion::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = Companion::class.java.toClasspathResourceLoader()
): ConfigResult<A> = loadConfig(A::class, emptyList(), resourceOrFiles, classpathResourceLoader)

/**
Expand All @@ -141,7 +141,7 @@ class ConfigLoader(
kclass: KClass<A>,
configSources: List<ConfigSource>,
resourceOrFiles: List<String>,
classpathResourceLoader: ClasspathResourceLoader = Companion::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = Companion::class.java.toClasspathResourceLoader()
): ConfigResult<A> {
require(kclass.isData) { "Can only decode into data classes [was ${kclass}]" }
return ConfigParser(
Expand All @@ -161,7 +161,7 @@ class ConfigLoader(
obfuscator = obfuscator ?: PrefixObfuscator(3),
reportPrintFn = reportPrintFn ?: { println(it) },
environment = environment,
resolvers = resolvers,
resolvers = resolvers
).decode(kclass, environment, resourceOrFiles, propertySources, configSources)
}

Expand All @@ -185,15 +185,15 @@ class ConfigLoader(
*/
fun loadNodeOrThrow(
resourceOrFiles: List<String>,
classpathResourceLoader: ClasspathResourceLoader = ConfigLoader::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = ConfigLoader::class.java.toClasspathResourceLoader()
): Node = loadNode(resourceOrFiles, emptyList(), classpathResourceLoader).returnOrThrow()

fun loadNode(vararg resourceOrFiles: String): ConfigResult<Node> = loadNode(resourceOrFiles.toList())

fun loadNode(
resourceOrFiles: List<String>,
configSources: List<ConfigSource> = emptyList(),
classpathResourceLoader: ClasspathResourceLoader = ConfigLoader::class.java.toClasspathResourceLoader(),
classpathResourceLoader: ClasspathResourceLoader = ConfigLoader::class.java.toClasspathResourceLoader()
): ConfigResult<Node> {
return ConfigParser(
classpathResourceLoader = classpathResourceLoader,
Expand All @@ -212,7 +212,7 @@ class ConfigLoader(
obfuscator = StrictObfuscator("*"), // not used when loading nodes
reportPrintFn = reportPrintFn ?: { }, // not used when loading nodes
environment = environment,
resolvers = resolvers,
resolvers = resolvers
).load(resourceOrFiles, propertySources, configSources)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,14 @@ class ConfigLoaderBuilder private constructor() {
@Deprecated(
"use withReport(). Passing in a reporter no longer has any effect. Specify secrets policy and obfuscator directly on this builder.",
ReplaceWith("withReport()"),
level = DeprecationLevel.ERROR,
level = DeprecationLevel.ERROR
)
fun withReport(reporter: Reporter) = apply { useReport = true }

@Deprecated(
"use withReport(). Passing in a reporter no longer has any effect. Specify secrets policy and obfuscator directly on this builder.",
ReplaceWith("withReport()"),
level = DeprecationLevel.ERROR,
level = DeprecationLevel.ERROR
)
fun report(reporter: Reporter) = apply { useReport = true }

Expand All @@ -327,7 +327,7 @@ class ConfigLoaderBuilder private constructor() {
environment = environment,
obfuscator = obfuscator,
reportPrintFn = reportPrintFn,
flattenArraysToString = flattenArraysToString,
flattenArraysToString = flattenArraysToString
)
}
}
Expand All @@ -336,27 +336,27 @@ fun defaultPropertySources(): List<PropertySource> = listOfNotNull(
EnvironmentVariableOverridePropertySource(true),
SystemPropertiesPropertySource,
UserSettingsPropertySource,
XdgConfigPropertySource,
XdgConfigPropertySource
)

fun defaultPreprocessors(): List<Preprocessor> = listOf(
EnvOrSystemPropertyPreprocessor,
RandomPreprocessor,
LookupPreprocessor,
LookupPreprocessor
)

fun defaultResolvers(): List<Resolver> = listOf(
EnvVarContextResolver,
SystemPropertyContextResolver,
ReferenceContextResolver,
HopliteContextResolver,
HopliteContextResolver
)

fun defaultParamMappers(): List<ParameterMapper> = listOf(
DefaultParamMapper,
SnakeCaseParamMapper,
KebabCaseParamMapper,
AliasAnnotationParamMapper,
AliasAnnotationParamMapper
)

val defaultDecoders = listOf(
Expand Down Expand Up @@ -409,5 +409,5 @@ val defaultDecoders = listOf(
com.sksamuel.hoplite.decoder.SecondsDecoder(),
com.sksamuel.hoplite.decoder.InlineClassDecoder(),
com.sksamuel.hoplite.decoder.SealedClassDecoder(),
com.sksamuel.hoplite.decoder.DataClassDecoder(),
com.sksamuel.hoplite.decoder.DataClassDecoder()
)

0 comments on commit 3a8259f

Please sign in to comment.