Swift: ignore IPA classes in dbscheme

This commit is contained in:
Paolo Tranquilli 2022-11-16 11:20:02 +01:00
Родитель 88a0c4053b
Коммит a3d33e27e2
3 изменённых файлов: 81 добавлений и 23 удалений

Просмотреть файл

@ -38,10 +38,12 @@ def dbtype(typename: str, add_or_none_except: typing.Optional[str] = None) -> st
return typename
def cls_to_dbscheme(cls: schema.Class, add_or_none_except: typing.Optional[str] = None):
def cls_to_dbscheme(cls: schema.Class, lookup: typing.Dict[str, schema.Class], add_or_none_except: typing.Optional[str] = None):
""" Yield all dbscheme entities needed to model class `cls` """
if cls.ipa:
return
if cls.derived:
yield Union(dbtype(cls.name), (dbtype(c) for c in cls.derived))
yield Union(dbtype(cls.name), (dbtype(c) for c in cls.derived if not lookup[c].ipa))
dir = pathlib.Path(cls.group) if cls.group else None
# output a table specific to a class only if it is a leaf class or it has 1-to-1 properties
# Leaf classes need a table to bind the `@` ids
@ -96,7 +98,7 @@ def cls_to_dbscheme(cls: schema.Class, add_or_none_except: typing.Optional[str]
def get_declarations(data: schema.Schema):
add_or_none_except = data.root_class.name if data.null else None
declarations = [d for cls in data.classes.values() for d in cls_to_dbscheme(cls, add_or_none_except)]
declarations = [d for cls in data.classes.values() for d in cls_to_dbscheme(cls, data.classes, add_or_none_except)]
if data.null:
property_classes = {
prop.type for cls in data.classes.values() for prop in cls.properties

Просмотреть файл

@ -255,8 +255,9 @@ def test_final_class_with_more_fields(generate, dir_param):
def test_empty_class_with_derived(generate):
assert generate([
schema.Class(
name="Base", derived={"Left", "Right"}),
schema.Class(name="Base", derived={"Left", "Right"}),
schema.Class(name="Left", bases=["Base"]),
schema.Class(name="Right", bases=["Base"]),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
@ -265,6 +266,14 @@ def test_empty_class_with_derived(generate):
lhs="@base",
rhs=["@left", "@right"],
),
dbscheme.Table(
name="lefts",
columns=[dbscheme.Column("id", "@left", binding=True)],
),
dbscheme.Table(
name="rights",
columns=[dbscheme.Column("id", "@right", binding=True)],
),
],
)
@ -278,6 +287,8 @@ def test_class_with_derived_and_single_property(generate, dir_param):
properties=[
schema.SingleProperty("single", "Prop"),
]),
schema.Class(name="Left", bases=["Base"]),
schema.Class(name="Right", bases=["Base"]),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
@ -294,7 +305,15 @@ def test_class_with_derived_and_single_property(generate, dir_param):
dbscheme.Column('single', '@prop'),
],
dir=dir_param.expected,
)
),
dbscheme.Table(
name="lefts",
columns=[dbscheme.Column("id", "@left", binding=True)],
),
dbscheme.Table(
name="rights",
columns=[dbscheme.Column("id", "@right", binding=True)],
),
],
)
@ -308,6 +327,8 @@ def test_class_with_derived_and_optional_property(generate, dir_param):
properties=[
schema.OptionalProperty("opt", "Prop"),
]),
schema.Class(name="Left", bases=["Base"]),
schema.Class(name="Right", bases=["Base"]),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
@ -324,7 +345,15 @@ def test_class_with_derived_and_optional_property(generate, dir_param):
dbscheme.Column('opt', '@prop'),
],
dir=dir_param.expected,
)
),
dbscheme.Table(
name="lefts",
columns=[dbscheme.Column("id", "@left", binding=True)],
),
dbscheme.Table(
name="rights",
columns=[dbscheme.Column("id", "@right", binding=True)],
),
],
)
@ -338,6 +367,8 @@ def test_class_with_derived_and_repeated_property(generate, dir_param):
properties=[
schema.RepeatedProperty("rep", "Prop"),
]),
schema.Class(name="Left", bases=["Base"]),
schema.Class(name="Right", bases=["Base"]),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
@ -355,7 +386,15 @@ def test_class_with_derived_and_repeated_property(generate, dir_param):
dbscheme.Column('rep', '@prop'),
],
dir=dir_param.expected,
)
),
dbscheme.Table(
name="lefts",
columns=[dbscheme.Column("id", "@left", binding=True)],
),
dbscheme.Table(
name="rights",
columns=[dbscheme.Column("id", "@right", binding=True)],
),
],
)
@ -469,5 +508,37 @@ def test_null_class(generate):
)
def test_ipa_classes_ignored(generate):
assert generate([
schema.Class(name="A", ipa=schema.IpaInfo()),
schema.Class(name="B", ipa=schema.IpaInfo(from_class="A")),
schema.Class(name="C", ipa=schema.IpaInfo(on_arguments={"x": "A"})),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
declarations=[],
)
def test_ipa_derived_classes_ignored(generate):
assert generate([
schema.Class(name="A", derived={"B", "C"}),
schema.Class(name="B", bases=["A"], ipa=schema.IpaInfo()),
schema.Class(name="C", bases=["A"]),
]) == dbscheme.Scheme(
src=schema_file,
includes=[],
declarations=[
dbscheme.Union("@a", ["@c"]),
dbscheme.Table(
name="cs",
columns=[
dbscheme.Column("id", "@c", binding=True),
],
)
],
)
if __name__ == '__main__':
sys.exit(pytest.main([__file__] + sys.argv[1:]))

Просмотреть файл

@ -52,7 +52,6 @@ callable_bodies(
@file =
@db_file
| @unknown_file
;
#keyset[id]
@ -77,7 +76,6 @@ locatable_locations(
@location =
@db_location
| @unknown_location
;
#keyset[id]
@ -135,14 +133,6 @@ diagnostics(
| @unspecified_element
;
unknown_files(
unique int id: @unknown_file
);
unknown_locations(
unique int id: @unknown_location
);
unspecified_elements(
unique int id: @unspecified_element,
string property: string ref,
@ -941,7 +931,6 @@ lazy_initializer_exprs( //dir=expr
@lookup_expr =
@dynamic_lookup_expr
| @member_ref_expr
| @method_ref_expr
| @subscript_expr
;
@ -1334,10 +1323,6 @@ metatype_conversion_exprs( //dir=expr
unique int id: @metatype_conversion_expr
);
method_ref_exprs( //dir=expr
unique int id: @method_ref_expr
);
nil_literal_exprs( //dir=expr
unique int id: @nil_literal_expr
);