From 21b7796ce36c8e8cbe66ba3c7ed0a0b6750ca728 Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Fri, 29 Nov 2024 14:49:33 +0530 Subject: [PATCH 1/4] config: Return default if sub-config key doesn't exist --- crates/omnix-common/src/config.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/crates/omnix-common/src/config.rs b/crates/omnix-common/src/config.rs index 73d378e3..917f5051 100644 --- a/crates/omnix-common/src/config.rs +++ b/crates/omnix-common/src/config.rs @@ -77,14 +77,7 @@ impl OmConfig { // Get the config map, returning default if it doesn't exist let config = match self.config.get::(root_key)? { Some(res) => res, - None => { - return if self.reference.is_empty() { - Ok((T::default(), &[])) - } else { - // Reference requires the config to exist. - Err(OmConfigError::UnexpectedAttribute(self.reference.join("."))) - }; - } + None => return Ok((T::default(), &[])) }; let default = "default".to_string(); @@ -129,10 +122,6 @@ pub enum OmConfigError { #[error("Missing configuration attribute: {0}")] MissingConfigAttribute(String), - /// Unexpected attribute - #[error("Unexpected attribute: {0}")] - UnexpectedAttribute(String), - /// A [nix_rs::command::NixCmdError] #[error("Nix command error: {0}")] NixCmdError(#[from] nix_rs::command::NixCmdError), From 69e6c533531aad33513755c8a7e14a592ac9a6da Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Fri, 29 Nov 2024 17:26:39 +0530 Subject: [PATCH 2/4] fmt --- crates/omnix-common/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/omnix-common/src/config.rs b/crates/omnix-common/src/config.rs index 917f5051..af00e27c 100644 --- a/crates/omnix-common/src/config.rs +++ b/crates/omnix-common/src/config.rs @@ -77,7 +77,7 @@ impl OmConfig { // Get the config map, returning default if it doesn't exist let config = match self.config.get::(root_key)? { Some(res) => res, - None => return Ok((T::default(), &[])) + None => return Ok((T::default(), &[])), }; let default = "default".to_string(); From 5a1b11f07593bbdb9d167f5baac49642486bfd39 Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Sat, 30 Nov 2024 11:10:24 +0530 Subject: [PATCH 3/4] Use `flake_url.without_attr` while fetching `FlakeMetadata` --- crates/omnix-common/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/omnix-common/src/config.rs b/crates/omnix-common/src/config.rs index af00e27c..cdc0554e 100644 --- a/crates/omnix-common/src/config.rs +++ b/crates/omnix-common/src/config.rs @@ -37,7 +37,7 @@ impl OmConfig { let path = if let Some(local_path) = flake_url.without_attr().as_local_path() { local_path.to_path_buf() } else { - FlakeMetadata::from_nix(NixCmd::get().await, flake_url) + FlakeMetadata::from_nix(NixCmd::get().await, &flake_url.without_attr()) .await? .path } From a99ef17e9c3a97ef1ef7f706f1cb458eba6f4c36 Mon Sep 17 00:00:00 2001 From: shivaraj-bh Date: Sat, 30 Nov 2024 16:40:39 +0530 Subject: [PATCH 4/4] omnix-common: Add tests --- crates/omnix-common/src/config.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/omnix-common/src/config.rs b/crates/omnix-common/src/config.rs index cdc0554e..3a2ba42a 100644 --- a/crates/omnix-common/src/config.rs +++ b/crates/omnix-common/src/config.rs @@ -7,6 +7,8 @@ use nix_rs::{ flake::{eval::nix_eval_attr, metadata::FlakeMetadata, url::FlakeUrl}, }; use serde::{de::DeserializeOwned, Deserialize}; +#[cfg(test)] +use std::str::FromStr; /// [OmConfigTree] with additional metadata about the flake URL and reference. /// @@ -138,3 +140,28 @@ pub enum OmConfigError { #[error("Failed to read yaml: {0}")] ReadYaml(#[from] std::io::Error), } + +#[tokio::test] +async fn test_get_missing_sub_config() { + let om_config = OmConfig { + flake_url: FlakeUrl::from_str(".").unwrap(), + reference: vec![], + config: serde_yaml::from_str("").unwrap(), + }; + + let (res, _rest) = om_config.get_sub_config_under::("health").unwrap(); + + assert_eq!(res, String::default()); +} + +#[tokio::test] +async fn test_get_omconfig_from_remote_flake_with_attr() { + let om_config = OmConfig::get( + &FlakeUrl::from_str( + "github:juspay/omnix/0ed2a389d6b4c8eb78caed778e20e872d2a59973#default.omnix", + ) + .unwrap(), + ) + .await; + assert!(om_config.is_ok()); +}