Skip to content

Makes to and from into namespaces with a nested json command#54

Merged
zslayton merged 5 commits intomasterfrom
to-from-modules
Jun 13, 2023
Merged

Makes to and from into namespaces with a nested json command#54
zslayton merged 5 commits intomasterfrom
to-from-modules

Conversation

@zslayton
Copy link
Copy Markdown
Contributor

@zslayton zslayton commented Jun 12, 2023

Prior to this PR, to and from were commands that each took a format
flag that could be used to specify json. This patch promotes to and
from into namespaces that each have a nested json command. This makes
it easier to add support for other subcommands in to and from, and
allows those subcommands to expose settings that only make sense for the
corresponding format. (For example: configuring a catalog for Ion data,
or a list of supported tags for CBOR.)

Also fixes #36 by manually outputting a newline after writing any file
in an Ion text format.


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

zslayton added 4 commits June 9, 2023 17:18
Prior to this PR, 'to' and 'from' were commands that each took a 'format'
flag that could be used to specify 'json'. This patch promotes 'to' and
'from' into namespaces that each have a nested 'json' command. This makes
it easier to add support for other subcommands in 'to' and 'from', and
allows those subcommands to expose settings that only make sense for the
corresponding format. (For example: configuring a catalog for Ion data,
or a list of supported tags for CBOR.)

Also fixes #36 by manually outputting a newline after writing any file
in an Ion text format.
@zslayton zslayton changed the title To from modules Makes to and from into namespaces with a nested json command Jun 12, 2023
@zslayton zslayton requested review from desaikd and jobarr-amzn June 12, 2023 14:35
Copy link
Copy Markdown
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ There are still a LOT of opportunities to DRY up this codebase with some traits to capture the common dispatch logic and base application configuration flags, but they're out of scope for this PR.

Comment on lines -32 to -33
.long("input")
.short('i')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ At some point certain subcommands began accepting BOTH -i input files and trailing arguments as input files. This is reasonable, but it isn't supported as a first-class feature in clap. clap has a debug_assert that will cause the program to panic in non---release configurations if an option is both named (-i, --input) and positional.

For now, I'm removing the named flag. We can add it back later, but it will take a bit more code to achieve in the prescribed manner.

)
})?;
Box::new(file)
Box::new(BufWriter::new(file))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Previously the to json command was not using buffered output except when writing to STDOUT.

};

// Treat the mmap as a byte array.
let ion_data: &[u8] = &mmap[..];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ ion-rs now supports iterating over the elements in a buffered reader, so we no longer have to mmap the input file. This also means that we can read directly from STDIN without first dumping it to a temporary file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Again, lots of opportunity to DRY this up remains.

) -> IonResult<usize> {
match format {
// XXX: The text formats below each have additional logic to append a newline because the
// ion-rs writer doesn't handle this automatically like it should.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Addresses #36. The real fix still needs to happen upstream in ion-rs. See amazon-ion/ion-rust#437.

Copy link
Copy Markdown
Contributor

@desaikd desaikd left a comment

Choose a reason for hiding this comment

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

Looks good! It would be nice to have a parent sub command (maybe convert) for from and to. I think that will give us nice encapsulation of the converter commands as well as we could specify some common behavior there but as you mentioned this is out of scope for the PR.

Comment thread src/bin/ion/commands/beta/mod.rs Outdated
Base automatically changed from upgrade-ion-rs-v0.18.1 to master June 12, 2023 20:36
};
result
// Because JSON data is valid Ion, the `dump` command may be reused for converting JSON.
// TODO ideally, this would perform some smarter "up-conversion".
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Even more ideally, this smarter up-conversion would be configurable.

Comment on lines +12 to +13
// Maps the given command name to the entry point for that command if it exists
pub fn runner_for_from_command(command_name: &str) -> Option<CommandRunner> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some day... #1

The factoring here allows for discovery of an external command or subcommand in runner_for_FOO_command- I like it. It would be even better if the interface included the known command context so far, e.g. here it would be ["ion", "from"] so that e.g. external command discovery could look for ion-from-cbor and know that ion from cbor should invoke it.

Thoughts? This comment might be better in #1 than here... added as a comment there.

Comment on lines +49 to +51
if let Some(input_file_names) = matches.get_many::<String>("input") {
// Input files were specified, run the converter on each of them in turn
for input_file in input_file_names {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We see this pattern repeated over and over again in ion-cli- in the long term it would be great if we had a common solution to it. Nothing to do here at the moment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Created: #55

@zslayton zslayton merged commit 3cc0747 into master Jun 13, 2023
@zslayton zslayton deleted the to-from-modules branch June 13, 2023 11:37
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.

No newline after each stream written by ion dump -f pretty

3 participants