Skip to content

Commit

Permalink
Added reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed May 27, 2023
1 parent efb2d6a commit 0391dab
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AwsOps(private val client: AWSSecretsManager) {
}

fun report(context: DecoderContext, result: GetSecretValueResult) {
context.report(
context.reporter.report(
ReportSection,
mapOf(
"Name" to result.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AwsSecretsManagerPreprocessor(
val value = client.getSecretValue(req)

if (report)
context.report(
context.reporter.report(
"AWS Secrets Manager Lookups",
mapOf(
"Name" to value.name,
Expand All @@ -78,7 +78,6 @@ class AwsSecretsManagerPreprocessor(
ConfigFailure.PreprocessorWarning("Empty secret '$key' in AWS SecretsManager").invalid()
else {
if (index == null) {
context.addMetaData(CommonMetadata.RemoteLookup, value)
node.copy(value = secret)
.withMeta(CommonMetadata.Secret, true)
.withMeta(CommonMetadata.UnprocessedValue, node.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AwsSecretsManagerPreprocessor(
val value = client.getSecretValue(valueRequest)

if (report)
context.report(
context.reporter.report(
"AWS Secrets Manager Lookups",
mapOf(
"Name" to value.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import com.sksamuel.hoplite.fp.valid

class AzureOps(private val client: SecretClient) {

fun report(context: DecoderContext, secret: KeyVaultSecret) {
context.report(
private fun report(context: DecoderContext, secret: KeyVaultSecret) {
context.reporter.report(
Section,
mapOf(
"Key" to secret.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class ConsulConfigPreprocessor(
when (val v = it.orElseGet { null }) {
null -> ConfigFailure.PreprocessorWarning("Unable to locate consul key '$key'").invalid()
else -> {
if (report)
context.report("Consul Lookups", mapOf("Key" to key))
if (report) context.reporter.report("Consul Lookups", mapOf("Key" to key))
node.copy(value = v).withMeta(CommonMetadata.UnprocessedValue, node.value).valid()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import kotlin.reflect.KType
data class DecoderContext(
val decoders: DecoderRegistry,
val paramMappers: List<ParameterMapper>,
val reporter: Reporter = Reporter(),
// these are the dot paths for every config value - overrided or not, that was used
val usedPaths: MutableSet<DotPath> = mutableSetOf(),
// this tracks the types that a node was marshalled into
val used: MutableSet<NodeState> = mutableSetOf(),
val metadata: MutableMap<String, Any?> = mutableMapOf(),
val reports: MutableMap<String, List<Map<String, Any?>>> = mutableMapOf(),
val config: DecoderConfig = DecoderConfig(false),
val environment: Environment? = null,
val resolvers: Resolving = Resolving.empty,
Expand All @@ -35,7 +35,6 @@ data class DecoderContext(
val sealedTypeDiscriminatorField: String? = null,
) {


/**
* Returns a [Decoder] for type [type].
*/
Expand All @@ -53,23 +52,14 @@ data class DecoderContext(
this.used.add(NodeState(node, true, value, type))
}

fun getMetaData(key: String): Any? = metadata[key]

fun addMetaData(key: String, value: Any?) {
metadata[key] = value
}

/**
* Adds the given row to the named [section] in the report.
* Adds a [row] to the named [section] in the report.
*/
fun report(section: String, row: Map<String, Any?>) {
val rows = reports.getOrPut(section) { emptyList() }
reports[section] = (rows + row).distinct()
}
@Deprecated("Use reporter.report", ReplaceWith("reporter.report(section, row)"))
fun report(section: String, row: Map<String, Any?>) = reporter.report(section, row)

companion object {
val zero =
DecoderContext(DecoderRegistry.zero, emptyList(), mutableSetOf(), resolvers = Resolving(emptyList(), Undefined))
val zero = DecoderContext(DecoderRegistry.zero, emptyList(), resolvers = Resolving(emptyList(), Undefined))
}
}

Expand Down
19 changes: 19 additions & 0 deletions hoplite-core/src/main/kotlin/com/sksamuel/hoplite/Reporter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sksamuel.hoplite

/**
* Allows [Resolver]s and [Decoder]s to add content to the output report.
*/
class Reporter {

private val sections: MutableMap<String, List<Map<String, Any?>>> = mutableMapOf()

fun getReport(): Map<String, List<Map<String, Any?>>> = sections.toMap()

/**
* Adds a [row] to the named [section] in the report.
*/
fun report(section: String, row: Map<String, Any?>) {
val rows = sections.getOrPut(section) { emptyList() }
sections[section] = (rows + row).distinct()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ConfigParser(
// always do report regardless of decoder result
if (useReport) {
Reporter(reportPrintFn, obfuscator, environment)
.printReport(propertySources, state, context.reports)
.printReport(propertySources, state, context.reporter.getReport())
}

decoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GcpSecretManagerPreprocessor(
val value = resp.payload.data.toStringUtf8()

if (report)
context.report("GCP Secret Manager Lookups", mapOf("Key" to key))
context.reporter.report("GCP Secret Manager Lookups", mapOf("Key" to key))

if (value.isNullOrBlank())
ConfigFailure.PreprocessorWarning("Empty value for '$key' in GCP Secret Manager").invalid()
Expand Down

0 comments on commit 0391dab

Please sign in to comment.