-
Notifications
You must be signed in to change notification settings - Fork 195
feat: add key method #639
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
Open
polothy
wants to merge
1
commit into
aws-cloudformation:main
Choose a base branch
from
polothy:feat-key
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
feat: add key method #639
Changes from all commits
Commits
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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1194,6 +1194,7 @@ pub(crate) enum FunctionName { | |||||
| ToLower, | ||||||
| ToUpper, | ||||||
| UrlDecode, | ||||||
| Key, // <-- add this line | ||||||
| } | ||||||
|
|
||||||
| impl FunctionName { | ||||||
|
|
@@ -1213,6 +1214,7 @@ impl FunctionName { | |||||
| | FunctionName::ParseEpoch | ||||||
| | FunctionName::ParseChar => 1, | ||||||
| FunctionName::Now => 0, | ||||||
| FunctionName::Key => 1, // key expects 1 argument | ||||||
|
Author
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.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -1235,6 +1237,7 @@ impl std::fmt::Display for FunctionName { | |||||
| FunctionName::ToLower => "to_lower", | ||||||
| FunctionName::ToUpper => "to_upper", | ||||||
| FunctionName::UrlDecode => "url_decode", | ||||||
| FunctionName::Key => "key", | ||||||
| }; | ||||||
| write!(f, "{}", name) | ||||||
| } | ||||||
|
|
@@ -1260,6 +1263,7 @@ impl TryFrom<&str> for FunctionName { | |||||
| "to_lower" => Ok(FunctionName::ToLower), | ||||||
| "to_upper" => Ok(FunctionName::ToUpper), | ||||||
| "url_decode" => Ok(FunctionName::UrlDecode), | ||||||
| "key" => Ok(FunctionName::Key), | ||||||
| _ => Err(Error::ParseError(format!( | ||||||
| "No function with the name '{name}' exists.", | ||||||
| ))), | ||||||
|
|
@@ -1282,6 +1286,7 @@ struct ParseBooleanFunction; | |||||
| struct ParseCharFunction; | ||||||
| struct ParseEpochFunction; | ||||||
| struct NowFunction; | ||||||
| struct KeyFunction; | ||||||
|
|
||||||
| trait Callable { | ||||||
| fn call(&self, args: &[Vec<QueryResult>]) -> Result<Vec<Option<PathAwareValue>>>; | ||||||
|
|
@@ -1305,6 +1310,7 @@ impl Callable for FunctionName { | |||||
| FunctionName::ParseChar => ParseCharFunction.call(args), | ||||||
| FunctionName::ParseEpoch => ParseEpochFunction.call(args), | ||||||
| FunctionName::Now => NowFunction.call(args), | ||||||
| FunctionName::Key => KeyFunction.call(args), | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -2471,6 +2477,13 @@ pub(crate) fn resolve_function<'value, 'eval, 'loc: 'value>( | |||||
| .collect::<Vec<_>>()) | ||||||
| } | ||||||
|
|
||||||
| impl Callable for KeyFunction { | ||||||
| fn call(&self, args: &[Vec<QueryResult>]) -> Result<Vec<Option<PathAwareValue>>> { | ||||||
| use crate::rules::functions::key::key; | ||||||
| Ok(vec![Some(key(&args[0]))]) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
| #[path = "eval_context_tests.rs"] | ||||||
| pub(super) mod eval_context_tests; | ||||||
This file contains hidden or 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,4 +1,5 @@ | ||
| pub(crate) mod collections; | ||
| pub mod converters; | ||
| pub mod date_time; | ||
| pub(crate) mod key; | ||
| pub(crate) mod strings; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use crate::rules::path_value::{Path, PathAwareValue}; | ||
| use crate::rules::QueryResult; | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "key_tests.rs"] | ||
| mod key_tests; | ||
|
|
||
| pub(crate) fn key(args: &[QueryResult]) -> PathAwareValue { | ||
| // Return the key (logical id) for the first resolved argument | ||
| for arg in args { | ||
| match arg { | ||
| QueryResult::Resolved(val) | QueryResult::Literal(val) => { | ||
| let path = val.self_path(); | ||
| // Use relative() to get the last segment/key | ||
| let key = path.relative(); | ||
| return PathAwareValue::String((path.clone(), key.to_string())); | ||
| } | ||
| QueryResult::UnResolved(unresolved) => { | ||
| let path = unresolved.traversed_to.self_path(); | ||
| let key = path.relative(); | ||
| return PathAwareValue::String((path.clone(), key.to_string())); | ||
| } | ||
| } | ||
| } | ||
| // If no key found, return empty string at root | ||
| PathAwareValue::String((Path::root(), String::new())) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #[cfg(test)] | ||
| use crate::rules::functions::key; | ||
| use crate::rules::path_value::{Path, PathAwareValue}; | ||
| use crate::rules::QueryResult; | ||
| use std::rc::Rc; | ||
|
|
||
| #[test] | ||
| fn test_key_function_returns_last_segment() { | ||
| let path = Path::new("/Resources/MyBucket".to_string(), 0, 0); | ||
| let value = PathAwareValue::String((path.clone(), "test-value".to_string())); | ||
| let result = key::key(&[QueryResult::Resolved(Rc::new(value))]); | ||
| if let PathAwareValue::String((_, key_str)) = result { | ||
| assert_eq!(key_str, "MyBucket"); | ||
| } else { | ||
| panic!("Expected String result"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_key_function_empty_args() { | ||
| let result = key::key(&[]); | ||
| if let PathAwareValue::String((_, key_str)) = result { | ||
| assert_eq!(key_str, ""); | ||
| } else { | ||
| panic!("Expected String result"); | ||
| } | ||
| } |
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.