Skip to content

Commit bba5d9d

Browse files
committed
Codegen: add set to schema definitions
1 parent 6dd45b3 commit bba5d9d

File tree

4 files changed

+23
-152
lines changed

4 files changed

+23
-152
lines changed

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def modify(self, prop: _schema.Property):
7777
K = _schema.Property.Kind
7878
if prop.kind != K.SINGLE:
7979
raise _schema.Error(
80-
"Optional should only be applied to simple property types")
80+
"optional should only be applied to simple property types")
8181
prop.kind = K.OPTIONAL
8282

8383

@@ -90,7 +90,14 @@ def modify(self, prop: _schema.Property):
9090
prop.kind = K.REPEATED_OPTIONAL
9191
else:
9292
raise _schema.Error(
93-
"Repeated should only be applied to simple or optional property types")
93+
"list should only be applied to simple or optional property types")
94+
95+
class _Setifier(_schema.PropertyModifier):
96+
def modify(self, prop: _schema.Property):
97+
K = _schema.Property.Kind
98+
if prop.kind != K.SINGLE:
99+
raise _schema.Error("set should only be applied to simple property types")
100+
prop.kind = K.REPEATED_UNORDERED
94101

95102

96103
class _TypeModifier:
@@ -122,6 +129,7 @@ def f(cls: type) -> type:
122129
predicate = _schema.predicate_marker
123130
optional = _TypeModifier(_Optionalizer())
124131
list = _TypeModifier(_Listifier())
132+
set = _TypeModifier(_Setifier())
125133

126134
child = _ChildModifier()
127135
doc = _DocModifier

misc/codegen/schemadefs.py

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

misc/codegen/test/test_schemaloader.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class A:
167167
three: defs.list[defs.boolean]
168168
four: defs.list[defs.optional[defs.string]]
169169
five: defs.predicate
170+
six: defs.set[defs.string]
170171

171172
assert data.classes == {
172173
'A': schema.Class('A', properties=[
@@ -175,6 +176,7 @@ class A:
175176
schema.RepeatedProperty('three', 'boolean'),
176177
schema.RepeatedOptionalProperty('four', 'string'),
177178
schema.PredicateProperty('five'),
179+
schema.RepeatedUnorderedProperty('six', 'string'),
178180
]),
179181
}
180182

@@ -193,6 +195,7 @@ class B(A):
193195
two: defs.optional[A]
194196
three: defs.list[A]
195197
four: defs.list[defs.optional[A]]
198+
five: defs.set[A]
196199

197200
assert data.classes == {
198201
'A': schema.Class('A', derived={'B'}),
@@ -201,6 +204,7 @@ class B(A):
201204
schema.OptionalProperty('two', 'A'),
202205
schema.RepeatedProperty('three', 'A'),
203206
schema.RepeatedOptionalProperty('four', 'A'),
207+
schema.RepeatedUnorderedProperty('five', 'A'),
204208
]),
205209
}
206210

@@ -213,13 +217,15 @@ class A:
213217
two: defs.optional["A"]
214218
three: defs.list["A"]
215219
four: defs.list[defs.optional["A"]]
220+
five: defs.set["A"]
216221

217222
assert data.classes == {
218223
'A': schema.Class('A', properties=[
219224
schema.SingleProperty('one', 'A'),
220225
schema.OptionalProperty('two', 'A'),
221226
schema.RepeatedProperty('three', 'A'),
222227
schema.RepeatedOptionalProperty('four', 'A'),
228+
schema.RepeatedUnorderedProperty('five', 'A'),
223229
]),
224230
}
225231

0 commit comments

Comments
 (0)