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

WIP #906

Closed
Closed

WIP #906

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
1 change: 1 addition & 0 deletions karapace/protobuf/compare_type_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def compare_type_lists(
other_types_list: Sequence[TypeElement],
result: CompareResult,
compare_types: CompareTypes,
compare_full_path: bool = False,
) -> CompareResult:
self_types = {}
other_types = {}
Expand Down
11 changes: 11 additions & 0 deletions karapace/protobuf/enum_constant_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dataclasses import dataclass, field
from karapace.protobuf.location import Location
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_options


Expand All @@ -21,6 +22,16 @@ class EnumConstantElement:
documentation: str = ""
options: list[OptionElement] = field(default_factory=list)

def with_full_path_expanded(self, type_tree: TypeTree) -> EnumConstantElement:
full_path_options = [option.with_full_path_expanded(type_tree) for option in self.options]
return EnumConstantElement(
location=self.location,
name=self.name,
tag=self.tag,
documentation=self.documentation,
options=full_path_options,
)

def to_schema(self) -> str:
result: list[str] = []
append_documentation(result, self.documentation)
Expand Down
14 changes: 13 additions & 1 deletion karapace/protobuf/enum_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from karapace.protobuf.location import Location
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.type_element import TypeElement
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_indented
from typing import Sequence

