From 37d5208b2d44c26956814bb7ab5be2dbac4b5b97 Mon Sep 17 00:00:00 2001 From: jiahuihu Date: Fri, 3 Jan 2025 16:32:45 -0500 Subject: [PATCH 1/4] convert dot to camel case --- kotlin-reference-server/build.gradle.kts | 1 + .../stellar/reference/di/ConfigContainer.kt | 10 ++++---- .../org/stellar/anchor/util/StringHelper.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/kotlin-reference-server/build.gradle.kts b/kotlin-reference-server/build.gradle.kts index 02d11dff89..c5fde43d5d 100644 --- a/kotlin-reference-server/build.gradle.kts +++ b/kotlin-reference-server/build.gradle.kts @@ -21,6 +21,7 @@ dependencies { implementation(variantOf(libs.java.stellar.sdk) { classifier("uber") }) implementation(project(mapOf("path" to ":api-schema"))) + implementation(project(":lib-util")) } tasks { diff --git a/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt index 874965b6ba..7c6faf3ed8 100644 --- a/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt +++ b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt @@ -1,6 +1,7 @@ package org.stellar.reference.di import com.sksamuel.hoplite.* +import org.stellar.anchor.util.StringHelper.dotToCamelCase import org.stellar.reference.data.Config import org.stellar.reference.data.LocationConfig @@ -25,15 +26,14 @@ class ConfigContainer(envMap: Map?) { 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) - cfgBuilder.addMapSource(this) + val camelEnvMap = this.mapKeys { (key, _) -> dotToCamelCase(key) } + locationCfgBuilder.addMapSource(camelEnvMap) + cfgBuilder.addMapSource(camelEnvMap) } val locationConfig = locationCfgBuilder.build().loadConfigOrThrow() diff --git a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java index fd08dfdfd8..2765c5c765 100644 --- a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java +++ b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java @@ -52,6 +52,31 @@ public static String toPosixForm(String camel) { .toUpperCase(); } + /** + * 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. + */ + public static String dotToCamelCase(String s) { + String[] parts = s.split("\\."); // Split by dot + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; + if (i == 0) { + // If it's the first part, leave it as is (in lowercase) + result.append(part); + } else { + // Capitalize the first letter of other parts + result.append(part.substring(0, 1).toUpperCase()).append(part.substring(1)); + } + } + return result.toString(); + } + static Gson gson = GsonUtils.getInstance(); /** From ad617c9c3f84f33b619a1813e7eb0763a5cef59a Mon Sep 17 00:00:00 2001 From: jiahuihu Date: Mon, 6 Jan 2025 16:24:51 -0500 Subject: [PATCH 2/4] helper function edge case --- .../src/main/java/org/stellar/anchor/util/StringHelper.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java index 2765c5c765..9cb28a30d2 100644 --- a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java +++ b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java @@ -66,6 +66,9 @@ public static String dotToCamelCase(String s) { for (int i = 0; i < parts.length; i++) { String part = parts[i]; + if (part.isEmpty()) { + continue; + } if (i == 0) { // If it's the first part, leave it as is (in lowercase) result.append(part); From 46dd92b5f87f850178b17732c0aa3580fa39ad8e Mon Sep 17 00:00:00 2001 From: jiahuihu Date: Tue, 7 Jan 2025 11:56:32 -0500 Subject: [PATCH 3/4] fix more key mapping issue --- .../org/stellar/reference/StringHelperKt.kt | 25 +++++++++++++++++ .../stellar/reference/di/ConfigContainer.kt | 7 +++-- .../org/stellar/anchor/util/StringHelper.java | 28 ------------------- 3 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 kotlin-reference-server/src/main/kotlin/org/stellar/reference/StringHelperKt.kt diff --git a/kotlin-reference-server/src/main/kotlin/org/stellar/reference/StringHelperKt.kt b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/StringHelperKt.kt new file mode 100644 index 0000000000..2e6d68ef04 --- /dev/null +++ b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/StringHelperKt.kt @@ -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 +} diff --git a/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt index 7c6faf3ed8..32d0094385 100644 --- a/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt +++ b/kotlin-reference-server/src/main/kotlin/org/stellar/reference/di/ConfigContainer.kt @@ -1,9 +1,9 @@ package org.stellar.reference.di import com.sksamuel.hoplite.* -import org.stellar.anchor.util.StringHelper.dotToCamelCase import org.stellar.reference.data.Config import org.stellar.reference.data.LocationConfig +import org.stellar.reference.dotToCamelCase class ConfigContainer(envMap: Map?) { var config: Config = readCfg(envMap) @@ -31,9 +31,12 @@ class ConfigContainer(envMap: Map?) { // Add any environment variable overrides from the envMap envMap?.run { + // 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) - cfgBuilder.addMapSource(camelEnvMap) } val locationConfig = locationCfgBuilder.build().loadConfigOrThrow() diff --git a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java index 9cb28a30d2..fd08dfdfd8 100644 --- a/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java +++ b/lib-util/src/main/java/org/stellar/anchor/util/StringHelper.java @@ -52,34 +52,6 @@ public static String toPosixForm(String camel) { .toUpperCase(); } - /** - * 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. - */ - public static String dotToCamelCase(String s) { - String[] parts = s.split("\\."); // Split by dot - StringBuilder result = new StringBuilder(); - - for (int i = 0; i < parts.length; i++) { - String part = parts[i]; - if (part.isEmpty()) { - continue; - } - if (i == 0) { - // If it's the first part, leave it as is (in lowercase) - result.append(part); - } else { - // Capitalize the first letter of other parts - result.append(part.substring(0, 1).toUpperCase()).append(part.substring(1)); - } - } - return result.toString(); - } - static Gson gson = GsonUtils.getInstance(); /** From 62f77a23910d0ea1840cbdff09be556ae7dcde5d Mon Sep 17 00:00:00 2001 From: jiahuihu Date: Tue, 7 Jan 2025 12:02:45 -0500 Subject: [PATCH 4/4] remove unused dependency --- kotlin-reference-server/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/kotlin-reference-server/build.gradle.kts b/kotlin-reference-server/build.gradle.kts index c5fde43d5d..02d11dff89 100644 --- a/kotlin-reference-server/build.gradle.kts +++ b/kotlin-reference-server/build.gradle.kts @@ -21,7 +21,6 @@ dependencies { implementation(variantOf(libs.java.stellar.sdk) { classifier("uber") }) implementation(project(mapOf("path" to ":api-schema"))) - implementation(project(":lib-util")) } tasks {