Skip to content

Commit d77cf56

Browse files
authored
[skip ci] kafka_gen.py: bug fixes (#1832)
1 parent fe0f8f5 commit d77cf56

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

script/kafka_gen.py

+29-14
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
COMPACT_NULLABLE_TYPE_MAPS = {
7777
"string": "!CompactNullableString",
7878
"bytes": "!CompactNullableBytes",
79-
"records": ..., # TODO, produce version >= 9
79+
"records": ..., # TODO, produce version >= 9
8080
"array": "!(CompactKaArray {})",
8181
}
8282

@@ -287,9 +287,11 @@ def format(self):
287287
else:
288288
data_type = f"data {self._name} = {self._cons}"
289289
data_fields = "\n , ".join(
290-
f"{f.name} :: {f.ver_ty}\n{format_field_doc(f.doc)}"
291-
if f.doc
292-
else f"{f.name} :: {f.ver_ty}"
290+
(
291+
f"{f.name} :: {f.ver_ty}\n{format_field_doc(f.doc)}"
292+
if f.doc
293+
else f"{f.name} :: {f.ver_ty}"
294+
)
293295
for f in self.fields
294296
)
295297
data_fields = " { " + data_fields + "\n }"
@@ -316,9 +318,9 @@ def append_hs_datas(datas: List[HsData], data: HsData):
316318
and data.fields == data_.fields # noqa: W503
317319
):
318320
# An int mean use this fileds instead
319-
DATA_TYPE_RENAMES[
320-
f"{data.name}V{data.version}"
321-
] = f"{data.name}V{data_.version}"
321+
DATA_TYPE_RENAMES[f"{data.name}V{data.version}"] = (
322+
f"{data.name}V{data_.version}"
323+
)
322324
data.fields = data_.version
323325
same_found = True
324326
datas.append(data)
@@ -639,30 +641,39 @@ def parse(msg, custom=False):
639641

640642

641643
def convert_field_type(f, is_nullable=False, is_flexible=False):
642-
if f.ka_type == "string" and is_nullable:
643-
return "!NullableString"
644+
if f.ka_type == "string":
645+
if is_nullable and not is_flexible:
646+
return "!NullableString"
647+
if is_nullable and is_flexible:
648+
return "!CompactNullableString"
649+
if not is_nullable and is_flexible:
650+
return "!CompactString"
644651
return f.ty
645652

646653

647-
def _convert_field_array(f, ver, label, direction, is_flexible=False):
654+
def _convert_field_array(f, ver, label, direction, convert_flexible=False):
648655
convertCompact = (
649656
"kaArrayToCompact" if direction == "To" else "kaArrayFromCompact"
650657
)
651658
if f.ka_type_arr_of in {*TYPE_MAPS.keys(), "TaggedFields"}:
652-
if is_flexible:
659+
if convert_flexible:
653660
return f"{convertCompact} {label}.{f.name}"
654661
return label + "." + f.name
655662
else:
656663
converter = lower_fst(f.ka_type_arr_of) + direction + "V" + str(ver)
657-
if is_flexible:
664+
if convert_flexible:
658665
return f"fmap {converter} ({convertCompact} {label}.{f.name})"
659666
return f"fmap {converter} {label}.{f.name}"
660667

661668

662669
def convert_field_to(f, ver, is_flexible, label):
663670
if f.ka_type == "array":
664671
return _convert_field_array(
665-
f, ver, label, "To", is_flexible=is_flexible
672+
f,
673+
ver,
674+
label,
675+
"To",
676+
convert_flexible=is_flexible and f.ty != f.ver_ty,
666677
)
667678

668679
if f.ka_type in {*TYPE_MAPS.keys(), "TaggedFields"}:
@@ -677,7 +688,11 @@ def convert_field_from(src_fields, ver, dest, is_flexible, label):
677688
if src is not None:
678689
if src.ka_type == "array":
679690
return _convert_field_array(
680-
src, ver, label, "From", is_flexible=is_flexible
691+
src,
692+
ver,
693+
label,
694+
"From",
695+
convert_flexible=is_flexible and src.ty != src.ver_ty,
681696
)
682697
return label + "." + src.name
683698

0 commit comments

Comments
 (0)