Skip to content

skip query params already in x-ms-paths #790

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

Merged
merged 7 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions services/autorust/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ syn = { version = "1.0", features = ["parsing"] }
camino = "1.0"
askama = "0.11"
toml = "0.5"
qstring = "0.7"
45 changes: 40 additions & 5 deletions services/autorust/codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl<'a> CodeGen<'a> {
pub enum Error {
#[error("SpecError: {0}")]
Spec(#[from] spec::Error),
#[error(transparent)]
InvalidUri(#[from] http::uri::InvalidUri),
#[error("creating function name: {0}")]
FunctionName(#[source] crate::identifier::Error),
#[error("creating type name for schema ref: {0}")]
Expand All @@ -115,7 +117,7 @@ pub enum Error {
source: crate::identifier::Error,
property: String,
},
#[error("creating name for enum value {property}: {source}")]
#[error("creating name for enum value {property}")]
EnumValueName {
source: crate::identifier::Error,
property: String,
Expand Down Expand Up @@ -192,11 +194,27 @@ pub fn create_mod() -> TokenStream {
// any word character or `-` between curly braces
pub static PARAM_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\{([\w-]+)\}").unwrap());

pub fn parse_params(path: &str) -> Vec<String> {
/// Get a list of parameter names in the URI path
/// For example: "/storage/{storage-account-name}/sas/{sas-definition-name}"
/// Returns ["storage-account-name", "sas-definition-name"]
pub fn parse_path_params(path: &str) -> Vec<String> {
// capture 0 is the whole match and 1 is the actual capture like other languages
PARAM_RE.captures_iter(path).into_iter().map(|c| c[1].to_string()).collect()
}

/// Get a set of parameter names in the URI query
/// For example: "/?restype=service&comp=userdelegationkey"
/// Returns ["restype", "comp"]
pub fn parse_query_params(uri: &str) -> Result<HashSet<String>, Error> {
if let Some(n) = uri.find('?') {
let query = &uri[n..];
let qs = qstring::QString::from(query);
Ok(qs.into_iter().map(|(k, _)| k).collect())
} else {
Ok(HashSet::new())
}
}

#[derive(Clone)]
pub struct TypeNameCode {
type_path: TypePath,
Expand Down Expand Up @@ -452,10 +470,27 @@ mod tests {
use super::*;

#[test]
fn test_parse_params_keyvault() -> Result<(), Error> {
fn test_parse_query_params() -> Result<(), Error> {
let names = parse_query_params("/?restype=service&comp=userdelegationkey")?;
assert_eq!(2, names.len());
assert!(names.contains("restype"));
assert!(names.contains("comp"));
Ok(())
}

#[test]
fn test_parse_query_params_no_slash() -> Result<(), Error> {
let names = parse_query_params("?overload=EventGridEvent")?;
assert_eq!(1, names.len());
assert!(names.contains("overload"));
Ok(())
}

#[test]
fn test_parse_path_params_keyvault() -> Result<(), Error> {
assert_eq!(
parse_params("/storage/{storage-account-name}/sas/{sas-definition-name}"),
vec!["storage-account-name".to_owned(), "sas-definition-name".to_owned()]
parse_path_params("/storage/{storage-account-name}/sas/{sas-definition-name}"),
vec!["storage-account-name".to_string(), "sas-definition-name".to_string()]
);
Ok(())
}
Expand Down
Loading