-
Notifications
You must be signed in to change notification settings - Fork 36
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
Element reader #485
Element reader #485
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #485 +/- ##
==========================================
- Coverage 90.37% 89.45% -0.92%
==========================================
Files 78 77 -1
Lines 13749 13683 -66
==========================================
- Hits 12425 12240 -185
- Misses 1324 1443 +119
... and 9 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ 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.
🗺️ PR tour
@@ -1,10 +1,13 @@ | |||
// This module and its contents are visible as a workaround for: | |||
// https://github.com/amazon-ion/ion-rust/issues/484 |
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.
🗺️ Clickable link: #484.
@@ -11,7 +11,7 @@ use crate::Symbol; | |||
/// .push("foo") | |||
/// .build() | |||
/// .into(); | |||
/// let expected = Element::parse(r#"[1, true, "foo"]"#).unwrap(); | |||
/// let expected = Element::read_one(r#"[1, true, "foo"]"#).unwrap(); |
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.
🗺️ In the previous PR, we introduced the method Element::parse(ion_data) -> IonResult<Element>
.
The new ElementReader
trait has three read_
methods:
read_next_element()
read_one_element()
read_all_elements()
In this PR, I added analogous convenience methods to Element
:
Element::read_first()
-- (It's stateless, so you can't read more than the first one.)Element::read_one()
-- This was previously calledparse
. It enforces that the input has exactly one value.Element::read_all()
A lot of the changes that follow are renaming Element::parse
to Element::read_one
.
src/value/native_reader.rs
Outdated
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.
🗺️ The API for the NativeElementReader
was constrained by needing to be compatible with the IonCElementReader
which has since been removed. I copied over some helper method logic, but started with a fresh version of the ElementReader
trait.
)] | ||
#[case::struct_( | ||
ion_struct!{"greetings": "hello"}, | ||
Element::read_one(r#"{greetings: "hello"}"#).unwrap() |
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.
🗺️ These changes were merge conflicts with #481. There's no logic change.
}; | ||
|
||
reader.read_all(buffer.as_slice()) | ||
} |
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 able to remove the RoundTrip
trait and its implementations. The one-size-fits-all serialize
method below performs much of the same logic without needing the trait.
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.
🥳
} | ||
|
||
trait ElementApi { | ||
type ReaderApi: ElementReader; |
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 also able to remove this associated type. Now different readers can be configured and returned as the same (dynamically dispatched) Reader
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.
🥳
@@ -249,13 +230,15 @@ trait ElementApi { | |||
Ok(()) | |||
} | |||
|
|||
fn read_file(reader: &Self::ReaderApi, file_name: &str) -> IonResult<Vec<Element>> { | |||
// TODO have a better API that doesn't require buffering into memory everything... |
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.
🗺️ Removing this old comment; he native reader implementations don't have to load the entire input into memory. 🎉
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.
🎉
fn make_reader() -> Self::ReaderApi { | ||
NativeElementReader | ||
fn make_reader(data: &[u8]) -> IonResult<Reader<'_>> { | ||
ReaderBuilder::default().build(data) |
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.
🗺️ Constructs a blocking, format-agnostic reader.
@@ -645,8 +622,20 @@ mod non_blocking_native_element_tests { | |||
&[] | |||
} | |||
|
|||
fn make_reader() -> Self::ReaderApi { | |||
NonBlockingNativeElementReader | |||
fn make_reader(data: &[u8]) -> IonResult<Reader<'_>> { |
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.
🗺️ This method needs to construct a format-agnostic non-blocking reader, which isn't currently possible with the existing ReaderBuilder
. Fixing that is being tracked in #484.
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.
Is that worth leaving a comment here so that #484 cleanup can easily find all the relevant pieces of code?
src/value/native_reader.rs
Outdated
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!
} | ||
|
||
trait ElementApi { | ||
type ReaderApi: ElementReader; |
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.
🥳
}; | ||
|
||
reader.read_all(buffer.as_slice()) | ||
} |
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.
🥳
@@ -249,13 +230,15 @@ trait ElementApi { | |||
Ok(()) | |||
} | |||
|
|||
fn read_file(reader: &Self::ReaderApi, file_name: &str) -> IonResult<Vec<Element>> { | |||
// TODO have a better API that doesn't require buffering into memory everything... |
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.
🎉
@@ -645,8 +622,20 @@ mod non_blocking_native_element_tests { | |||
&[] | |||
} | |||
|
|||
fn make_reader() -> Self::ReaderApi { | |||
NonBlockingNativeElementReader | |||
fn make_reader(data: &[u8]) -> IonResult<Reader<'_>> { |
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.
Is that worth leaving a comment here so that #484 cleanup can easily find all the relevant pieces of code?
Co-authored-by: jobarr-amzn <[email protected]>
Co-authored-by: jobarr-amzn <[email protected]>
This PR introduces an updated
ElementReader
trait that provides methods for materializing values from the reader intoElement
s, and is available for all user-level reader implementations regardless of format or blocking strategy.Other changes:
Element
type that mirror the methodnames in the
ElementReader
trait, simplifying one-off parsing.Element::read_one()
,Element::read_next()
,Element::read_all()
NativeElementReader
, which used to be part of thenative
/ion-c
dichotomy.element-test-vectors
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.