-
Notifications
You must be signed in to change notification settings - Fork 2
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
Prathiksha-Nataraja
wants to merge
4
commits into
main
Choose a base branch
from
handling-rust-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f37a15f
fix: handle rustypes List, Tuple, HashMap, Struct
Prathiksha-Nataraja 4ddcbfb
chore: update handling rustypes
Prathiksha-Nataraja 1311ffc
chore: update handling for the Rustype struct, value
Prathiksha-Nataraja d235e9f
Delete test/main.echo
Prathiksha-Nataraja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,91 @@ | ||
use super::*; | ||
use anyhow::anyhow; | ||
|
||
fn validate_value(value: &serde_json::Value, expected_type: &RustType) -> anyhow::Result<()> { | ||
match expected_type { | ||
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::Struct(_) => {} | ||
|
||
RustType::Value => { | ||
if !value.is_object() { | ||
return Err(anyhow!("value must be a Json Object")); | ||
} | ||
} | ||
|
||
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)?, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -136,6 +222,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, | ||
|
@@ -150,40 +237,8 @@ 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)?; | ||
|
||
Some(value_str) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.