Skip to content

Adds config option for writing struct field names as text/sid #816

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

Merged
merged 2 commits into from
Aug 17, 2024

Conversation

zslayton
Copy link
Contributor

Plumbs a ValueWriterConfig instance through the application layer value writer types.

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

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 🧭

Comment on lines +342 to +344
fn config(&self) -> ValueWriterConfig {
v1_0::Binary::default_value_writer_config()
}
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 StructWriter impls need to be able to configure a value writer for each field. Only the application-level value writer types (ListWriter, StructWriter, etc) actually hold a ValueWriterConfig (at least for the time being), so the raw writer types just return the default ValueWriterConfig for their encoding.

@@ -59,7 +59,9 @@ impl<'value, 'top> BinaryValueWriter_1_1<'value, 'top> {
}

pub fn with_inline_symbol_text(mut self) -> Self {
self.value_writer_config = self.value_writer_config.with_delimited_containers();
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 was a copy/paste error that made it through in an earlier PR.

@@ -741,7 +739,7 @@ macro_rules! annotate_and_delegate_1_1 {
let value_writer = $crate::lazy::encoder::binary::v1_1::value_writer::BinaryValueWriter_1_1::new(
self.allocator,
self.buffer,
self.config(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Previously, fn config(&self) -> ValueWriterConfig was a required method on ValueWriter and MakeValueWriter, but I realized it wasn't used in many places.

This PR moves the requirement to the StructWriter trait, which only has ~6 impls.

Self {
name,
struct_writer,
value_writer_config,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Constructing a FieldWriter now takes a ValueWriterConfig. Application-level constructs use their own, lower-level constructs just use their encoding's default for now.

pub fn new() -> Self {
ValueWriterConfig::default()
/// Constructs a `ValueWriterConfig` that writes all symbol tokens as inline text.
pub const fn text() -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Constructors that can be used as a jumping-off point in a const context.

Comment on lines -47 to -48
symbol_creation_policy,
supports_text_tokens,
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 config options have been replaced by ValueWriterConfig.

Comment on lines -211 to -212
self.raw_value_writer = self
.raw_value_writer
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 used to delegate to their inner raw writer, but I realized that was just storing application-level config at a lower level. Now the application-level types store their own config.

}
}

pub fn with_field_name_encoding(mut self, field_name_encoding: FieldNameEncoding) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Notice that this lives on the struct writer, so you don't do

writer.value_writer().with_field_name_encoding(E)

you do

writer.struct_writer().with_field_name_encoding(E)

which is a bit more obvious.

self.encoding,
self.value_writer_config,
self.raw_struct_writer.make_value_writer(),
)
}
}

impl<'value, V: ValueWriter> FieldEncoder for ApplicationStructWriter<'value, V> {
fn encode_field_name(&mut self, name: impl AsRawSymbolRef) -> IonResult<()> {
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 changes in this method are what cause the field name encoding config setting to be respected.

@@ -150,6 +154,14 @@ impl Encoding for BinaryEncoding_1_0 {
fn default_write_config() -> WriteConfig<Self> {
WriteConfig::<Self>::new()
}

fn default_value_writer_config() -> ValueWriterConfig {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Each encoding can now provide its default ValueWriterConfig, which is what a Writer starts with.

@zslayton zslayton marked this pull request as ready for review August 16, 2024 21:17
Comment on lines 959 to 960
// Symbols that are already in the symbol table are written as SIDs
(RawSymbolRef::Text("name"), &[0xF9, 0x6E, 0x61, 0x6D, 0x65]), // FlexSym 4, SID $4,
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a copy-paste mistake from one of the other tests. Is it?

Copy link
Contributor Author

@zslayton zslayton Aug 17, 2024

Choose a reason for hiding this comment

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

Yeah. The code is right but the comments are wrong. I'll fix this before merging, thanks.

@zslayton zslayton merged commit 4e8739f into main Aug 17, 2024
28 of 31 checks passed
@zslayton zslayton deleted the field-name-encoding branch August 17, 2024 13:27
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