-
Notifications
You must be signed in to change notification settings - Fork 55
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
[refactor] Make StructType and DataType::struct_type more general #385
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #385 +/- ##
=======================================
Coverage 77.67% 77.68%
=======================================
Files 49 49
Lines 10089 10084 -5
Branches 10089 10084 -5
=======================================
- Hits 7837 7834 -3
Misses 1805 1805
+ Partials 447 445 -2 ☔ View full report in Codecov by Sentry. |
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.
awesome this is better ergonomics! one question but LGTM
Self::Struct(data) => DataType::struct_type(data.fields.clone()), | ||
Self::Array(data) => DataType::array_type(data.tpe.clone()), | ||
Self::Array(data) => data.tpe.clone().into(), |
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.
do we have the same From
impl for the struct_type above?
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.
No, because it seemed one level of indirection too many to have From<Iterator>
.
I guess we could consider implementing FromIterator for StructType
? But we'd still need either DataType::struct_type
or another impl FromIterator
... and at that point things are uncomfortably indirect?
let data_type: DataType = some_iter.map(...).collect();
Also, there's no corresponding TryFromIterator
trait to replace StructType::try_new
and DataType::try_struct_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.
yea makes sense. seems fine to leave :)
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.
Oh. My. Maybe we should impl FromIterator
. This actually works:
impl FromIterator<StructField> for StructType {
fn from_iter<T: IntoIterator<Item = StructField>>(iter: T) -> Self {
Self {
type_name: "struct".into(),
fields: iter.into_iter().map(|f| (f.name.clone(), f)).collect(),
}
}
}
impl StructType {
pub fn new(fields: impl IntoIterator<Item = StructField>) -> Self {
fields.into_iter().collect()
}
pub fn try_new<E>(fields: impl IntoIterator<Item = Result<StructField, E>>) -> Result<Self, E> {
fields.into_iter().try_collect()
}
(Itertools::try_collect
Just Works)
But this would only be to make new
and try_new
nicer -- I checked all the various call sites and almost none of them benefit from using collect directly.
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 agree that's nice, but it'll be a mess if we ever need another argument for creating a struct, so given only local benefit I'd err on the side of not implementing FromIterator
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.
nice, thanks!
Self::Struct(data) => DataType::struct_type(data.fields.clone()), | ||
Self::Array(data) => DataType::array_type(data.tpe.clone()), | ||
Self::Array(data) => data.tpe.clone().into(), |
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 agree that's nice, but it'll be a mess if we ever need another argument for creating a struct, so given only local benefit I'd err on the side of not implementing FromIterator
Constructing new
StructType
instances has historically been more verbose than necessary because they require aVec<StructField>
. Update the various constructors to acceptimpl IntoIterator
instead, and simplify call sites to take advantage.