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

[ANCHOR-936] Fix ktReferenceServerConfig is not populated correctly #1607

Merged
merged 4 commits into from
Jan 7, 2025
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,25 @@
package org.stellar.reference

/**
* Converts a dot-separated string to camelCase. For example: - "kt.reference.server.config" becomes
* "ktReferenceServerConfig"
*
* @param s The dot-separated string to be converted.
* @return A string in camelCase format.
* @throws NullPointerException If the input string is null.
*/
fun dotToCamelCase(s: String): String {
return s.split(".") // Split by dot
.mapIndexed { index, part ->
if (part.isEmpty()) {
"" // Ignore empty parts caused by consecutive dots or leading/trailing dots
} else if (index == 0) {
part.replaceFirstChar {
it.lowercaseChar()
} // Ensure the first part starts with a lowercase letter
} else {
part.replaceFirstChar { it.uppercaseChar() } // Capitalize the first letter of other parts
}
}
.joinToString("") // Join all parts back together as camelCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.stellar.reference.di
import com.sksamuel.hoplite.*
import org.stellar.reference.data.Config
import org.stellar.reference.data.LocationConfig
import org.stellar.reference.dotToCamelCase

class ConfigContainer(envMap: Map<String, String>?) {
var config: Config = readCfg(envMap)
Expand All @@ -25,15 +26,17 @@ class ConfigContainer(envMap: Map<String, String>?) {
val locationCfgBuilder =
ConfigLoaderBuilder.default().addPropertySource(PropertySource.environment())

val cfgBuilder = ConfigLoaderBuilder.default()

// Add environment variables as a property source for the config object
cfgBuilder.addPropertySource(PropertySource.environment())
val cfgBuilder = ConfigLoaderBuilder.default().addPropertySource(PropertySource.environment())

// Add any environment variable overrides from the envMap
envMap?.run {
locationCfgBuilder.addMapSource(this)
// env variables override
cfgBuilder.addMapSource(this)

// for the location config, we need to convert the keys to camel case
val camelEnvMap = this.mapKeys { (key, _) -> dotToCamelCase(key) }
locationCfgBuilder.addMapSource(camelEnvMap)
}

val locationConfig = locationCfgBuilder.build().loadConfigOrThrow<LocationConfig>()
Expand Down
Loading