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

fix: handle rust types #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
116 changes: 82 additions & 34 deletions echo-library/src/common/starlark_modules.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
use super::*;
use anyhow::anyhow;

fn validate_value(value: &serde_json::Value, expected_type: &RustType) -> anyhow::Result<()> {
match expected_type {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Validation for Value type not handled.

RustType::String => {
if !value.is_string() {
return Err(anyhow!("Value must be a string"));
}
}
RustType::Int => {
if !value.is_i64() {
return Err(anyhow!("Value must be an integer"));
}
}
RustType::Float => {
if !value.is_f64() {
return Err(anyhow!("Value must be a float"));
}
}
RustType::Uint => {
if !value.is_u64() {
return Err(anyhow!("Value must be a positive integer"));
}
}
RustType::Boolean => {
if !value.is_boolean() {
return Err(anyhow!("Value must be a boolean"));
}
}

RustType::HashMap(key_type, value_type) => {
if let Some(map) = value.as_object() {
for (key, val) in map.iter() {
validate_value(val, value_type)?;
}
} else {
return Err(anyhow!("Value must be a JSON object"));
}
}

RustType::List(item_type) => {
let parsed_array = value
.as_array()
.ok_or_else(|| anyhow!("Value must be a JSON array"))?;

for element in parsed_array.iter() {
validate_value(element, item_type)?;
}
}

RustType::Tuple(key_type, value_type) => {
let parsed_tuple = value
.as_array()
.ok_or_else(|| anyhow!("Value must be a JSON tuple with two elements"))?;

if parsed_tuple.len() != 2 {
return Err(anyhow!("Tuple must have exactly two elements"));
}

// Pattern matching for key and value types
match (&parsed_tuple[0], &parsed_tuple[1]) {
(serde_json::Value::String(key), value) => validate_value(value, value_type)?,
(serde_json::Value::Number(number), value) if number.is_i64() => {
validate_value(value, value_type)?
}
(serde_json::Value::Number(number), value) if number.is_f64() => {
validate_value(value, value_type)?
}
(serde_json::Value::Bool(bool_value), value) => validate_value(value, value_type)?,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Validation for tuple types, hashmaps, arrays, and similar structures is not handled.

_ => return Err(anyhow!("Unsupported tuple element types")),
}
}

_ => return Err(anyhow!("Unsupported input type for default value")),
}

Ok(())
}

#[allow(clippy::type_complexity)]
#[starlark_module]
pub fn starlark_workflow_module(builder: &mut GlobalsBuilder) {
Expand Down Expand Up @@ -136,6 +214,7 @@ pub fn starlark_workflow_module(builder: &mut GlobalsBuilder) {
///
/// * A Result containing the input object of `Input` type
///

fn argument(
name: String,
input_type: Value,
Expand All @@ -150,40 +229,9 @@ pub fn starlark_workflow_module(builder: &mut GlobalsBuilder) {
.to_json()
.map_err(|err| anyhow!("Failed to parse default value: {}", err))?;

match input_type {
RustType::String => {
if !value_str.contains("\"") {
return Err(anyhow!("Value must be in String type"));
}
}
RustType::Int => {
if value_str.parse::<i32>().is_err() {
return Err(anyhow!("Value must be an integer"));
}
}
RustType::Float => {
if value_str.parse::<f32>().is_err() {
return Err(anyhow!("Value must be a float"));
}
}
RustType::Uint => {
if value_str.parse::<u32>().is_err() {
return Err(anyhow!("Value must be a positive integer"));
}
}
RustType::Boolean => {
if value_str != "true" && value_str != "false" {
return Err(anyhow!("Value must be either true or false"));
}
}
RustType::HashMap(_, _) => {}
RustType::List(_) => {}
RustType::Tuple(_, _) => {}
RustType::Struct(_) => {}
_ => {
return Err(anyhow!("Unsupported input type for default value"));
}
}
let parsed_value = serde_json::from_str(&value_str)?;
validate_value(&parsed_value, &input_type)?;
// validate_value(&serde_json::from_str(&value_str)?, &input_type)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

code clean up


Some(value_str)
}
Expand Down
2 changes: 2 additions & 0 deletions echo-library/src/types/rust_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rayon::collections::btree_map::IterMut;
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove unused imports


use super::*;

#[derive(Debug, PartialEq, Eq, Allocative, ProvidesStaticType, Clone, Deserialize, Serialize)]
Expand Down
17 changes: 17 additions & 0 deletions test/main.echo
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove echo file

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
hello_world = task(
kind = "hello_world",
action_name = "hello_world",
input_arguments = [
argument(
name="name",
input_type = HashMap(String, Int),
default_value = {"ff" : 5}
),
],
)

workflows(
name = "test",
version = "0.0.1",
tasks = [hello_world]
)