Skip to content

Commit

Permalink
Merge pull request #1208 from rainlanguage/2025-01-22-selected-deploy…
Browse files Browse the repository at this point in the history
…ment-context

Update parsing logic with additional context
  • Loading branch information
hardyjosh authored Jan 23, 2025
2 parents d3e715b + 7004c1d commit 6fdbbea
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/js_api/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl DotrainOrderGui {
let gui = self
.dotrain_order
.dotrain_yaml()
.get_gui()?
.get_gui(Some(self.selected_deployment.clone()))?
.ok_or(GuiError::GuiConfigNotFound)?;
Ok(gui)
}
Expand Down
23 changes: 15 additions & 8 deletions crates/settings/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use strict_yaml_rust::StrictYaml;
use thiserror::Error;
use typeshare::typeshare;
use yaml::{
context::Context, default_document, require_hash, require_string, YamlError, YamlParsableHash,
context::{Context, GuiContextTrait},
default_document, require_hash, require_string, YamlError, YamlParsableHash,
};

#[cfg(target_family = "wasm")]
Expand Down Expand Up @@ -59,28 +60,34 @@ impl YamlParsableHash for Deployment {
) -> Result<HashMap<String, Self>, YamlError> {
let mut deployments = HashMap::new();

let orders = Order::parse_all_from_yaml(documents.clone(), context)?;

for document in &documents {
let document_read = document.read().map_err(|_| YamlError::ReadLockError)?;

if let Ok(deployments_hash) = require_hash(&document_read, Some("deployments"), None) {
for (key_yaml, deployment_yaml) in deployments_hash {
let deployment_key = key_yaml.as_str().unwrap_or_default().to_string();

if let Some(context) = context {
if let Some(current_deployment) = context.get_current_deployment() {
if current_deployment != &deployment_key {
continue;
}
}
}

let mut context = Context::from_context(context);

let order_key = require_string(
deployment_yaml,
Some("order"),
Some(format!(
"order string missing in deployment: {deployment_key}"
)),
)?;
let order = orders
.get(&order_key)
.ok_or_else(|| YamlError::KeyNotFound(order_key.clone()))?
.clone();
context.add_current_order(order_key.clone());

let mut context = Context::new();
let order =
Order::parse_from_yaml(documents.clone(), &order_key, Some(&context))?;
context.add_order(Arc::new(order.clone()));

let scenario = Scenario::parse_from_yaml(
Expand Down
17 changes: 13 additions & 4 deletions crates/settings/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
yaml::{
context::Context, default_document, get_hash_value, optional_hash, optional_string,
optional_vec, require_string, require_vec, YamlError, YamlParsableHash, YamlParseableValue,
context::{Context, GuiContextTrait},
default_document, get_hash_value, optional_hash, optional_string, optional_vec,
require_string, require_vec, YamlError, YamlParsableHash, YamlParseableValue,
},
Deployment, Token, TokenRef,
};
Expand Down Expand Up @@ -349,7 +350,7 @@ impl YamlParseableValue for Gui {

fn parse_from_yaml_optional(
documents: Vec<Arc<RwLock<StrictYaml>>>,
_: Option<&Context>,
context: Option<&Context>,
) -> Result<Option<Self>, YamlError> {
let mut gui_res: Option<Gui> = None;
let mut gui_deployments_res: HashMap<String, GuiDeployment> = HashMap::new();
Expand Down Expand Up @@ -397,7 +398,15 @@ impl YamlParseableValue for Gui {
for (deployment_name, deployment_yaml) in deployments {
let deployment_name = deployment_name.as_str().unwrap_or_default().to_string();

let mut context = Context::new();
if let Some(context) = context {
if let Some(current_deployment) = context.get_current_deployment() {
if current_deployment != &deployment_name {
continue;
}
}
}

let mut context = Context::from_context(context);

let select_tokens = match optional_vec(deployment_yaml, "select-tokens") {
Some(tokens) => Some(
Expand Down
10 changes: 9 additions & 1 deletion crates/settings/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strict_yaml_rust::StrictYaml;
use thiserror::Error;
use typeshare::typeshare;
use yaml::{
context::{Context, SelectTokensContext},
context::{Context, GuiContextTrait, SelectTokensContext},
default_document, optional_string, require_hash, require_string, require_vec, YamlError,
YamlParsableHash,
};
Expand Down Expand Up @@ -324,6 +324,14 @@ impl YamlParsableHash for Order {
for (key_yaml, order_yaml) in orders_hash {
let order_key = key_yaml.as_str().unwrap_or_default().to_string();

if let Some(context) = context {
if let Some(current_order) = context.get_current_order() {
if current_order != &order_key {
continue;
}
}
}

let mut network: Option<Arc<Network>> = None;

let deployer = match optional_string(order_yaml, "deployer") {
Expand Down
54 changes: 54 additions & 0 deletions crates/settings/src/yaml/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ use crate::{Order, OrderIO, Token};
use std::sync::Arc;
use thiserror::Error;

#[derive(Debug, Clone, Default)]
pub struct GuiContext {
pub current_deployment: Option<String>,
pub current_order: Option<String>,
}

#[derive(Debug, Clone, Default)]
pub struct Context {
pub order: Option<Arc<Order>>,
pub select_tokens: Option<Vec<String>>,
pub gui_context: Option<GuiContext>,
}

#[derive(Error, Debug, PartialEq)]
Expand Down Expand Up @@ -101,14 +108,45 @@ impl OrderContext for Context {
}
}

pub trait GuiContextTrait {
fn get_current_deployment(&self) -> Option<&String>;

fn get_current_order(&self) -> Option<&String>;
}

impl GuiContextTrait for Context {
fn get_current_deployment(&self) -> Option<&String> {
self.gui_context
.as_ref()
.and_then(|gui_context| gui_context.current_deployment.as_ref())
}

fn get_current_order(&self) -> Option<&String> {
self.gui_context
.as_ref()
.and_then(|gui_context| gui_context.current_order.as_ref())
}
}

impl Context {
pub fn new() -> Self {
Self {
order: None,
select_tokens: None,
gui_context: None,
}
}

pub fn from_context(context: Option<&Context>) -> Self {
let mut new_context = Self::new();
if let Some(context) = context {
new_context.order.clone_from(&context.order);
new_context.select_tokens.clone_from(&context.select_tokens);
new_context.gui_context.clone_from(&context.gui_context);
}
new_context
}

pub fn add_order(&mut self, order: Arc<Order>) -> &mut Self {
self.order = Some(order);
self
Expand All @@ -119,6 +157,22 @@ impl Context {
self
}

pub fn add_current_deployment(&mut self, deployment: String) -> &mut Self {
self.gui_context = Some(GuiContext {
current_deployment: Some(deployment),
current_order: None,
});
self
}

pub fn add_current_order(&mut self, order: String) -> &mut Self {
self.gui_context = Some(GuiContext {
current_deployment: None,
current_order: Some(order),
});
self
}

fn resolve_path(&self, path: &str) -> Result<String, ContextError> {
let parts: Vec<&str> = path.split('.').collect();

Expand Down
16 changes: 10 additions & 6 deletions crates/settings/src/yaml/dotrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ impl DotrainYaml {
Deployment::parse_from_yaml(self.documents.clone(), key, None)
}

pub fn get_gui(&self) -> Result<Option<Gui>, YamlError> {
Gui::parse_from_yaml_optional(self.documents.clone(), None)
pub fn get_gui(&self, current_deployment: Option<String>) -> Result<Option<Gui>, YamlError> {
let mut context = Context::new();
if let Some(deployment) = current_deployment {
context.add_current_deployment(deployment);
}
Gui::parse_from_yaml_optional(self.documents.clone(), Some(&context))
}
}

Expand Down Expand Up @@ -351,7 +355,7 @@ mod tests {
"order1"
);

let gui = dotrain_yaml.get_gui().unwrap().unwrap();
let gui = dotrain_yaml.get_gui(None).unwrap().unwrap();
assert_eq!(gui.name, "Test gui");
assert_eq!(gui.description, "Test description");
assert_eq!(gui.deployments.len(), 1);
Expand Down Expand Up @@ -577,7 +581,7 @@ mod tests {
fn test_handlebars() {
let dotrain_yaml = DotrainYaml::new(vec![HANDLEBARS_YAML.to_string()], false).unwrap();

let gui = dotrain_yaml.get_gui().unwrap().unwrap();
let gui = dotrain_yaml.get_gui(None).unwrap().unwrap();
let deployment = gui.deployments.get("deployment1").unwrap();

assert_eq!(
Expand Down Expand Up @@ -668,7 +672,7 @@ orders:
);

let dotrain_yaml = DotrainYaml::new(vec![missing_input_token_yaml], false).unwrap();
let error = dotrain_yaml.get_gui().unwrap_err();
let error = dotrain_yaml.get_gui(None).unwrap_err();
assert_eq!(
error,
YamlError::ParseError(
Expand All @@ -678,7 +682,7 @@ orders:
);

let dotrain_yaml = DotrainYaml::new(vec![missing_output_token_yaml], false).unwrap();
let error = dotrain_yaml.get_gui().unwrap_err();
let error = dotrain_yaml.get_gui(None).unwrap_err();
assert_eq!(
error,
YamlError::ParseError(
Expand Down

0 comments on commit 6fdbbea

Please sign in to comment.