Skip to content
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

Merged
merged 11 commits into from
Mar 16, 2023
Merged

Element reader #485

merged 11 commits into from
Mar 16, 2023

Conversation

zslayton
Copy link
Contributor

This PR introduces an updated ElementReader trait that provides methods for materializing values from the reader into Elements, and is available for all user-level reader implementations regardless of format or blocking strategy.

Other changes:

  • Adds static convenience methods to the Element type that mirror the method
    names in the ElementReader trait, simplifying one-off parsing.
    • Element::read_one(), Element::read_next(), Element::read_all()
  • Removes NativeElementReader, which used to be part of the native/ion-c dichotomy.
  • Streamlines some of the integration test logic in element-test-vectors.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov
Copy link

codecov bot commented Mar 15, 2023

Codecov Report

Patch coverage: 92.36% and project coverage change: -0.92 ⚠️

Comparison is base (747ee44) 90.37% compared to head (f19d455) 89.45%.

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     
Impacted Files Coverage Δ
src/ion_hash/mod.rs 100.00% <ø> (ø)
src/lib.rs 88.84% <ø> (ø)
src/reader.rs 80.18% <ø> (-2.39%) ⬇️
src/value/mod.rs 90.76% <ø> (ø)
src/value/reader.rs 92.94% <87.34%> (-7.06%) ⬇️
src/value/builders.rs 93.70% <100.00%> (ø)
src/value/element_stream_reader.rs 86.54% <100.00%> (-0.05%) ⬇️
src/value/native_writer.rs 94.54% <100.00%> (ø)
src/value/owned.rs 89.81% <100.00%> (+0.35%) ⬆️
tests/ion_hash_tests.rs 80.98% <100.00%> (ø)

... 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.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor Author

@zslayton zslayton left a 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
Copy link
Contributor Author

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();
Copy link
Contributor Author

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 called parse. 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.

Copy link
Contributor Author

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()
Copy link
Contributor Author

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())
}
Copy link
Contributor Author

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.

Copy link
Contributor

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;
Copy link
Contributor Author

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.

Copy link
Contributor

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...
Copy link
Contributor Author

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. 🎉

Copy link
Contributor

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)
Copy link
Contributor Author

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<'_>> {
Copy link
Contributor Author

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.

Copy link
Contributor

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?

@zslayton zslayton marked this pull request as ready for review March 15, 2023 21:54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

src/value/native_writer.rs Outdated Show resolved Hide resolved
}

trait ElementApi {
type ReaderApi: ElementReader;
Copy link
Contributor

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())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

tests/element_test_vectors.rs Outdated Show resolved Hide resolved
@@ -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...
Copy link
Contributor

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<'_>> {
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants