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

Global config store #267

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/bin/config-store.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a - in the filename, I've seen this also in the other files in the bin directory now. Everywhere we have underscores and not dashes in the file names. I guess going for consistency is probably good. I'd generally avoid dashes/spaces and so on in file names, I just use a underscore for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like dashes in binaries and options :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here — I prefer dashes to underscores in binary executable names. E.g., in Linux, the executable name is google-chrome. (For binary executables, even better is a short name with no dashes or underscores.)

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use clap::{Parser, Subcommand};
use derive_more::Display;
use gosub_engine::config;
use gosub_engine::config::settings::Setting;
use gosub_engine::config::storage::json::JsonStorageAdapter;
use gosub_engine::config::storage::sqlite::SqliteStorageAdapter;
use gosub_engine::config::storage::*;
use gosub_engine::config::StorageAdapter;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name "store" was a bit ambiguous. They are actually storage adapters since we already have a config store.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would make sense to reexport the StorageAdapters in gosub_engine::config::storage, so you can import them directly with

use gosub_engine::config::storage::*

Then you could also make your json memory and sqlite module private

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sharktheone Can you guve an example on what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, doing this in storage.rs

mod json;
mod memory;
mod sqlite;

pub use json::*;
pub use memory::*;
pub use sqlite::*;

Then you can do this

use gosub_engine::config::storage::*

and still use JsonStorageAdapter and SqliteStorageAdapter. This makes things cleaner. Most of the time when you import one of the storage atapters, you most likely need all.

use std::str::FromStr;

Expand Down
33 changes: 19 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod settings;
pub mod storage;

use crate::config::settings::{Setting, SettingInfo};
use crate::config::storage::memory::MemoryStorageAdapter;
use crate::config::storage::MemoryStorageAdapter;
use lazy_static::lazy_static;
use log::warn;
use serde_derive::Deserialize;
Expand Down Expand Up @@ -204,10 +204,6 @@ impl ConfigStore {
/// return the default value for the given key. Note that if the key is not found and no
/// default value is specified, this function will panic.
pub fn get(&self, key: &str) -> Option<Setting> {
if !self.has(key) {
return None;
}

if let Some(setting) = self.settings.lock().unwrap().borrow().get(key) {
return Some(setting.clone());
}
Expand All @@ -223,19 +219,27 @@ impl ConfigStore {
}

// Return the default value for the setting when nothing is found
let info = self.settings_info.get(key).unwrap();
Some(info.default.clone())
if let Some(info) = self.settings_info.get(key) {
return Some(info.default.clone());
}

// At this point we haven't found the key in the store, we haven't found it in storage, and we
// don't have a default value. This is a programming error, so we panic.
panic!("config: Setting {} is not known", key);
}

/// Sets the given setting to the given value. Will persist the setting to the
/// storage.
/// storage. Note that the setting MUST have a settings-info entry, otherwise
/// this function will not store the setting.
pub fn set(&self, key: &str, value: Setting) {
if !self.has(key) {
warn!("config: Setting {} is not known", key);
return;
}
let info = match self.settings_info.get(key) {
Some(info) => info,
None => {
warn!("config: Setting {} is not known", key);
return;
}
};

let info = self.settings_info.get(key).unwrap();
if mem::discriminant(&info.default) != mem::discriminant(&value) {
warn!(
"config: Setting {} is of different type than setting expects",
Expand All @@ -249,6 +253,7 @@ impl ConfigStore {
.unwrap()
.borrow_mut()
.insert(key.to_string(), value.clone());

self.storage.set(key, value);
}

Expand Down Expand Up @@ -291,7 +296,7 @@ impl ConfigStore {
#[cfg(test)]
mod test {
use super::*;
use storage::memory::MemoryStorageAdapter;
use storage::MemoryStorageAdapter;

#[test]
fn config_store() {
Expand Down
4 changes: 2 additions & 2 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Setting {

match self {
Setting::Map(values) => values.clone(),
_ => Vec::new(),
other => vec![other.to_string()],
}
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ impl FromStr for Setting {

/// Converts a string to a setting or None when the string is invalid
fn from_str(key: &str) -> Result<Setting> {
let (key_type, key_value) = key.split_once(':').unwrap();
let (key_type, key_value) = key.split_once(':').expect("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something that says, what has failed in the expect would be nice, else it is more or less the same as unwrap


let setting = match key_type {
"b" => Setting::Bool(
Expand Down
10 changes: 7 additions & 3 deletions src/config/storage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub mod json;
pub mod memory;
pub mod sqlite;
mod json;
mod memory;
mod sqlite;

pub use json::*;
pub use memory::*;
pub use sqlite::*;
5 changes: 1 addition & 4 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ mod test {
#[test]
fn resolver() {
// Add simple logger, if not possible, that's fine too
match SimpleLogger::new().init() {
Ok(_) => {}
Err(_) => {}
}
let _ = SimpleLogger::new().init();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i think about this, I'm not sure if we want to to this here. It maybe would make sense to use the SimpleLogger instead of our own and invoke the logger init somewhere in the main fn. The other option I'd see, to upgrade our logger and use this instead. I guess it makes sense to only have one logger.


let mut dns = Dns::new();

Expand Down
Loading