Expand All @@ -30,6 +31,17 @@ def __init__(
super().__init__(location, name, documentation, options or [], [])
self.constants = constants or []

def with_full_path_expanded(self, type_tree: TypeTree) -> EnumElement:
full_path_options = [option.with_full_path_expanded(type_tree) for option in self.options]
full_path_constants = [constant.with_full_path_expanded(type_tree) for constant in self.constants]
return EnumElement(
location=self.location,
name=self.name,
documentation=self.documentation,
options=full_path_options,
constants=full_path_constants,
)

def to_schema(self) -> str:
result: list[str] = []
append_documentation(result, self.documentation)
Expand All @@ -49,7 +61,7 @@ def to_schema(self) -> str:
result.append("}\n")
return "".join(result)

def compare(self, other: TypeElement, result: CompareResult, types: CompareTypes) -> None:
def compare(self, other: TypeElement, result: CompareResult, types: CompareTypes, compare_full_path: bool = False) -> None:
self_tags = {}
other_tags = {}
if types:
Expand Down
11 changes: 11 additions & 0 deletions karapace/protobuf/extend_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dataclasses import dataclass
from karapace.protobuf.field_element import FieldElement
from karapace.protobuf.location import Location
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_indented
from typing import Sequence

Expand All @@ -20,6 +21,16 @@ class ExtendElement:
documentation: str = ""
fields: Sequence[FieldElement] | None = None

def with_full_path_expanded(self, type_tree: TypeTree) -> ExtendElement:
# read again carefully there -> https://protobuf.com/docs/language-spec#fully-qualified-references
full_path_fields = [field.with_full_path_expanded(type_tree) for field in self.fields] if self.fields else None
return ExtendElement(
location=self.location,
name=self.name,
documentation=self.documentation,
fields=full_path_fields,
)

def to_schema(self) -> str:
result: list[str] = []
append_documentation(result, self.documentation)
Expand Down
1 change: 1 addition & 0 deletions karapace/protobuf/extensions_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ExtensionsElement:
location: Location
documentation: str = ""
values: list[int | KotlinRange] | None = None
fully_qualified_name: str | None = None

def to_schema(self) -> str:
result: list[str] = []
Expand Down
26 changes: 26 additions & 0 deletions karapace/protobuf/field_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.proto_type import ProtoType
from karapace.protobuf.type_element import TypeElement
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_options


Expand All @@ -31,6 +32,7 @@ def __init__(
tag: int | None = None,
documentation: str = "",
options: list | None = None,
fully_qualified_name: str | None = None,
) -> None:
self.location = location
self.label = label
Expand All @@ -41,6 +43,30 @@ def __init__(
self.tag = tag
self.documentation = documentation
self.options = options or []
self.fully_qualified_name = fully_qualified_name or ""

def with_full_path_expanded(self, type_tree: TypeTree) -> FieldElement:
full_path_element_type = self.element_type
element_type_tokens = self.element_type.split(".")
maybe_path_in_tree = type_tree.type_in_tree(element_type_tokens)
if maybe_path_in_tree:
missing_tokens = maybe_path_in_tree.expand_missing_absolute_path()
else:
missing_tokens = []

full_path_options = self.options
return FieldElement(
location=self.location,
label=self.label,
element_type=full_path_element_type,
name=self.name,
default_value=self.default_value,
json_name=self.json_name,
tag=self.tag,
documentation=self.documentation,
options=full_path_options,
fully_qualified_name=".".join(missing_tokens + element_type_tokens)
)

def to_schema(self) -> str:
result: list[str] = []
Expand Down
13 changes: 13 additions & 0 deletions karapace/protobuf/group_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from karapace.protobuf.field import Field
from karapace.protobuf.field_element import FieldElement
from karapace.protobuf.location import Location
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_indented
from typing import Sequence

Expand All @@ -23,6 +24,18 @@ class GroupElement:
documentation: str = ""
fields: Sequence[FieldElement] | None = None

def with_full_path_expanded(self, type_tree: TypeTree) -> GroupElement:
full_path_fields = [field.with_full_path_expanded(type_tree) for field in self.fields] if self.fields else None
# here also
return GroupElement(
label=self.label,
location=self.location,
name=self.name,
tag=self.tag,
documentation=self.documentation,
fields=full_path_fields,
)

def to_schema(self) -> str:
result: list[str] = []
append_documentation(result, self.documentation)
Expand Down
31 changes: 30 additions & 1 deletion karapace/protobuf/message_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.reserved_element import ReservedElement
from karapace.protobuf.type_element import TypeElement
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_indented
from typing import Sequence

Expand All @@ -27,6 +28,34 @@ class MessageElement(TypeElement):
fields: Sequence[FieldElement]
one_ofs: Sequence[OneOfElement]
groups: Sequence[GroupElement]
fully_qualified_name: str | None = None

def with_full_path_expanded(self, type_tree: TypeTree) -> MessageElement:
# here also

element_type_tokens = self.name.split(".")
maybe_path_in_tree = type_tree.type_in_tree(element_type_tokens)
if maybe_path_in_tree:
missing_tokens = maybe_path_in_tree.expand_missing_absolute_path()
else:
missing_tokens = []
full_path_nested_types = [nested_type.with_full_path_expanded(type_tree) for nested_type in self.nested_types]
full_path_options = [option.with_full_path_expanded(type_tree) for option in self.options]
full_path_fields = [field.with_full_path_expanded(type_tree) for field in self.fields]
full_path_one_ofs = [one_off.with_full_path_expanded(type_tree) for one_off in self.one_ofs]
full_path_groups = [group.with_full_path_expanded(type_tree) for group in self.groups]
return MessageElement(
location=self.location,
name=self.name,
documentation=self.documentation,
nested_types=full_path_nested_types,
options=full_path_options,
reserveds=self.reserveds,
fields=full_path_fields,
one_ofs=full_path_one_ofs,
extensions=self.extensions,
groups=full_path_groups,
)

def __init__(
self,
Expand Down Expand Up @@ -90,7 +119,7 @@ def to_schema(self) -> str:
result.append("}\n")
return "".join(result)

def compare(self, other: TypeElement, result: CompareResult, types: CompareTypes) -> None:
def compare(self, other: TypeElement, result: CompareResult, types: CompareTypes, compare_full_path: bool = False) -> None:
from karapace.protobuf.compare_type_lists import compare_type_lists

if not isinstance(other, MessageElement):
Expand Down
14 changes: 14 additions & 0 deletions karapace/protobuf/one_of_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from karapace.protobuf.field_element import FieldElement
from karapace.protobuf.group_element import GroupElement
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_documentation, append_indented
from typing import Sequence

Expand All @@ -32,6 +33,19 @@ def __init__(
self.options = options or []
self.groups = groups or []

def with_full_path_expanded(self, type_tree: TypeTree) -> OneOfElement:
full_path_fields = [field.with_full_path_expanded(type_tree) for field in self.fields]
full_path_groups = [group.with_full_path_expanded(type_tree) for group in self.groups]
full_path_options = [option.with_full_path_expanded(type_tree) for option in self.options]

return OneOfElement(
name=self.name,
documentation=self.documentation,
fields=full_path_fields,
groups=full_path_groups,
options=full_path_options,
)

def to_schema(self) -> str:
result: list[str] = []
append_documentation(result, self.documentation)
Expand Down
39 changes: 30 additions & 9 deletions karapace/protobuf/option_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
from __future__ import annotations

from enum import Enum
from karapace.protobuf.type_tree import TypeTree
from karapace.protobuf.utils import append_indented, append_options, try_to_schema


class OptionElement:
name: str
fully_qualified_name: str | None = None

class Kind(Enum):
STRING = 1
Expand All @@ -30,6 +32,20 @@ def __init__(self, name: str, kind: Kind, value, is_parenthesized: bool = False)
self.is_parenthesized = is_parenthesized
self.formattedName = f"({self.name})" if is_parenthesized else self.name

def with_full_path_expanded(self, type_tree: TypeTree) -> OptionElement:
# fully qualified name also here.

#full_path_element_type = self.element_type
#element_type_tokens = self.element_type.split(".")
#missing_tokens = type_tree.type_in_tree(element_type_tokens).expand_missing_absolute_path()

return OptionElement(
name=self.name, # understand if the name should be changed, otherwise remove the method.
kind=self.kind,
value=self.value,
is_parenthesized=self.is_parenthesized,
)

def to_schema(self) -> str:
aline = ""
if self.kind == self.Kind.STRING:
Expand All @@ -43,9 +59,9 @@ def to_schema(self) -> str:
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), "}"]
aline = [f"{self.formattedName} = {{\n", OptionElement.format_option_map(self.value), "}"]
elif self.kind == self.Kind.LIST:
aline = [f"{self.formattedName} = ", self.append_options(self.value)]
aline = [f"{self.formattedName} = ", OptionElement.append_options(self.value)]
else:
raise ValueError("Unknown value Kind.")

Expand All @@ -62,36 +78,41 @@ def append_options(options: list) -> str:
append_options(data, options)
return "".join(data)

def format_option_map(self, value: dict) -> str:

@staticmethod
def format_option_map(value: dict) -> str:
keys = list(value.keys())
last_index = len(keys) - 1
result: list[str] = []
for index, key in enumerate(keys):
endl = "," if (index != last_index) else ""
append_indented(result, f"{key}: {self.format_option_map_value(value[key])}{endl}")
append_indented(result, f"{key}: {OptionElement.format_option_map_value(value[key])}{endl}")
return "".join(result)

def format_option_map_value(self, value) -> str:

@staticmethod
def format_option_map_value(value) -> str:
aline = value
if isinstance(value, str):
aline = f'"{value}"'
elif isinstance(value, dict):
aline = ["{\n", self.format_option_map(value), "}"]
aline = ["{\n", OptionElement.format_option_map(value), "}"]
elif isinstance(value, list):
aline = ["[\n", self.format_list_map_value(value), "]"]
aline = ["[\n", OptionElement.format_list_map_value(value), "]"]

if isinstance(aline, list):
return "".join(aline)
if isinstance(aline, str):
return aline
return value

def format_list_map_value(self, value) -> str:
@staticmethod
def format_list_map_value(value) -> str:
last_index = len(value) - 1
result: list[str] = []
for index, elm in enumerate(value):
endl = "," if (index != last_index) else ""
append_indented(result, f"{self.format_option_map_value(elm)}{endl}")
append_indented(result, f"{OptionElement.format_option_map_value(elm)}{endl}")
return "".join(result)

def __repr__(self) -> str:
Expand Down
Loading
Loading