refactor: kill dupe Offset type (#2230)

Closes STOR-522
This commit is contained in:
Philip Jenvey 2026-04-16 09:11:57 -07:00 committed by GitHub
parent 99625bb0ed
commit fd1ea29419
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 48 deletions

View File

@ -1,4 +1,4 @@
use std::{num::ParseIntError, str::FromStr};
use std::str::FromStr;
use actix_web::{Error, FromRequest, HttpRequest, dev::Payload, web::Query};
use futures::future::{LocalBoxFuture, TryFutureExt};
@ -13,45 +13,6 @@ use syncstorage_db::{Sorting, SyncTimestamp, params};
use super::{BATCH_MAX_IDS, RequestErrorLocation, VALID_ID_REGEX, request_error};
use crate::web::error::ValidationErrorKind;
#[derive(Debug, Default, Clone, Copy, Deserialize, Eq, PartialEq, Validate)]
#[serde(default)]
pub struct Offset {
pub timestamp: Option<SyncTimestamp>,
pub offset: u64,
}
impl From<Offset> for params::Offset {
fn from(offset: Offset) -> Self {
Self {
timestamp: offset.timestamp,
offset: offset.offset,
}
}
}
impl FromStr for Offset {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = match s.chars().position(|c| c == ':') {
None => Offset {
timestamp: None,
offset: s.parse::<u64>()?,
},
Some(_colon_position) => {
let mut parts = s.split(':');
let timestamp_string = parts.next().unwrap_or("0");
let timestamp = SyncTimestamp::from_milliseconds(timestamp_string.parse::<u64>()?);
let offset = parts.next().unwrap_or("0").parse::<u64>()?;
Offset {
timestamp: Some(timestamp),
offset,
}
}
};
Ok(result)
}
}
/// Verifies that the list of id's is not too long and that the ids are valid
pub fn validate_qs_ids(ids: &[String]) -> Result<(), ValidationError> {
if ids.len() > BATCH_MAX_IDS {
@ -87,13 +48,15 @@ where
}
}
pub fn deserialize_offset<'de, D>(deserializer: D) -> Result<Option<Offset>, D::Error>
pub fn deserialize_offset<'de, D>(deserializer: D) -> Result<Option<params::Offset>, D::Error>
where
D: Deserializer<'de>,
{
let maybe_str: Option<String> = Deserialize::deserialize(deserializer)?;
if let Some(val) = maybe_str {
return Ok(Some(Offset::from_str(&val).map_err(SerdeError::custom)?));
return Ok(Some(
params::Offset::from_str(&val).map_err(SerdeError::custom)?,
));
}
Ok(None)
}
@ -154,7 +117,7 @@ pub struct BsoQueryParams {
/// position at which to restart search (string)
#[serde(deserialize_with = "deserialize_offset")]
pub offset: Option<Offset>,
pub offset: Option<params::Offset>,
/// a comma-separated list of BSO ids (list of strings)
#[serde(deserialize_with = "deserialize_comma_sep_string", default)]
@ -235,7 +198,7 @@ mod tests {
use syncstorage_db::{Sorting, SyncTimestamp, params};
use super::{BsoQueryParams, Offset};
use super::{BsoQueryParams, params::Offset};
use crate::web::extractors::test_utils::{extract_body_as_str, make_state};
#[test]

View File

@ -311,7 +311,7 @@ pub async fn get_collection(
older: coll.query.older,
sort: coll.query.sort,
limit: coll.query.limit,
offset: coll.query.offset.map(Into::into),
offset: coll.query.offset,
ids: coll.query.ids.clone(),
full: coll.query.full,
collection: coll.collection.clone(),

View File

@ -60,7 +60,7 @@ uid_data! {
DeleteStorage,
}
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct Offset {
pub timestamp: Option<SyncTimestamp>,
pub offset: u64,

View File

@ -662,7 +662,7 @@ impl Db for SpannerDb {
AND collection_id = @collection_id
AND expiry > CURRENT_TIMESTAMP()";
let limit = params.limit.map(i64::from).unwrap_or(-1);
let params::Offset { offset, timestamp } = params.offset.clone().unwrap_or_default();
let params::Offset { offset, timestamp } = params.offset.unwrap_or_default();
let sort = params.sort;
let mut streaming = self.bsos_query(query, params).await?;
@ -694,7 +694,7 @@ impl Db for SpannerDb {
async fn get_bso_ids(&mut self, params: params::GetBsos) -> DbResult<results::GetBsoIds> {
let limit = params.limit.map(i64::from).unwrap_or(-1);
let params::Offset { offset, timestamp } = params.offset.clone().unwrap_or_default();
let params::Offset { offset, timestamp } = params.offset.unwrap_or_default();
let sort = params.sort;
let query = "\