Skip to content

Commit 3e70380

Browse files
authored
Merge pull request github#12710 from github/redsun82/swift-imported-modules-as-set
Swift: make imported and exported modules a set
2 parents 8e54328 + 3d6916e commit 3e70380

38 files changed

+10636
-233
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repos:
1919
rev: v1.6.0
2020
hooks:
2121
- id: autopep8
22-
files: ^swift/.*\.py
22+
files: ^misc/codegen/.*\.py
2323

2424
- repo: local
2525
hooks:

misc/codegen/generators/cppgen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def _get_field(cls: schema.Class, p: schema.Property, add_or_none_except: typing
4949
is_optional=p.is_optional,
5050
is_repeated=p.is_repeated,
5151
is_predicate=p.is_predicate,
52+
is_unordered=p.is_unordered,
5253
trap_name=trap_name,
5354
)
5455
args.update(cpp.get_field_override(p.name))

misc/codegen/generators/dbschemegen.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ def cls_to_dbscheme(cls: schema.Class, lookup: typing.Dict[str, schema.Class], a
6464
)
6565
# use property-specific tables for 1-to-many and 1-to-at-most-1 properties
6666
for f in cls.properties:
67-
if f.is_repeated:
67+
if f.is_unordered:
68+
yield Table(
69+
name=inflection.tableize(f"{cls.name}_{f.name}"),
70+
columns=[
71+
Column("id", type=dbtype(cls.name)),
72+
Column(inflection.singularize(f.name), dbtype(f.type, add_or_none_except)),
73+
],
74+
dir=dir,
75+
)
76+
elif f.is_repeated:
6877
yield Table(
6978
keyset=KeySet(["id", "index"]),
7079
name=inflection.tableize(f"{cls.name}_{f.name}"),

misc/codegen/generators/qlgen.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, prev_child: str =
109109
prev_child=prev_child if prop.is_child else None,
110110
is_optional=prop.is_optional,
111111
is_predicate=prop.is_predicate,
112+
is_unordered=prop.is_unordered,
112113
description=prop.description
113114
)
114115
if prop.is_single:
@@ -123,7 +124,7 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, prev_child: str =
123124
singular=inflection.singularize(inflection.camelize(prop.name)),
124125
plural=inflection.pluralize(inflection.camelize(prop.name)),
125126
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
126-
tableparams=["this", "index", "result"],
127+
tableparams=["this", "index", "result"] if not prop.is_unordered else ["this", "result"],
127128
doc=_get_doc(cls, prop, plural=False),
128129
doc_plural=_get_doc(cls, prop, plural=True),
129130
)
@@ -255,7 +256,7 @@ def _get_all_properties_to_be_tested(cls: schema.Class, lookup: typing.Dict[str,
255256
# TODO here operations are duplicated, but should be better if we split ql and qltest generation
256257
p = get_ql_property(c, p)
257258
yield ql.PropertyForTest(p.getter, is_total=p.is_single or p.is_predicate,
258-
type=p.type if not p.is_predicate else None, is_repeated=p.is_repeated)
259+
type=p.type if not p.is_predicate else None, is_indexed=p.is_indexed)
259260
if p.is_repeated and not p.is_optional:
260261
yield ql.PropertyForTest(f"getNumberOf{p.plural}", type="int")
261262
elif p.is_optional and not p.is_repeated:

misc/codegen/lib/cpp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Field:
3636
base_type: str
3737
is_optional: bool = False
3838
is_repeated: bool = False
39+
is_unordered: bool = False
3940
is_predicate: bool = False
4041
trap_name: str = None
4142
first: bool = False

misc/codegen/lib/ql.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Property:
3636
first: bool = False
3737
is_optional: bool = False
3838
is_predicate: bool = False
39+
is_unordered: bool = False
3940
prev_child: Optional[str] = None
4041
qltest_skip: bool = False
4142
description: List[str] = field(default_factory=list)
@@ -49,7 +50,11 @@ def __post_init__(self):
4950

5051
@property
5152
def getter(self):
52-
return f"get{self.singular}" if not self.is_predicate else self.singular
53+
if self.is_predicate:
54+
return self.singular
55+
if self.is_unordered:
56+
return self.indefinite_getter
57+
return f"get{self.singular}"
5358

5459
@property
5560
def indefinite_getter(self):
@@ -77,6 +82,10 @@ def is_child(self):
7782
def has_description(self) -> bool:
7883
return bool(self.description)
7984

85+
@property
86+
def is_indexed(self) -> bool:
87+
return self.is_repeated and not self.is_unordered
88+
8089

8190
@dataclass
8291
class Base:
@@ -188,7 +197,7 @@ class PropertyForTest:
188197
getter: str
189198
is_total: bool = True
190199
type: Optional[str] = None
191-
is_repeated: bool = False
200+
is_indexed: bool = False
192201

193202

194203
@dataclass

misc/codegen/lib/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Kind(Enum):
2525
OPTIONAL = auto()
2626
REPEATED_OPTIONAL = auto()
2727
PREDICATE = auto()
28+
REPEATED_UNORDERED = auto()
2829

2930
kind: Kind
3031
name: Optional[str] = None
@@ -44,7 +45,11 @@ def is_optional(self) -> bool:
4445

4546
@property
4647
def is_repeated(self) -> bool:
47-
return self.kind in (self.Kind.REPEATED, self.Kind.REPEATED_OPTIONAL)
48+
return self.kind in (self.Kind.REPEATED, self.Kind.REPEATED_OPTIONAL, self.Kind.REPEATED_UNORDERED)
49+
50+
@property
51+
def is_unordered(self) -> bool:
52+
return self.kind == self.Kind.REPEATED_UNORDERED
4853

4954
@property
5055
def is_predicate(self) -> bool:
@@ -65,6 +70,7 @@ def has_builtin_type(self) -> bool:
6570
RepeatedOptionalProperty = functools.partial(
6671
Property, Property.Kind.REPEATED_OPTIONAL)
6772
PredicateProperty = functools.partial(Property, Property.Kind.PREDICATE)
73+
RepeatedUnorderedProperty = functools.partial(Property, Property.Kind.REPEATED_UNORDERED)
6874

6975

7076
@dataclass

misc/codegen/lib/schemadefs.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class _ChildModifier(_schema.PropertyModifier):
88
def modify(self, prop: _schema.Property):
99
if prop.type is None or prop.type[0].islower():
1010
raise _schema.Error("Non-class properties cannot be children")
11+
if prop.is_unordered:
12+
raise _schema.Error("Set properties cannot be children")
1113
prop.is_child = True
1214

1315

@@ -77,7 +79,7 @@ def modify(self, prop: _schema.Property):
7779
K = _schema.Property.Kind
7880
if prop.kind != K.SINGLE:
7981
raise _schema.Error(
80-
"Optional should only be applied to simple property types")
82+
"optional should only be applied to simple property types")
8183
prop.kind = K.OPTIONAL
8284

