Skip to content

Commit

Permalink
Add build_from_json to object_store_factory (#709)
Browse files Browse the repository at this point in the history
* Add build_from_json function

* Add tests
  • Loading branch information
lizardoluis authored Oct 17, 2024
1 parent 432efbd commit 9e2b449
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions object_store_factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ tracing-log = { workspace = true }
tracing-subscriber = { workspace = true }
url = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }


128 changes: 128 additions & 0 deletions object_store_factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ pub struct StorageLocationInfo {
}

impl ObjectStoreConfig {
pub async fn build_from_json(
url: &Url,
json_str: &str,
) -> Result<ObjectStoreConfig, object_store::Error> {
let (scheme, _) = ObjectStoreScheme::parse(url)?;

match scheme {
ObjectStoreScheme::Memory => Ok(ObjectStoreConfig::Memory),
ObjectStoreScheme::Local => {
let config: LocalConfig = serde_json::from_str(json_str).unwrap();
Ok(ObjectStoreConfig::Local(config))
}
ObjectStoreScheme::AmazonS3 => {
let config: S3Config = serde_json::from_str(json_str).unwrap();
Ok(ObjectStoreConfig::AmazonS3(config))
}
ObjectStoreScheme::GoogleCloudStorage => {
let config: GCSConfig = serde_json::from_str(json_str).unwrap();
Ok(ObjectStoreConfig::GoogleCloudStorage(config))
}
_ => {
unimplemented!();
}
}
}

pub fn to_hashmap(&self) -> HashMap<String, String> {
match self {
ObjectStoreConfig::Local(config) => config.to_hashmap(),
Expand Down Expand Up @@ -167,3 +193,105 @@ pub async fn build_storage_location_info_from_opts(
url: url.to_string(),
})
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[tokio::test]
async fn test_build_from_json_local() {
let url = Url::parse("file:///tmp").unwrap();
let json_str = json!({
"type": "local",
"data_dir": "/tmp",
"disable_hardlinks": false
})
.to_string();

let config = ObjectStoreConfig::build_from_json(&url, &json_str)
.await
.unwrap();
if let ObjectStoreConfig::Local(local_config) = config {
assert_eq!(local_config.data_dir, "/tmp");
assert!(!local_config.disable_hardlinks);
} else {
panic!("Expected ObjectStoreConfig::Local");
}
}

#[tokio::test]
async fn test_build_from_json_memory() {
let url = Url::parse("memory:///").unwrap();
let json_str = json!({
"type": "memory"
})
.to_string();

let config = ObjectStoreConfig::build_from_json(&url, &json_str)
.await
.unwrap();
assert_eq!(config, ObjectStoreConfig::Memory);
}

#[tokio::test]
async fn test_build_from_json_amazon_s3() {
let url = Url::parse("s3://bucket").unwrap();
let json_str = json!({
"type": "s3",
"bucket": "bucket",
"region": "us-west-2",
"access_key_id": "test_access_key",
"secret_access_key": "test_secret_key"
})
.to_string();

let config = ObjectStoreConfig::build_from_json(&url, &json_str)
.await
.unwrap();
if let ObjectStoreConfig::AmazonS3(s3_config) = config {
assert_eq!(s3_config.bucket, "bucket");
assert_eq!(s3_config.region.unwrap(), "us-west-2");
assert_eq!(s3_config.access_key_id.unwrap(), "test_access_key");
assert_eq!(s3_config.secret_access_key.unwrap(), "test_secret_key");
} else {
panic!("Expected ObjectStoreConfig::AmazonS3");
}
}

#[tokio::test]
async fn test_build_from_json_google_cloud_storage() {
let url = Url::parse("gs://bucket").unwrap();
let json_str = json!({
"type": "gcs",
"bucket": "bucket",
"google_application_credentials": "test_credentials"
})
.to_string();

let config = ObjectStoreConfig::build_from_json(&url, &json_str)
.await
.unwrap();
if let ObjectStoreConfig::GoogleCloudStorage(gcs_config) = config {
assert_eq!(gcs_config.bucket, "bucket");
assert_eq!(
gcs_config.google_application_credentials.unwrap(),
"test_credentials"
);
} else {
panic!("Expected ObjectStoreConfig::GoogleCloudStorage");
}
}

#[tokio::test]
async fn test_build_from_json_invalid_scheme() {
let url = Url::parse("ftp://bucket").unwrap();
let json_str = json!({
"type": "ftp"
})
.to_string();

let result = ObjectStoreConfig::build_from_json(&url, &json_str).await;
assert!(result.is_err());
}
}

0 comments on commit 9e2b449

Please sign in to comment.