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

feat : Implementing config for part of services #4344

Merged
merged 12 commits into from
Mar 13, 2024
30 changes: 22 additions & 8 deletions core/src/services/memory/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@ use async_trait::async_trait;
use crate::raw::adapters::typed_kv;
use crate::*;

use serde::Deserialize;

use self::raw::ConfigDeserializer;

//Config for memory.
AnuRage-git marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Default, Deserialize)]
#[serde(default)]
#[non_exhaustive]

AnuRage-git marked this conversation as resolved.
Show resolved Hide resolved
pub struct MemoryConfig{
//root of the backend.
pub root: Option<String>,
}


/// In memory service support. (BTreeMap Based)
#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct MemoryBuilder {
root: Option<String>,
config: MemoryConfig,
}

impl MemoryBuilder {
/// Set the root for BTreeMap.
pub fn root(&mut self, path: &str) -> &mut Self {
self.root = Some(path.into());
self.config.root = Some(path.into());
self
}
}
Expand All @@ -46,19 +61,18 @@ impl Builder for MemoryBuilder {
type Accessor = MemoryBackend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = Self::default();

map.get("root").map(|v| builder.root(v));

builder
MemoryBuilder{
config: MemoryConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed"),
}
}

fn build(&mut self) -> Result<Self::Accessor> {
let adapter = Adapter {
inner: Arc::new(Mutex::new(BTreeMap::default())),
};

Ok(MemoryBackend::new(adapter).with_root(self.root.as_deref().unwrap_or_default()))
Ok(MemoryBackend::new(adapter).with_root(self.config.root.as_deref().unwrap_or_default()))
AnuRage-git marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/services/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

mod backend;
pub use backend::MemoryBuilder as Memory;
pub use backend::MemoryConfig;
Loading