Skip to content

Commit

Permalink
Merge pull request #884 from Aiven-Open/jjaakola-aiven-fix-java-multi…
Browse files Browse the repository at this point in the history
…ple-files-protobuf-option-rendering

Fix java multiple files protobuf option rendering
  • Loading branch information
keejon authored May 24, 2024
2 parents ef3a486 + da8a0b6 commit d0ce5b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 8 additions & 2 deletions karapace/protobuf/option_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ def __init__(self, name: str, kind: Kind, value, is_parenthesized: bool = False)
self.formattedName = f"({self.name})" if is_parenthesized else self.name

def to_schema(self) -> str:
aline = None
aline = ""
if self.kind == self.Kind.STRING:
aline = f'{self.formattedName} = "{self.value}"'
elif self.kind in [self.Kind.BOOLEAN, self.Kind.NUMBER, self.Kind.ENUM]:
elif self.kind == self.Kind.BOOLEAN:
aline = f"{self.formattedName} = {str(self.value).lower()}"
elif self.kind == self.Kind.NUMBER:
aline = f"{self.formattedName} = {self.value}"
elif self.kind == self.Kind.ENUM:
aline = f"{self.formattedName} = {self.value}"
elif self.kind == self.Kind.OPTION:
aline = f"{self.formattedName}.{try_to_schema(self.value)}"
elif self.kind == self.Kind.MAP:
aline = [f"{self.formattedName} = {{\n", self.format_option_map(self.value), "}"]
elif self.kind == self.Kind.LIST:
aline = [f"{self.formattedName} = ", self.append_options(self.value)]
else:
raise ValueError("Unknown value Kind.")

if isinstance(aline, list):
return "".join(aline)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_protobuf_binary_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,36 @@
"CgdkZWZhdWx0Ig8KA0tleRIICgJpZBgBKAUiLQoDRG9nEgoKBG5hbWUYASgJEgwKBndlaWdodBgCKAUSDAoEdG95cxgEIAMoCWIGcHJvdG8z"
)

schema_serialized2 = (
"CiZwcm90by90ZWNoL2Rvam8vZHNwL3YxL2V2ZW50X2tleS5wcm90bxIQdGVjaC5kb2pvLmRzcC52MSIaCghFdmVudEtleRIOCgJpZBgBIAEoCVI"
+ "CaWRCiAEKFGNvbS50ZWNoLmRvam8uZHNwLnYxQg1FdmVudEtleVByb3RvUAGiAgNURESqAhBUZWNoLkRvam8uRHNwLlYxygIQVGVjaFxEb2pvXER"
+ "zcFxWMeICHFRlY2hcRG9qb1xEc3BcVjFcR1BCTWV0YWRhdGHqAhNUZWNoOjpEb2pvOjpEc3A6OlYxYgZwcm90bzM="
)

schema_plain2 = """\
syntax = "proto3";
package tech.dojo.dsp.v1;
option java_package = "com.tech.dojo.dsp.v1";
option java_outer_classname = "EventKeyProto";
option java_multiple_files = true;
option objc_class_prefix = "TDD";
option csharp_namespace = "Tech.Dojo.Dsp.V1";
option php_namespace = "Tech\\\\Dojo\\\\Dsp\\\\V1";
option php_metadata_namespace = "Tech\\\\Dojo\\\\Dsp\\\\V1\\\\GPBMetadata";
option ruby_package = "Tech::Dojo::Dsp::V1";
message EventKey {
string id = 1;
}
"""


@pytest.mark.parametrize(
"schema_plain,schema_serialized",
[
(schema_plain1, schema_serialized1),
(schema_plain2, schema_serialized2),
(schema_protobuf_plain, schema_protobuf_plain_bin),
(schema_protobuf_order_after, schema_protobuf_order_after_bin),
(schema_protobuf_nested_message4, schema_protobuf_nested_message4_bin),
Expand Down

0 comments on commit d0ce5b9

Please sign in to comment.