Codegen: add set to schema definitions

This commit is contained in:
Paolo Tranquilli
2023-03-29 14:03:07 +02:00
parent 6dd45b31e1
commit bba5d9dbd6
4 changed files with 23 additions and 152 deletions

View File

@@ -25,6 +25,7 @@ class Property:
OPTIONAL = auto()
REPEATED_OPTIONAL = auto()
PREDICATE = auto()
REPEATED_UNORDERED = auto()
kind: Kind
name: Optional[str] = None
@@ -44,7 +45,11 @@ class Property:
@property
def is_repeated(self) -> bool:
return self.kind in (self.Kind.REPEATED, self.Kind.REPEATED_OPTIONAL)
return self.kind in (self.Kind.REPEATED, self.Kind.REPEATED_OPTIONAL, self.Kind.REPEATED_UNORDERED)
@property
def is_unordered(self) -> bool:
return self.kind == self.Kind.REPEATED_UNORDERED
@property
def is_predicate(self) -> bool:
@@ -65,6 +70,7 @@ RepeatedProperty = functools.partial(Property, Property.Kind.REPEATED)
RepeatedOptionalProperty = functools.partial(
Property, Property.Kind.REPEATED_OPTIONAL)
PredicateProperty = functools.partial(Property, Property.Kind.PREDICATE)
RepeatedUnorderedProperty = functools.partial(Property, Property.Kind.REPEATED_UNORDERED)
@dataclass

View File

@@ -77,7 +77,7 @@ class _Optionalizer(_schema.PropertyModifier):
K = _schema.Property.Kind
if prop.kind != K.SINGLE:
raise _schema.Error(
"Optional should only be applied to simple property types")
"optional should only be applied to simple property types")
prop.kind = K.OPTIONAL
@@ -90,7 +90,14 @@ class _Listifier(_schema.PropertyModifier):
prop.kind = K.REPEATED_OPTIONAL
else:
raise _schema.Error(
"Repeated should only be applied to simple or optional property types")
"list should only be applied to simple or optional property types")
class _Setifier(_schema.PropertyModifier):
def modify(self, prop: _schema.Property):
K = _schema.Property.Kind
if prop.kind != K.SINGLE:
raise _schema.Error("set should only be applied to simple property types")
prop.kind = K.REPEATED_UNORDERED
class _TypeModifier:
@@ -122,6 +129,7 @@ string = "string"
predicate = _schema.predicate_marker
optional = _TypeModifier(_Optionalizer())
list = _TypeModifier(_Listifier())
set = _TypeModifier(_Setifier())
child = _ChildModifier()
doc = _DocModifier