From 13e93a6d355da99b0afc466f7a0295b2a727d54a Mon Sep 17 00:00:00 2001 From: Satvik Ramaprasad Date: Sat, 2 Nov 2024 11:04:29 +0530 Subject: [PATCH] [Feature] DatabricksConfig: Add clone() support --- .../databricks/sdk/core/DatabricksConfig.java | 33 +++++++++++-------- .../sdk/core/DatabricksConfigTest.java | 18 ++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java index 3b692860..20e7f883 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java @@ -631,6 +631,25 @@ public DatabricksEnvironment getDatabricksEnvironment() { return DatabricksEnvironment.getEnvironmentFromHostname(this.host); } + private DatabricksConfig clone(Set fieldsToSkip) { + DatabricksConfig newConfig = new DatabricksConfig(); + for (Field f : DatabricksConfig.class.getDeclaredFields()) { + if (fieldsToSkip.contains(f.getName())) { + continue; + } + try { + f.set(newConfig, f.get(this)); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + return newConfig; + } + + public DatabricksConfig clone() { + return clone(new HashSet<>()); + } + public DatabricksConfig newWithWorkspaceHost(String host) { Set fieldsToSkip = new HashSet<>( @@ -645,18 +664,6 @@ public DatabricksConfig newWithWorkspaceHost(String host) { // don't cache the // header factory. "headerFactory")); - DatabricksConfig newConfig = new DatabricksConfig(); - for (Field f : DatabricksConfig.class.getDeclaredFields()) { - if (fieldsToSkip.contains(f.getName())) { - continue; - } - try { - f.set(newConfig, f.get(this)); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - newConfig.setHost(host); - return newConfig; + return clone(fieldsToSkip).setHost(host); } } diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java index 3ec891c6..e552a142 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java @@ -177,4 +177,22 @@ public void testNewWithWorkspaceHost() { assert newWorkspaceConfig.getClientId().equals("my-client-id"); assert newWorkspaceConfig.getClientSecret().equals("my-client-secret"); } + + @Test + public void testClone() { + DatabricksConfig config = + new DatabricksConfig() + .setAuthType("oauth-m2m") + .setClientId("my-client-id") + .setClientSecret("my-client-secret") + .setAccountId("account-id") + .setHost("https://account.cloud.databricks.com"); + + DatabricksConfig newWorkspaceConfig = config.clone(); + + assert newWorkspaceConfig.getHost().equals("https://account.cloud.databricks.com"); + assert newWorkspaceConfig.getAuthType().equals("oauth-m2m"); + assert newWorkspaceConfig.getClientId().equals("my-client-id"); + assert newWorkspaceConfig.getClientSecret().equals("my-client-secret"); + } }