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

feat: add feature emit_fields #66

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ jobs:
- run:
name: Cargo test (btree)
command: cargo test --workspace --features btree
- run:
name: Cargo test (emit fields)
command: cargo test --workspace --features emit-fields
- cache_save

workflows:
Expand Down
1 change: 0 additions & 1 deletion pbjson-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ categories = ["encoding"]
repository = "https://github.com/influxdata/pbjson"

[dependencies]

heck = "0.4"
prost = "0.11"
prost-types = "0.11"
Expand Down
24 changes: 18 additions & 6 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ pub fn generate_message<W: Write>(
writer: &mut W,
ignore_unknown_fields: bool,
btree_map_paths: &[String],
emit_fields: bool,
) -> Result<()> {
let rust_type = resolver.rust_type(&message.path);

// Generate Serialize
write_serialize_start(0, &rust_type, writer)?;
write_message_serialize(resolver, 2, message, writer)?;
write_message_serialize(resolver, 2, message, writer, emit_fields)?;
write_serialize_end(0, writer)?;

// Generate Deserialize
Expand All @@ -62,7 +63,15 @@ pub fn generate_message<W: Write>(
Ok(())
}

fn write_field_empty_predicate<W: Write>(member: &Field, writer: &mut W) -> Result<()> {
fn write_field_empty_predicate<W: Write>(
member: &Field,
writer: &mut W,
emit_fields: bool,
) -> Result<()> {
if emit_fields {
return write!(writer, "true");
}

match (&member.field_type, &member.field_modifier) {
(_, FieldModifier::Required) => unreachable!(),
(_, FieldModifier::Repeated)
Expand Down Expand Up @@ -96,11 +105,12 @@ fn write_message_serialize<W: Write>(
indent: usize,
message: &Message,
writer: &mut W,
emit_fields: bool,
) -> Result<()> {
write_struct_serialize_start(indent, message, writer)?;
write_struct_serialize_start(indent, message, writer, emit_fields)?;

for field in &message.fields {
write_serialize_field(resolver, indent, field, writer)?;
write_serialize_field(resolver, indent, field, writer, emit_fields)?;
}

for one_of in &message.one_ofs {
Expand All @@ -114,6 +124,7 @@ fn write_struct_serialize_start<W: Write>(
indent: usize,
message: &Message,
writer: &mut W,
emit_fields: bool,
) -> Result<()> {
writeln!(writer, "{}use serde::ser::SerializeStruct;", Indent(indent))?;

Expand All @@ -134,7 +145,7 @@ fn write_struct_serialize_start<W: Write>(
continue;
}
write!(writer, "{}if ", Indent(indent))?;
write_field_empty_predicate(field, writer)?;
write_field_empty_predicate(field, writer, emit_fields)?;
writeln!(writer, " {{")?;
writeln!(writer, "{}len += 1;", Indent(indent + 1))?;
writeln!(writer, "{}}}", Indent(indent))?;
Expand Down Expand Up @@ -357,6 +368,7 @@ fn write_serialize_field<W: Write>(
indent: usize,
field: &Field,
writer: &mut W,
emit_fields: bool,
) -> Result<()> {
let as_ref = format!("&self.{}", field.rust_field_name());
let variable = Variable {
Expand Down Expand Up @@ -386,7 +398,7 @@ fn write_serialize_field<W: Write>(
}
FieldModifier::Repeated | FieldModifier::UseDefault => {
write!(writer, "{}if ", Indent(indent))?;
write_field_empty_predicate(field, writer)?;
write_field_empty_predicate(field, writer, emit_fields)?;
writeln!(writer, " {{")?;
write_serialize_variable(resolver, indent + 1, field, variable, writer)?;
writeln!(writer, "{}}}", Indent(indent))?;
Expand Down
8 changes: 8 additions & 0 deletions pbjson-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub struct Builder {
retain_enum_prefix: bool,
ignore_unknown_fields: bool,
btree_map_paths: Vec<String>,
emit_fields: bool,
}

impl Builder {
Expand Down Expand Up @@ -178,6 +179,12 @@ impl Builder {
self
}

/// Output fields with their default values.
pub fn emit_fields(&mut self) -> &mut Self {
self.emit_fields = true;
self
}

/// Generates code for all registered types where `prefixes` contains a prefix of
/// the fully-qualified path of the type
pub fn build<S: AsRef<str>>(&mut self, prefixes: &[S]) -> Result<()> {
Expand Down Expand Up @@ -261,6 +268,7 @@ impl Builder {
writer,
self.ignore_unknown_fields,
&self.btree_map_paths,
self.emit_fields,
)?
}
}
Expand Down
1 change: 1 addition & 0 deletions pbjson-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde = { version = "1.0", features = ["derive"] }
[features]
ignore-unknown-fields = []
btree = []
emit-fields = []

[dev-dependencies]
chrono = "0.4"
Expand Down
4 changes: 4 additions & 0 deletions pbjson-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ fn main() -> Result<()> {
builder.btree_map([".test"]);
}

if cfg!(feature = "emit-fields") {
builder.emit_fields();
}

builder.build(&[".test"])?;

Ok(())
Expand Down
8 changes: 8 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ mod tests {
}

#[test]
#[cfg(feature = "emit-fields")]
fn test_emit_fields() {
let mut decoded: KitchenSink = serde_json::from_str("{}").unwrap();
assert_ne!(serde_json::to_string(&decoded).unwrap().as_str(), "{}");
}

#[test]
#[cfg(not(feature = "emit-fields"))]
fn test_kitchen_sink() {
let mut decoded: KitchenSink = serde_json::from_str("{}").unwrap();

Expand Down