This is an automated email from the ASF dual-hosted git repository.

emaynard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git


The following commit(s) were added to refs/heads/main by this push:
     new 213fd0d0 Enable casting config values of most types (#343)
213fd0d0 is described below

commit 213fd0d0c2246021d7735c6c743a891bc4c6fb76
Author: Andrew Guterman <[email protected]>
AuthorDate: Fri Oct 4 13:59:50 2024 -0700

    Enable casting config values of most types (#343)
    
    * Enable casting config values of most types
    
    * Slightly randomize values
    
    * Add comments
    
    * Get rid of short/float, fix comment
    
    * Add negative test
---
 .../polaris/core/PolarisConfigurationStore.java    |  6 ++
 .../storage/PolarisConfigurationStoreTest.java     | 96 ++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git 
a/polaris-core/src/main/java/org/apache/polaris/core/PolarisConfigurationStore.java
 
b/polaris-core/src/main/java/org/apache/polaris/core/PolarisConfigurationStore.java
index f306a916..244e2063 100644
--- 
a/polaris-core/src/main/java/org/apache/polaris/core/PolarisConfigurationStore.java
+++ 
b/polaris-core/src/main/java/org/apache/polaris/core/PolarisConfigurationStore.java
@@ -69,6 +69,12 @@ public interface PolarisConfigurationStore {
 
     if (config.defaultValue instanceof Boolean) {
       return config.cast(Boolean.valueOf(String.valueOf(value)));
+    } else if (config.defaultValue instanceof Integer) {
+      return config.cast(Integer.valueOf(String.valueOf(value)));
+    } else if (config.defaultValue instanceof Long) {
+      return config.cast(Long.valueOf(String.valueOf(value)));
+    } else if (config.defaultValue instanceof Double) {
+      return config.cast(Double.valueOf(String.valueOf(value)));
     } else if (config.defaultValue instanceof List<?>) {
       return config.cast(List.copyOf((List<?>) value));
     } else {
diff --git 
a/polaris-core/src/test/java/org/apache/polaris/service/storage/PolarisConfigurationStoreTest.java
 
b/polaris-core/src/test/java/org/apache/polaris/service/storage/PolarisConfigurationStoreTest.java
new file mode 100644
index 00000000..e6d1c8be
--- /dev/null
+++ 
b/polaris-core/src/test/java/org/apache/polaris/service/storage/PolarisConfigurationStoreTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.storage;
+
+import java.util.List;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.PolarisConfiguration;
+import org.apache.polaris.core.PolarisConfigurationStore;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/** Unit test for the default behaviors of the PolarisConfigurationStore 
interface. */
+public class PolarisConfigurationStoreTest {
+  @Test
+  public void testConfigsCanBeCastedFromString() {
+    List<PolarisConfiguration<?>> configs =
+        List.of(
+            buildConfig("bool", true),
+            buildConfig("int", 12),
+            buildConfig("long", 34L),
+            buildConfig("double", 5.6D));
+
+    PolarisConfigurationStore store =
+        new PolarisConfigurationStore() {
+          /**
+           * Ad-hoc configuration store implementation that just returns the 
stringified version of
+           * the config's default value
+           */
+          @SuppressWarnings("unchecked")
+          @Override
+          public <T> @Nullable T getConfiguration(PolarisCallContext ctx, 
String configName) {
+            for (PolarisConfiguration<?> c : configs) {
+              if (c.key.equals(configName)) {
+                return (T) String.valueOf(c.defaultValue);
+              }
+            }
+
+            throw new IllegalStateException(
+                String.format(
+                    "Didn't find config value for %s, the test isn't set up 
correctly",
+                    configName));
+          }
+        };
+
+    // Ensure that we can fetch all the configs and that the value is what we 
expect, which
+    // is the config's default value based on how we've implemented 
PolarisConfigurationStore above.
+    for (PolarisConfiguration<?> c : configs) {
+      Assertions.assertEquals(c.defaultValue, store.getConfiguration(null, c));
+    }
+  }
+
+  @Test
+  public void testInvalidCastThrowsException() {
+    // Bool not included because Boolean.valueOf turns non-boolean strings to 
false
+    List<PolarisConfiguration<?>> configs =
+        List.of(buildConfig("int", 12), buildConfig("long", 34L), 
buildConfig("double", 5.6D));
+
+    PolarisConfigurationStore store =
+        new PolarisConfigurationStore() {
+          @SuppressWarnings("unchecked")
+          @Override
+          public <T> T getConfiguration(PolarisCallContext ctx, String 
configName) {
+            return (T) "abc123";
+          }
+        };
+
+    for (PolarisConfiguration<?> c : configs) {
+      Assertions.assertThrows(NumberFormatException.class, () -> 
store.getConfiguration(null, c));
+    }
+  }
+
+  private static <T> PolarisConfiguration<T> buildConfig(String key, T 
defaultValue) {
+    return PolarisConfiguration.<T>builder()
+        .key(key)
+        .description("")
+        .defaultValue(defaultValue)
+        .build();
+  }
+}

Reply via email to