Skip to content

Commit f0a772c

Browse files
authored
feat: refactor GetOptions with builder, add binary examples (#517)
* feat: Add get_range_opts, refactor GetOptions with builder * test: Fix test * refactor: Remove _opts functions, cargo fmt * chore: Clippy * docs: Add examples * fix: CI * fix: Silly wasm things * docs: Add examples inline to docs * fix: Update builder inputs to options * test: fix tests * fix: Bang my head on my keyboard * fix: Revert change trying to remove docs tests from wasm test
1 parent 19e7a8b commit f0a772c

File tree

6 files changed

+381
-126
lines changed

6 files changed

+381
-126
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ features = ["js"]
105105
[[test]]
106106
name = "get_range_file"
107107
path = "tests/get_range_file.rs"
108-
required-features = ["fs"]
108+
required-features = ["fs"]

src/buffered.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,7 @@ mod tests {
590590
writer.write_all(&[0; 5]).await.unwrap();
591591
writer.shutdown().await.unwrap();
592592
let response = store
593-
.get_opts(
594-
&path,
595-
GetOptions {
596-
head: true,
597-
..Default::default()
598-
},
599-
)
593+
.get_opts(&path, GetOptions::new().with_head(true))
600594
.await
601595
.unwrap();
602596
assert_eq!(response.meta.size, 25);
@@ -610,13 +604,7 @@ mod tests {
610604
writer.write_all(&[0; 20]).await.unwrap();
611605
writer.shutdown().await.unwrap();
612606
let response = store
613-
.get_opts(
614-
&path,
615-
GetOptions {
616-
head: true,
617-
..Default::default()
618-
},
619-
)
607+
.get_opts(&path, GetOptions::new().with_head(true))
620608
.await
621609
.unwrap();
622610
assert_eq!(response.meta.size, 40);
@@ -640,13 +628,7 @@ mod tests {
640628
.unwrap();
641629
writer.shutdown().await.unwrap();
642630
let response = store
643-
.get_opts(
644-
&path,
645-
GetOptions {
646-
head: true,
647-
..Default::default()
648-
},
649-
)
631+
.get_opts(&path, GetOptions::new().with_head(true))
650632
.await
651633
.unwrap();
652634
assert_eq!(response.meta.size, 25);
@@ -664,13 +646,7 @@ mod tests {
664646
.unwrap();
665647
writer.shutdown().await.unwrap();
666648
let response = store
667-
.get_opts(
668-
&path,
669-
GetOptions {
670-
head: true,
671-
..Default::default()
672-
},
673-
)
649+
.get_opts(&path, GetOptions::new().with_head(true))
674650
.await
675651
.unwrap();
676652
assert_eq!(response.meta.size, 40);

src/integration.rs

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ pub async fn put_get_delete_list(storage: &DynObjectStore) {
114114
let bytes = range_result.unwrap();
115115
assert_eq!(bytes, data.slice(range.start as usize..range.end as usize));
116116

117-
let opts = GetOptions {
118-
range: Some(GetRange::Bounded(2..5)),
119-
..Default::default()
120-
};
117+
let opts = GetOptions::new().with_range(Some(GetRange::Bounded(2..5)));
121118
let result = storage.get_opts(&location, opts).await.unwrap();
122119
// Data is `"arbitrary data"`, length 14 bytes
123120
assert_eq!(result.meta.size, 14); // Should return full object size (#5272)
@@ -131,20 +128,14 @@ pub async fn put_get_delete_list(storage: &DynObjectStore) {
131128
// Should be a non-fatal error
132129
out_of_range_result.unwrap_err();
133130

134-
let opts = GetOptions {
135-
range: Some(GetRange::Bounded(2..100)),
136-
..Default::default()
137-
};
131+
let opts = GetOptions::new().with_range(Some(GetRange::Bounded(2..100)));
138132
let result = storage.get_opts(&location, opts).await.unwrap();
139133
assert_eq!(result.range, 2..14);
140134
assert_eq!(result.meta.size, 14);
141135
let bytes = result.bytes().await.unwrap();
142136
assert_eq!(bytes, b"bitrary data".as_ref());
143137

144-
let opts = GetOptions {
145-
range: Some(GetRange::Suffix(2)),
146-
..Default::default()
147-
};
138+
let opts = GetOptions::new().with_range(Some(GetRange::Suffix(2)));
148139
match storage.get_opts(&location, opts).await {
149140
Ok(result) => {
150141
assert_eq!(result.range, 12..14);
@@ -156,10 +147,7 @@ pub async fn put_get_delete_list(storage: &DynObjectStore) {
156147
Err(e) => panic!("{e}"),
157148
}
158149

159-
let opts = GetOptions {
160-
range: Some(GetRange::Suffix(100)),
161-
..Default::default()
162-
};
150+
let opts = GetOptions::new().with_range(Some(GetRange::Suffix(100)));
163151
match storage.get_opts(&location, opts).await {
164152
Ok(result) => {
165153
assert_eq!(result.range, 0..14);
@@ -171,20 +159,14 @@ pub async fn put_get_delete_list(storage: &DynObjectStore) {
171159
Err(e) => panic!("{e}"),
172160
}
173161

174-
let opts = GetOptions {
175-
range: Some(GetRange::Offset(3)),
176-
..Default::default()
177-
};
162+
let opts = GetOptions::new().with_range(Some(GetRange::Offset(3)));
178163
let result = storage.get_opts(&location, opts).await.unwrap();
179164
assert_eq!(result.range, 3..14);
180165
assert_eq!(result.meta.size, 14);
181166
let bytes = result.bytes().await.unwrap();
182167
assert_eq!(bytes, b"itrary data".as_ref());
183168

184-
let opts = GetOptions {
185-
range: Some(GetRange::Offset(100)),
186-
..Default::default()
187-
};
169+
let opts = GetOptions::new().with_range(Some(GetRange::Offset(100)));
188170
storage.get_opts(&location, opts).await.unwrap_err();
189171

190172
let ranges = vec![0..1, 2..3, 0..5];
@@ -520,76 +502,55 @@ pub async fn get_opts(storage: &dyn ObjectStore) {
520502
storage.put(&path, "foo".into()).await.unwrap();
521503
let meta = storage.head(&path).await.unwrap();
522504

523-
let options = GetOptions {
524-
if_unmodified_since: Some(meta.last_modified),
525-
..GetOptions::default()
526-
};
505+
let options = GetOptions::new().with_if_unmodified_since(Some(meta.last_modified));
527506
match storage.get_opts(&path, options).await {
528507
Ok(_) | Err(Error::NotSupported { .. }) => {}
529508
Err(e) => panic!("{e}"),
530509
}
531510

532-
let options = GetOptions {
533-
if_unmodified_since: Some(meta.last_modified + chrono::Duration::try_hours(10).unwrap()),
534-
..GetOptions::default()
535-
};
511+
let options = GetOptions::new().with_if_unmodified_since(Some(
512+
meta.last_modified + chrono::Duration::try_hours(10).unwrap(),
513+
));
536514
match storage.get_opts(&path, options).await {
537515
Ok(_) | Err(Error::NotSupported { .. }) => {}
538516
Err(e) => panic!("{e}"),
539517
}
540518

541-
let options = GetOptions {
542-
if_unmodified_since: Some(meta.last_modified - chrono::Duration::try_hours(10).unwrap()),
543-
..GetOptions::default()
544-
};
519+
let options = GetOptions::new().with_if_unmodified_since(Some(
520+
meta.last_modified - chrono::Duration::try_hours(10).unwrap(),
521+
));
545522
match storage.get_opts(&path, options).await {
546523
Err(Error::Precondition { .. } | Error::NotSupported { .. }) => {}
547524
d => panic!("{d:?}"),
548525
}
549526

550-
let options = GetOptions {
551-
if_modified_since: Some(meta.last_modified),
552-
..GetOptions::default()
553-
};
527+
let options = GetOptions::new().with_if_modified_since(Some(meta.last_modified));
554528
match storage.get_opts(&path, options).await {
555529
Err(Error::NotModified { .. } | Error::NotSupported { .. }) => {}
556530
d => panic!("{d:?}"),
557531
}
558532

559-
let options = GetOptions {
560-
if_modified_since: Some(meta.last_modified - chrono::Duration::try_hours(10).unwrap()),
561-
..GetOptions::default()
562-
};
533+
let options = GetOptions::new().with_if_modified_since(Some(
534+
meta.last_modified - chrono::Duration::try_hours(10).unwrap(),
535+
));
563536
match storage.get_opts(&path, options).await {
564537
Ok(_) | Err(Error::NotSupported { .. }) => {}
565538
Err(e) => panic!("{e}"),
566539
}
567540

568541
let tag = meta.e_tag.unwrap();
569-
let options = GetOptions {
570-
if_match: Some(tag.clone()),
571-
..GetOptions::default()
572-
};
542+
let options = GetOptions::new().with_if_match(Some(tag.clone()));
573543
storage.get_opts(&path, options).await.unwrap();
574544

575-
let options = GetOptions {
576-
if_match: Some("invalid".to_string()),
577-
..GetOptions::default()
578-
};
545+
let options = GetOptions::new().with_if_match(Some("invalid".to_string()));
579546
let err = storage.get_opts(&path, options).await.unwrap_err();
580547
assert!(matches!(err, Error::Precondition { .. }), "{err}");
581548

582-
let options = GetOptions {
583-
if_none_match: Some(tag.clone()),
584-
..GetOptions::default()
585-
};
549+
let options = GetOptions::new().with_if_none_match(Some(tag.clone()));
586550
let err = storage.get_opts(&path, options).await.unwrap_err();
587551
assert!(matches!(err, Error::NotModified { .. }), "{err}");
588552

589-
let options = GetOptions {
590-
if_none_match: Some("invalid".to_string()),
591-
..GetOptions::default()
592-
};
553+
let options = GetOptions::new().with_if_none_match(Some("invalid".to_string()));
593554
storage.get_opts(&path, options).await.unwrap();
594555

595556
let result = storage.put(&path, "test".into()).await.unwrap();
@@ -599,26 +560,17 @@ pub async fn get_opts(storage: &dyn ObjectStore) {
599560
let meta = storage.head(&path).await.unwrap();
600561
assert_eq!(meta.e_tag.unwrap(), new_tag);
601562

602-
let options = GetOptions {
603-
if_match: Some(new_tag),
604-
..GetOptions::default()
605-
};
563+
let options = GetOptions::new().with_if_match(Some(new_tag.clone()));
606564
storage.get_opts(&path, options).await.unwrap();
607565

608-
let options = GetOptions {
609-
if_match: Some(tag),
610-
..GetOptions::default()
611-
};
566+
let options = GetOptions::new().with_if_match(Some(tag));
612567
let err = storage.get_opts(&path, options).await.unwrap_err();
613568
assert!(matches!(err, Error::Precondition { .. }), "{err}");
614569

615570
if let Some(version) = meta.version {
616571
storage.put(&path, "bar".into()).await.unwrap();
617572

618-
let options = GetOptions {
619-
version: Some(version),
620-
..GetOptions::default()
621-
};
573+
let options = GetOptions::new().with_version(Some(version));
622574

623575
// Can retrieve previous version
624576
let get_opts = storage.get_opts(&path, options).await.unwrap();

0 commit comments

Comments
 (0)