Skip to content

Commit cfca5f3

Browse files
committed
feat(cosmos): expand PartitionKey From impl examples and tests (#3570)
1 parent b9041b0 commit cfca5f3

1 file changed

Lines changed: 183 additions & 0 deletions

File tree

sdk/cosmos/azure_data_cosmos/src/partition_key.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,57 @@ impl From<InnerPartitionKeyValue> for PartitionKeyValue {
215215
}
216216

217217
impl From<&'static str> for PartitionKeyValue {
218+
/// Creates a [`PartitionKeyValue`] from a string slice.
219+
///
220+
/// # Examples
221+
///
222+
/// ```rust
223+
/// use azure_data_cosmos::PartitionKey;
224+
/// let pk = PartitionKey::from("my-partition-key");
225+
/// ```
218226
fn from(value: &'static str) -> Self {
219227
InnerPartitionKeyValue::String(value.to_string()).into()
220228
}
221229
}
222230

223231
impl From<String> for PartitionKeyValue {
232+
/// Creates a [`PartitionKeyValue`] from a [`String`].
233+
///
234+
/// # Examples
235+
///
236+
/// ```rust
237+
/// use azure_data_cosmos::PartitionKey;
238+
/// let pk = PartitionKey::from(String::from("my-partition-key"));
239+
/// ```
224240
fn from(value: String) -> Self {
225241
InnerPartitionKeyValue::String(value).into()
226242
}
227243
}
228244

229245
impl From<&String> for PartitionKeyValue {
246+
/// Creates a [`PartitionKeyValue`] from a [`&String`].
247+
///
248+
/// # Examples
249+
///
250+
/// ```rust
251+
/// use azure_data_cosmos::PartitionKey;
252+
/// let pk = PartitionKey::from(&String::from("my-partition-key"));
253+
/// ```
230254
fn from(value: &String) -> Self {
231255
InnerPartitionKeyValue::String(value.clone()).into()
232256
}
233257
}
234258

