Correct renaming of path component identifiers (#34)

This commit is contained in:
Adam Leventhal 2022-03-08 14:31:56 -08:00 committed by GitHub
parent 305635e779
commit ae7c178e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 5 deletions

View File

@ -653,7 +653,18 @@ impl Generator {
(query_build, query_use)
};
let url_path = method.path.compile();
let url_path = method.path.compile(
method
.params
.iter()
.filter_map(|param| match &param.kind {
OperationParameterKind::Path => {
Some((&param.api_name, &param.name))
}
_ => None,
})
.collect(),
);
let body_func =
method.params.iter().filter_map(|param| match &param.kind {

View File

@ -1,5 +1,7 @@
// Copyright 2022 Oxide Computer Company
use std::collections::HashMap;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
@ -17,7 +19,7 @@ pub struct PathTemplate {
}
impl PathTemplate {
pub fn compile(&self) -> TokenStream {
pub fn compile(&self, rename: HashMap<&String, &String>) -> TokenStream {
let mut fmt = String::new();
fmt.push_str("{}");
for c in self.components.iter() {
@ -30,7 +32,12 @@ impl PathTemplate {
let components = self.components.iter().filter_map(|component| {
if let Component::Parameter(n) = &component {
let param = format_ident!("{}", n);
let param = format_ident!(
"{}",
rename
.get(&n)
.expect(&format!("missing path name mapping {}", n)),
);
Some(quote! {
progenitor_client::encode_path(&#param.to_string())
})
@ -159,6 +166,8 @@ impl ToString for PathTemplate {
#[cfg(test)]
mod test {
use std::collections::HashMap;
use super::{parse, Component, PathTemplate};
#[test]
@ -220,8 +229,11 @@ mod test {
#[test]
fn compile() {
let mut rename = HashMap::new();
let number = "number".to_string();
rename.insert(&number, &number);
let t = parse("/measure/{number}").unwrap();
let out = t.compile();
let out = t.compile(rename);
let want = quote::quote! {
let url = format!("{}/measure/{}",
self.baseurl,

View File

@ -53,7 +53,13 @@ impl Client {
in_: &'a str,
use_: &'a str,
) -> Result<ResponseValue<()>, Error<types::Error>> {
let url = format ! ("{}/{}/{}/{}" , self . baseurl , progenitor_client :: encode_path (& ref . to_string ()) , progenitor_client :: encode_path (& type . to_string ()) , progenitor_client :: encode_path (& trait . to_string ()) ,);
let url = format!(
"{}/{}/{}/{}",
self.baseurl,
progenitor_client::encode_path(&ref_.to_string()),
progenitor_client::encode_path(&type_.to_string()),
progenitor_client::encode_path(&trait_.to_string()),
);
let mut query = Vec::new();
query.push(("if", if_.to_string()));
query.push(("in", in_.to_string()));