-
-
Notifications
You must be signed in to change notification settings - Fork 160
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
ref: Change the internal representation of project IDs from u64s to strings #452
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
use std::convert::TryFrom; | ||
use std::fmt; | ||
use std::str::FromStr; | ||
|
||
|
@@ -8,88 +7,45 @@ use thiserror::Error; | |
/// Raised if a project ID cannot be parsed from a string. | ||
#[derive(Debug, Error, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum ParseProjectIdError { | ||
/// Raised if the value is not an integer in the supported range. | ||
#[error("invalid value for project id")] | ||
InvalidValue, | ||
/// Raised if an empty value is parsed. | ||
#[error("empty or missing project id")] | ||
EmptyValue, | ||
} | ||
|
||
/// Represents a project ID. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)] | ||
pub struct ProjectId(u64); | ||
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)] | ||
pub struct ProjectId(String); | ||
|
||
impl ProjectId { | ||
/// Creates a new project ID from its numeric value. | ||
/// Creates a new project ID from its string representation. | ||
/// This assumes that the string is already well-formed and URL | ||
/// encoded/decoded. | ||
#[inline] | ||
pub fn new(id: u64) -> Self { | ||
Self(id) | ||
pub fn new(id: &str) -> Self { | ||
Self(id.to_string()) | ||
} | ||
|
||
/// Returns the numeric value of this project id. | ||
/// Returns the string representation of the project ID. | ||
#[inline] | ||
pub fn value(self) -> u64 { | ||
self.0 | ||
pub fn value(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl fmt::Display for ProjectId { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.value()) | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
macro_rules! impl_from { | ||
($ty:ty) => { | ||
impl From<$ty> for ProjectId { | ||
#[inline] | ||
fn from(val: $ty) -> Self { | ||
Self::new(val as u64) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from!(u8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, we could keep the Try/From impls, as they would just stringify the given int. But its also fine to just remove it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll follow up on this in a different place, but i'm curious to hear the argument for keeping the Try/From impls. i've got an inkling of an idea, but it could just be completely wrong. i'm open to adding these back in a separate PR though. right now it feels like keeping impls just for integer values looks a little weird: why are there |
||
impl_from!(u16); | ||
impl_from!(u32); | ||
impl_from!(u64); | ||
|
||
macro_rules! impl_try_from { | ||
($ty:ty) => { | ||
impl TryFrom<$ty> for ProjectId { | ||
type Error = ParseProjectIdError; | ||
|
||
#[inline] | ||
fn try_from(val: $ty) -> Result<Self, Self::Error> { | ||
match u64::try_from(val) { | ||
Ok(id) => Ok(Self::new(id)), | ||
Err(_) => Err(ParseProjectIdError::InvalidValue), | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_try_from!(usize); | ||
impl_try_from!(i8); | ||
impl_try_from!(i16); | ||
impl_try_from!(i32); | ||
impl_try_from!(i64); | ||
|
||
impl FromStr for ProjectId { | ||
type Err = ParseProjectIdError; | ||
|
||
fn from_str(s: &str) -> Result<ProjectId, ParseProjectIdError> { | ||
if s.is_empty() { | ||
return Err(ParseProjectIdError::EmptyValue); | ||
} | ||
|
||
match s.parse::<u64>() { | ||
Ok(val) => Ok(ProjectId::new(val)), | ||
Err(_) => Err(ParseProjectIdError::InvalidValue), | ||
} | ||
Ok(ProjectId::new(s)) | ||
} | ||
} | ||
|
||
|
@@ -100,21 +56,21 @@ mod test { | |
#[test] | ||
fn test_basic_api() { | ||
let id: ProjectId = "42".parse().unwrap(); | ||
assert_eq!(id, ProjectId::new(42)); | ||
assert_eq!( | ||
"42xxx".parse::<ProjectId>(), | ||
Err(ParseProjectIdError::InvalidValue) | ||
); | ||
assert_eq!(id, ProjectId::new("42")); | ||
assert_eq!("42xxx".parse::<ProjectId>().unwrap().value(), "42xxx"); | ||
assert_eq!( | ||
"".parse::<ProjectId>(), | ||
Err(ParseProjectIdError::EmptyValue) | ||
); | ||
assert_eq!(ProjectId::new(42).to_string(), "42"); | ||
assert_eq!(ProjectId::new("42").to_string(), "42"); | ||
|
||
assert_eq!(serde_json::to_string(&ProjectId::new(42)).unwrap(), "42"); | ||
assert_eq!( | ||
serde_json::from_str::<ProjectId>("42").unwrap(), | ||
ProjectId::new(42) | ||
serde_json::to_string(&ProjectId::new("42")).unwrap(), | ||
"\"42\"" | ||
); | ||
assert_eq!( | ||
serde_json::from_str::<ProjectId>("\"42\"").unwrap(), | ||
ProjectId::new("42") | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in the description, this signature change may impact some users. I'm actually not too sure if too many folks actually make active use of this value seeing as it isn't required as a parameter or anything like that in the rest of the SDK's API. If I had to guess, this change is probably relatively low-impact but I could be wrong about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m wondering if it might be simpler to just return
&str
here, and completely remove theProjectId
type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leaving it as an explicit type makes sure it's opaque and user's can't be tempted to try and parse things out of it, so I'd keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was thinking about having this return
&str
initially and as suggested, to get rid of the type completely.if we were to switch to something like a UUID or otherwise well-structured unique identifier, we could add back in validation to this type that we'd removed. in an ideal situation this would mean not having to rewrite and convert all of the project ID code back to using a newtype.
even if we're unable to reuse the code at all, i think it would make it easier for us to easily find and replace instances of project IDs with their replacements in the future. as @flub has mentioned, having a separate type makes it harder for users to do too much with the ID, but i suppose it's not like we're trying particularly hard to prevent them from doing so either by exposing
ProjectId::value
.still, i'm tempted to keep the type because it helps explicitly express an opinion about what
ProjectId
s are and how to work with them. we'll see if this burns us sometime in the future 🙃