235259
impl From<Cow<'static, str>> for PartitionKeyValue {
260+
/// Creates a [`PartitionKeyValue`] from a [`Cow<'static, str>`].
261+
///
262+
/// # Examples
263+
///
264+
/// ```rust
265+
/// use azure_data_cosmos::PartitionKey;
266+
/// use std::borrow::Cow;
267+
/// let pk = PartitionKey::from(Cow::Borrowed("my-pk"));
268+
/// ```
236269
fn from(value: Cow<'static, str>) -> Self {
237270
InnerPartitionKeyValue::String(value.into_owned()).into()
238271
}
@@ -241,6 +274,14 @@ impl From<Cow<'static, str>> for PartitionKeyValue {
241274
macro_rules! impl_from_number {
242275
($source_type: ty) => {
243276
impl From<$source_type> for PartitionKeyValue {
277+
/// Creates a [`PartitionKeyValue`] from a numeric value.
278+
///
279+
/// # Examples
280+
///
281+
/// ```rust
282+
/// use azure_data_cosmos::PartitionKey;
283+
/// let pk = PartitionKey::from(42);
284+
/// ```
244285
fn from(value: $source_type) -> Self {
245286
InnerPartitionKeyValue::Number(value as f64).into()
246287
}
@@ -259,6 +300,20 @@ impl_from_number!(u64);
259300
impl_from_number!(u8);
260301
impl_from_number!(usize);
261302

303+
impl From<bool> for PartitionKeyValue {
304+
/// Creates a [`PartitionKeyValue`] from a [`bool`].
305+
///
306+
/// # Examples
307+
///
308+
/// ```rust
309+
/// use azure_data_cosmos::PartitionKey;
310+
/// let pk = PartitionKey::from(true);
311+
/// ```
312+
fn from(value: bool) -> Self {
313+
InnerPartitionKeyValue::Bool(value).into()
314+
}
315+
}
316+
262317
impl From<f32> for PartitionKeyValue {
263318
/// Creates a [`PartitionKeyValue`] from an `f32`.
264319
///
@@ -267,6 +322,13 @@ impl From<f32> for PartitionKeyValue {
267322
/// # Panics
268323
///
269324
/// This method panics if given an Infinite or NaN value.
325+
///
326+
/// # Examples
327+
///
328+
/// ```rust
329+
/// use azure_data_cosmos::PartitionKey;
330+
/// let pk = PartitionKey::from(4.2f32);
331+
/// ```
270332
fn from(value: f32) -> Self {
271333
assert!(
272334
!value.is_infinite() && !value.is_nan(),
@@ -282,6 +344,13 @@ impl From<f64> for PartitionKeyValue {
282344
/// # Panics
283345
///
284346
/// This method panics if given an Infinite or NaN value.
347+
///
348+
/// # Examples
349+
///
350+
/// ```rust
351+
/// use azure_data_cosmos::PartitionKey;
352+
/// let pk = PartitionKey::from(12.34);
353+
/// ```
285354
fn from(value: f64) -> Self {
286355
assert!(
287356
!value.is_infinite() && !value.is_nan(),
@@ -292,6 +361,17 @@ impl From<f64> for PartitionKeyValue {
292361
}
293362

294363
impl<T: Into<PartitionKeyValue>> From<Option<T>> for PartitionKeyValue {
364+
/// Creates a [`PartitionKeyValue`] from an [`Option<T>`].
365+
///
366+
/// If the value is `None`, it is treated as a null partition key.
367+
///
368+
/// # Examples
369+
///
370+
/// ```rust
371+
/// use azure_data_cosmos::PartitionKey;
372+
/// let pk = PartitionKey::from(Some("tenant1"));
373+
/// let pk_null = PartitionKey::from(None::<&str>);
374+
/// ```
295375
fn from(value: Option<T>) -> Self {
296376
match value {
297377
Some(t) => t.into(),
@@ -301,6 +381,16 @@ impl<T: Into<PartitionKeyValue>> From<Option<T>> for PartitionKeyValue {
301381
}
302382

303383
impl From<()> for PartitionKey {
384+
/// Creates a [`PartitionKey`] that represents an empty list of partition key values.
385+
///
386+
/// This is used to signal a cross-partition query.
387+
///
388+
/// # Examples
389+
///
390+
/// ```rust
391+
/// use azure_data_cosmos::PartitionKey;
392+
/// let pk = PartitionKey::from(());
393+
/// ```
304394
fn from(_: ()) -> Self {
305395
PartitionKey::EMPTY
306396
}
@@ -343,6 +433,14 @@ impl From<Vec<PartitionKeyValue>> for PartitionKey {
343433
}
344434

345435
impl<T: Into<PartitionKeyValue>> From<T> for PartitionKey {
436+
/// Creates a single-level [`PartitionKey`] from any type that can be converted into a [`PartitionKeyValue`].
437+
///
438+
/// # Examples
439+
///
440+
/// ```rust
441+
/// use azure_data_cosmos::PartitionKey;
442+
/// let pk = PartitionKey::from("tenant1");
443+
/// ```
346444
fn from(value: T) -> Self {
347445
PartitionKey(vec![value.into()])
348446
}
@@ -351,6 +449,14 @@ impl<T: Into<PartitionKeyValue>> From<T> for PartitionKey {
351449
macro_rules! impl_from_tuple {
352450
($($n:tt $name:ident)*) => {
353451
impl<$($name: Into<PartitionKeyValue>),*> From<($($name,)*)> for PartitionKey {
452+
/// Creates a hierarchical [`PartitionKey`] from a tuple of values.
453+
///
454+
/// # Examples
455+
///
456+
/// ```rust
457+
/// use azure_data_cosmos::PartitionKey;
458+
/// let pk = PartitionKey::from(("parent", "child"));
459+
/// ```
354460
fn from(value: ($($name,)*)) -> Self {
355461
PartitionKey(vec![$(
356462
value.$n.into()
@@ -590,4 +696,81 @@ mod tests {
590696
let partition_key = PartitionKey::from(keys);
591697
assert_eq!(key_to_string(partition_key), r#"["tenant1",{}]"#);
592698
}
699+
700+
#[test]
701+
fn bool_values() {
702+
assert_eq!(key_to_string(true), r#"[true]"#);
703+
assert_eq!(key_to_string(false), r#"[false]"#);
704+
}
705+
706+
#[test]
707+
fn string_variants() {
708+
// String
709+
assert_eq!(
710+
key_to_string(String::from("my_partition_key")),
711+
r#"["my_partition_key"]"#
712+
);
713+
// &String
714+
let s = String::from("my_partition_key");
715+
assert_eq!(key_to_string(&s), r#"["my_partition_key"]"#);
716+
// Cow
717+
assert_eq!(
718+
key_to_string(std::borrow::Cow::<'static, str>::Borrowed(
719+
"my_partition_key"
720+
)),
721+
r#"["my_partition_key"]"#
722+
);
723+
}
724+
725+
#[test]
726+
fn hierarchical_2_level() {
727+
assert_eq!(key_to_string(("parent", "child")), r#"["parent","child"]"#);
728+
}
729+
730+
#[test]
731+
fn hierarchical_3_level() {
732+
assert_eq!(
733+
key_to_string(("tenant", "user", 123)),
734+
r#"["tenant","user",123]"#
735+
);
736+
}
737+
738+
#[test]
739+
fn options_exhaustive() {
740+
assert_eq!(key_to_string(Some(true)), r#"[true]"#);
741+
assert_eq!(key_to_string(None::<bool>), r#"[null]"#);
742+
assert_eq!(key_to_string(Some(42)), r#"[42]"#);
743+
assert_eq!(key_to_string(Some(String::from("val"))), r#"["val"]"#);
744+
}
745+
746+
#[test]
747+
fn wrapped_single_value() {
748+
// Test that From<T> for PartitionKey wraps correctly
749+
let pk: PartitionKey = "single".into();
750+
assert_eq!(key_to_string(pk), r#"["single"]"#);
751+
}
752+
753+
#[test]
754+
#[should_panic(expected = "value should be a non-infinite number")]
755+
fn f32_nan_panic() {
756+
let _ = PartitionKey::from(f32::NAN);
757+
}
758+
759+
#[test]
760+
#[should_panic(expected = "value should be a non-infinite number")]
761+
fn f32_infinity_panic() {
762+
let _ = PartitionKey::from(f32::INFINITY);
763+
}
764+
765+
#[test]
766+
#[should_panic(expected = "value should be a non-infinite number")]
767+
fn f64_nan_panic() {
768+
let _ = PartitionKey::from(f64::NAN);
769+
}
770+
771+
#[test]
772+
#[should_panic(expected = "value should be a non-infinite number")]
773+
fn f64_infinity_panic() {
774+
let _ = PartitionKey::from(f64::INFINITY);
775+
}
593776
}

0 commit comments

Comments
 (0)