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

Remove ToJsonVector #952

Merged
merged 1 commit into from
Jul 21, 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: 0 additions & 1 deletion sdk/data_cosmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ mod cosmos_entity;
mod headers;
mod resource_quota;
mod time_nonce;
mod to_json_vector;

pub(crate) use authorization_policy::AuthorizationPolicy;
pub(crate) use time_nonce::TimeNonce;
Expand Down
88 changes: 86 additions & 2 deletions sdk/data_cosmos/src/resources/stored_procedure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Utilities for interacting with [`StoredProcedure`]s.

/// Stored procedure's parameters
pub type Parameters = crate::to_json_vector::ToJsonVector;
use std::fmt::Debug;

use azure_core::error::ResultExt;
use serde::Serialize;

/// A piece of application logic that is registered and executed against a collection as a single transaction
///
Expand Down Expand Up @@ -32,3 +34,85 @@ impl StoredProcedure {
&self.id
}
}

/// A list of parameters passed to the stored procedure.
#[derive(Clone)]
pub struct Parameters {
vec: Vec<String>,
}

impl Parameters {
/// Create a new parameter list.
pub fn new() -> Self {
Self { vec: Vec::new() }
}

/// Push a parameter on to the list.
pub fn push<T: Serialize>(&mut self, item: &T) -> azure_core::Result<()> {
self.vec.push(serde_json::to_string(item).with_context(
azure_core::error::ErrorKind::DataConversion,
|| {
let ty = std::any::type_name::<T>();
format!("failed to convert `{ty}` to StoredProcedure parameter")
},
)?);
Ok(())
}

/// Convert the list to json
pub(crate) fn to_json(&self) -> String {
let mut result = String::from("[");
let items = self.vec.join(", ");
result.push_str(&items);
result.push(']');
result
}
}

impl Debug for Parameters {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.to_json())
}
}

impl<T, U> From<T> for Parameters
where
T: IntoIterator<Item = U>,
U: Serialize,
{
fn from(iter: T) -> Self {
let mut params = Self::new();
for item in iter {
params.push(&item).unwrap();
}
params
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn serialize() {
let owned = "owned".to_owned();

let mut serialized = Parameters::new();
serialized.push(&"aaa").unwrap();
serialized.push(&owned).unwrap();
serialized.push(&100u64).unwrap();
assert_eq!(serialized.to_json(), "[\"aaa\", \"owned\", 100]");

let vector = vec!["pollo", "arrosto"];
let parameters: Parameters = vector.into();
assert_eq!(parameters.to_json(), "[\"pollo\", \"arrosto\"]");

let array = ["pollo", "arrosto"];
let parameters: Parameters = array.into();
assert_eq!(parameters.to_json(), "[\"pollo\", \"arrosto\"]");

let slice = &["pollo", "arrosto"][..];
let parameters: Parameters = slice.into();
assert_eq!(parameters.to_json(), "[\"pollo\", \"arrosto\"]");
}
}
118 changes: 0 additions & 118 deletions sdk/data_cosmos/src/to_json_vector.rs

This file was deleted.