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

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 46f23cb40 refactor(service/tikv): Add TikvConfig to implement 
ConfigDeserializer (#3512)
46f23cb40 is described below

commit 46f23cb407fdee69dd7c14cff84bf6820fb41af0
Author: caicancai <[email protected]>
AuthorDate: Wed Nov 8 14:38:06 2023 +0800

    refactor(service/tikv): Add TikvConfig to implement ConfigDeserializer 
(#3512)
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
    
    * refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
---
 core/src/services/mod.rs          |  2 +
 core/src/services/tikv/backend.rs | 94 +++++++++++++++++++++++++--------------
 core/src/services/tikv/mod.rs     |  1 +
 3 files changed, 63 insertions(+), 34 deletions(-)

diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs
index 042cf5a93..1d0d9a36e 100644
--- a/core/src/services/mod.rs
+++ b/core/src/services/mod.rs
@@ -220,6 +220,8 @@ pub use self::redb::Redb;
 mod tikv;
 #[cfg(feature = "services-tikv")]
 pub use self::tikv::Tikv;
+#[cfg(feature = "services-tikv")]
+pub use self::tikv::TikvConfig;
 
 #[cfg(feature = "services-foundationdb")]
 mod foundationdb;
diff --git a/core/src/services/tikv/backend.rs 
b/core/src/services/tikv/backend.rs
index 8dc46920a..e60394aca 100644
--- a/core/src/services/tikv/backend.rs
+++ b/core/src/services/tikv/backend.rs
@@ -20,11 +20,13 @@ use std::fmt::Debug;
 use std::fmt::Formatter;
 
 use async_trait::async_trait;
+use serde::Deserialize;
 use tikv_client::Config;
 use tikv_client::RawClient;
 use tokio::sync::OnceCell;
 
 use crate::raw::adapters::kv;
+use crate::raw::*;
 use crate::Builder;
 use crate::Capability;
 use crate::Error;
@@ -32,41 +34,71 @@ use crate::ErrorKind;
 use crate::Scheme;
 use crate::*;
 
-/// TiKV backend builder
-#[doc = include_str!("docs.md")]
-#[derive(Clone, Default)]
-pub struct TikvBuilder {
+/// Config for Tikv services support.
+#[derive(Default, Deserialize, Clone)]
+#[serde(default)]
+#[non_exhaustive]
+pub struct TikvConfig {
     /// network address of the TiKV service.
-    endpoints: Option<Vec<String>>,
+    pub endpoints: Option<Vec<String>>,
     /// whether using insecure connection to TiKV
-    insecure: bool,
+    pub insecure: bool,
     /// certificate authority file path
-    ca_path: Option<String>,
+    pub ca_path: Option<String>,
     /// cert path
-    cert_path: Option<String>,
+    pub cert_path: Option<String>,
     /// key path
-    key_path: Option<String>,
+    pub key_path: Option<String>,
+}
+
+impl Debug for TikvConfig {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        let mut d = f.debug_struct("TikvConfig");
+
+        d.field("endpoints", &self.endpoints)
+            .field("insecure", &self.insecure)
+            .field("ca_path", &self.ca_path)
+            .field("cert_path", &self.cert_path)
+            .field("key_path", &self.key_path)
+            .finish()
+    }
+}
+
+/// TiKV backend builder
+#[doc = include_str!("docs.md")]
+#[derive(Clone, Default)]
+pub struct TikvBuilder {
+    config: TikvConfig,
+}
+
+impl Debug for TikvBuilder {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        let mut d = f.debug_struct("TikvBuilder");
+
+        d.field("config", &self.config);
+        d.finish_non_exhaustive()
+    }
 }
 
 impl TikvBuilder {
     /// Set the network address of the TiKV service.
     pub fn endpoints(&mut self, endpoints: Vec<String>) -> &mut Self {
         if !endpoints.is_empty() {
-            self.endpoints = Some(endpoints)
+            self.config.endpoints = Some(endpoints)
         }
         self
     }
 
     /// Set the insecure connection to TiKV.
     pub fn insecure(&mut self) -> &mut Self {
-        self.insecure = true;
+        self.config.insecure = true;
         self
     }
 
     /// Set the certificate authority file path.
     pub fn ca_path(&mut self, ca_path: &str) -> &mut Self {
         if !ca_path.is_empty() {
-            self.ca_path = Some(ca_path.to_string())
+            self.config.ca_path = Some(ca_path.to_string())
         }
         self
     }
@@ -74,7 +106,7 @@ impl TikvBuilder {
     /// Set the certificate file path.
     pub fn cert_path(&mut self, cert_path: &str) -> &mut Self {
         if !cert_path.is_empty() {
-            self.cert_path = Some(cert_path.to_string())
+            self.config.cert_path = Some(cert_path.to_string())
         }
         self
     }
@@ -82,7 +114,7 @@ impl TikvBuilder {
     /// Set the key file path.
     pub fn key_path(&mut self, key_path: &str) -> &mut Self {
         if !key_path.is_empty() {
-            self.key_path = Some(key_path.to_string())
+            self.config.key_path = Some(key_path.to_string())
         }
         self
     }
@@ -93,23 +125,14 @@ impl Builder for TikvBuilder {
     type Accessor = Backend;
 
     fn from_map(map: HashMap<String, String>) -> Self {
-        let mut builder = TikvBuilder::default();
-
-        map.get("endpoints")
-            .map(|v| v.split(',').map(|s| 
s.to_owned()).collect::<Vec<String>>())
-            .map(|v| builder.endpoints(v));
-        map.get("insecure")
-            .filter(|v| *v == "on" || *v == "true")
-            .map(|_| builder.insecure());
-        map.get("ca_path").map(|v| builder.ca_path(v));
-        map.get("cert_path").map(|v| builder.cert_path(v));
-        map.get("key_path").map(|v| builder.key_path(v));
-
-        builder
+        let config = TikvConfig::deserialize(ConfigDeserializer::new(map))
+            .expect("config deserialize must succeed");
+
+        TikvBuilder { config }
     }
 
     fn build(&mut self) -> Result<Self::Accessor> {
-        let endpoints = self.endpoints.take().ok_or_else(|| {
+        let endpoints = self.config.endpoints.take().ok_or_else(|| {
             Error::new(
                 ErrorKind::ConfigInvalid,
                 "endpoints is required but not set",
@@ -117,8 +140,10 @@ impl Builder for TikvBuilder {
             .with_context("service", Scheme::Tikv)
         })?;
 
-        if self.insecure
-            && (self.ca_path.is_some() || self.key_path.is_some() || 
self.cert_path.is_some())
+        if self.config.insecure
+            && (self.config.ca_path.is_some()
+                || self.config.key_path.is_some()
+                || self.config.cert_path.is_some())
         {
             return Err(
                 Error::new(ErrorKind::ConfigInvalid, "invalid tls 
configuration")
@@ -130,10 +155,10 @@ impl Builder for TikvBuilder {
         Ok(Backend::new(Adapter {
             client: OnceCell::new(),
             endpoints,
-            insecure: self.insecure,
-            ca_path: self.ca_path.clone(),
-            cert_path: self.cert_path.clone(),
-            key_path: self.key_path.clone(),
+            insecure: self.config.insecure,
+            ca_path: self.config.ca_path.clone(),
+            cert_path: self.config.cert_path.clone(),
+            key_path: self.config.key_path.clone(),
         }))
     }
 }
@@ -154,6 +179,7 @@ pub struct Adapter {
 impl Debug for Adapter {
     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
         let mut ds = f.debug_struct("Adapter");
+
         ds.field("endpoints", &self.endpoints);
         ds.finish()
     }
diff --git a/core/src/services/tikv/mod.rs b/core/src/services/tikv/mod.rs
index c33101206..ae9c15bd6 100644
--- a/core/src/services/tikv/mod.rs
+++ b/core/src/services/tikv/mod.rs
@@ -18,3 +18,4 @@
 mod backend;
 
 pub use backend::TikvBuilder as Tikv;
+pub use backend::TikvConfig;

Reply via email to