Skip to content

Commit

Permalink
Fix proto3 optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Nov 29, 2023
1 parent bc71321 commit 7a377ca
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 29 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utility Library for Rust"
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -37,10 +37,10 @@ tokio = ["bomboni_common/tokio"]
tonic = ["bomboni_proto/tonic", "bomboni_request/tonic"]

[dependencies]
bomboni_common = { path = "bomboni_common", version = "0.1.7" }
bomboni_derive = { path = "bomboni_derive", version = "0.1.7" }
bomboni_common = { path = "bomboni_common", version = "0.1.8" }
bomboni_derive = { path = "bomboni_derive", version = "0.1.8" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.7", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.7", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.7", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.7", optional = true }
bomboni_prost = { path = "bomboni_prost", version = "0.1.8", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.8", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.8", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.8", optional = true }
2 changes: 1 addition & 1 deletion bomboni_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_common"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Common things for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_derive"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Provides derive implementations for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_prost/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_prost"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with prost. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
34 changes: 25 additions & 9 deletions bomboni_prost/src/oneofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ pub fn write_message_oneofs(context: &Context, s: &mut TokenStream, message: &De

if context.config.field_names {
for (oneof_index, oneof) in message.oneof_decl.iter().enumerate() {
if message.field.iter().any(|field| {
field.oneof_index == Some(oneof_index as i32) && field.proto3_optional()
}) {
continue;
}

write_name(context, s, message, oneof);
write_variant_names(context, s, message, oneof, oneof_index);
}
}

if context.config.oneof_utility {
for (oneof_index, oneof) in message.oneof_decl.iter().enumerate() {
if message.field.iter().any(|field| {
field.oneof_index == Some(oneof_index as i32) && field.proto3_optional()
}) {
continue;
}

write_variant_from(context, s, message, oneof, oneof_index);
write_variant_utility(context, s, message, oneof, oneof_index);
}
Expand All @@ -31,6 +43,8 @@ pub fn write_message_oneofs(context: &Context, s: &mut TokenStream, message: &De
if context.config.oneof_utility {
write_into_owner(context, s, message);
}

// panic!();
}

fn write_name(
Expand Down Expand Up @@ -213,24 +227,26 @@ fn write_variant_utility(
});
}

let oneof_ident = context.get_oneof_ident(message, oneof);
s.extend(quote! {
impl #oneof_ident {
pub fn get_variant_name(&self) -> &'static str {
match self {
#variant_cases
if !variant_cases.is_empty() {
let oneof_ident = context.get_oneof_ident(message, oneof);
s.extend(quote! {
impl #oneof_ident {
pub fn get_variant_name(&self) -> &'static str {
match self {
#variant_cases
}
}
}
}
});
});
}
}

fn write_into_owner(context: &Context, s: &mut TokenStream, message: &DescriptorProto) {
if message.oneof_decl.len() != 1
|| !message
.field
.iter()
.all(|field| field.oneof_index.is_some())
.all(|field| field.oneof_index.is_some() && !field.proto3_optional())
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions bomboni_proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_proto"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with Protobuf/gRPC. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -36,5 +36,5 @@ serde_json = { version = "1.0.108", optional = true }
serde_json = "1.0.108"

[build-dependencies]
bomboni_prost = { path = "../bomboni_prost", version = "0.1.7" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.8" }
prost-build = "0.12.3"
1 change: 1 addition & 0 deletions bomboni_proto/tests/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn names() {

assert_eq!(Command::NAME, "Command");
assert_eq!(Command::KIND_ONEOF_NAME, "kind");
assert_eq!(Command::ETAG_FIELD_NAME, "etag");
assert_eq!(CommandKind::STATUS_VARIANT_NAME, "status");

assert_eq!(Status::NAME, "Command.Status");
Expand Down
4 changes: 3 additions & 1 deletion bomboni_proto/tests/proto/command/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ message Command {

string user = 1;

optional string etag = 2;

oneof kind {
Status status = 2;
Status status = 3;
string print = 4;
perms.Perms apply_perms = 5;
}
Expand Down
8 changes: 4 additions & 4 deletions bomboni_request/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with API requests. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -18,9 +18,9 @@ testing = []
tonic = ["bomboni_proto/tonic", "dep:tonic"]

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.7" }
bomboni_derive = { path = "../bomboni_derive", version = "0.1.7" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.7" }
bomboni_common = { path = "../bomboni_common", version = "0.1.8" }
bomboni_derive = { path = "../bomboni_derive", version = "0.1.8" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.8" }
thiserror = "1.0.50"
itertools = "0.12.0"
time = { version = "0.3.30", features = ["formatting", "parsing"] }
Expand Down
6 changes: 3 additions & 3 deletions bomboni_template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_template"
version = "0.1.7"
version = "0.1.8"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working Handlebars templates. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -17,8 +17,8 @@ path = "src/lib.rs"
testing = []

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.7" }
bomboni_proto = { version = "0.1.7", path = "../bomboni_proto", features = [
bomboni_common = { path = "../bomboni_common", version = "0.1.8" }
bomboni_proto = { version = "0.1.8", path = "../bomboni_proto", features = [
"json",
] }
thiserror = "1.0.50"
Expand Down

0 comments on commit 7a377ca

Please sign in to comment.