8385

@@ -90,7 +92,15 @@ def modify(self, prop: _schema.Property):
9092
prop.kind = K.REPEATED_OPTIONAL
9193
else:
9294
raise _schema.Error(
93-
"Repeated should only be applied to simple or optional property types")
95+
"list should only be applied to simple or optional property types")
96+
97+
98+
class _Setifier(_schema.PropertyModifier):
99+
def modify(self, prop: _schema.Property):
100+
K = _schema.Property.Kind
101+
if prop.kind != K.SINGLE:
102+
raise _schema.Error("set should only be applied to simple property types")
103+
prop.kind = K.REPEATED_UNORDERED
94104

95105

96106
class _TypeModifier:
@@ -122,6 +132,7 @@ def f(cls: type) -> type:
122132
predicate = _schema.predicate_marker
123133
optional = _TypeModifier(_Optionalizer())
124134
list = _TypeModifier(_Listifier())
135+
set = _TypeModifier(_Setifier())
125136

126137
child = _ChildModifier()
127138
doc = _DocModifier

misc/codegen/schemadefs.py

Lines changed: 0 additions & 149 deletions
This file was deleted.

misc/codegen/templates/cpp_classes_cpp.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void {{name}}::emit({{^final}}TrapLabel<{{name}}Tag> id, {{/final}}std::ostream&
2424
{{#is_repeated}}
2525
for (auto i = 0u; i < {{field_name}}.size(); ++i) {
2626
{{^is_optional}}
27-
out << {{trap_name}}Trap{id, i, {{field_name}}[i]} << '\n';
27+
out << {{trap_name}}Trap{id, {{^is_unordered}}i, {{/is_unordered}}{{field_name}}[i]} << '\n';
2828
{{/is_optional}}
2929
{{#is_optional}}
3030
if ({{field_name}}[i]) out << {{trap_name}}Trap{id, i, *{{field_name}}[i]} << '\n';

0 commit comments

Comments
 (0)