Skip to content

Commit

Permalink
The scope of this pr is to replace the old parsing strategy and move …
Browse files Browse the repository at this point in the history
…towards a tree based type verification strategy.

From this commit it's possible to refer to a type using a partial path and not only a fully specified path. If we declare for e.g. the following schema:
```protobuf
syntax = "proto3";

package my.awesome.customer.plan.entity.v1beta1;

message CustomerPlan {
  string plan_name = 1;
}
```

When importing it it's sufficient to use a part of the full specified path (that in this case would be `my.awesome.customer.plan.entity.v1beta1.CustomerPlan`). As e.g. it would be enough to use `entity.v1beta1.CustomerPlan` or `v1beta1.CustomerPlan`, so the following declaration would be accepted:
```protobuf
syntax = "proto3";

package my.awesome.customer.plan.entity.v1beta1;
import "my/awesome/customer/plan/entity/v1beta1/entity.proto";

message CustomerPlanEvent {
  message Created {
    entity.v1beta1.CustomerPlan plan = 1;
  }
}
```
  • Loading branch information
eliax1996 committed Oct 6, 2023
1 parent 8a4ff32 commit be81a6a
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 210 deletions.
83 changes: 0 additions & 83 deletions karapace/protobuf/dependency.py

This file was deleted.

39 changes: 17 additions & 22 deletions karapace/protobuf/known_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,21 @@ def static_init(cls) -> None:
cls.index_simple[item] = key


@static_init
class DependenciesHardcoded:
index: Set = set()

@classmethod
def static_init(cls) -> None:
cls.index = {
"bool",
"bytes",
"double",
"float",
"fixed32",
"fixed64",
"int32",
"int64",
"sfixed32",
"sfixed64",
"sint32",
"sint64",
"string",
"uint32",
"uint64",
}
index: Set[str] = {
"bool",
"bytes",
"double",
"float",
"fixed32",
"fixed64",
"int32",
"int64",
"sfixed32",
"sfixed64",
"sint32",
"sint64",
"string",
"uint32",
"uint64",
}
3 changes: 3 additions & 0 deletions karapace/protobuf/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# wire-library/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Location.kt


# todo: this is fishy, how it behaves when you have an import just for path?
# Like `google/type/postal_address.proto`, which is the associated Location
# in this case?
class Location:
"""Locates a .proto file, or a self.position within a .proto file, on the file system"""

Expand Down
20 changes: 14 additions & 6 deletions karapace/protobuf/proto_file_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from karapace.dependency import Dependency
from karapace.protobuf.compare_result import CompareResult, Modification
from karapace.protobuf.compare_type_storage import CompareTypes
from karapace.protobuf.extend_element import ExtendElement
from karapace.protobuf.location import Location
from karapace.protobuf.option_element import OptionElement
from karapace.protobuf.service_element import ServiceElement
from karapace.protobuf.syntax import Syntax
from karapace.protobuf.type_element import TypeElement
from typing import Dict, List, Optional
Expand All @@ -29,18 +32,23 @@ def _collect_dependencies_types(compare_types: CompareTypes, dependencies: Optio
_collect_dependencies_types(compare_types, sub_deps, is_self)


# todo: transform those types into serious object with parsing that filters out invalid type name or invalid package name
TypeName = str
PackageName = str


class ProtoFileElement:
def __init__(
self,
location: Location,
package_name: Optional[str] = None,
package_name: Optional[PackageName] = None,
syntax: Optional[Syntax] = None,
imports: Optional[list] = None,
public_imports: Optional[list] = None,
imports: Optional[List[TypeName]] = None,
public_imports: Optional[List[TypeName]] = None,
types: Optional[List[TypeElement]] = None,
services: Optional[list] = None,
extend_declarations: Optional[list] = None,
options: Optional[list] = None,
services: Optional[List[ServiceElement]] = None,
extend_declarations: Optional[List[ExtendElement]] = None,
options: Optional[List[OptionElement]] = None,
) -> None:
if types is None:
types = list()
Expand Down
Loading

0 comments on commit be81a6a

Please sign in to comment.