Skip to content

Commit 35c1d31

Browse files
committed
Swift: add doc name override
1 parent 8de7df9 commit 35c1d31

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

swift/codegen/generators/qlgen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ def get_ql_property(cls: schema.Class, prop: schema.Property, prev_child: str =
7474
singular=inflection.camelize(prop.name),
7575
tablename=inflection.tableize(cls.name),
7676
tableparams=["this"] + ["result" if p is prop else "_" for p in cls.properties if p.is_single],
77-
doc_name=_humanize(prop.name),
77+
doc_name=_humanize(prop.doc_name or prop.name),
7878
)
7979
elif prop.is_repeated:
8080
args.update(
8181
singular=inflection.singularize(inflection.camelize(prop.name)),
8282
plural=inflection.pluralize(inflection.camelize(prop.name)),
8383
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
8484
tableparams=["this", "index", "result"],
85-
doc_name=_humanize(inflection.singularize(prop.name)),
86-
doc_name_plural=_humanize(inflection.pluralize(prop.name))
85+
doc_name=_humanize(inflection.singularize(prop.doc_name or prop.name)),
86+
doc_name_plural=_humanize(inflection.pluralize(prop.doc_name or prop.name))
8787
)
8888
elif prop.is_optional:
8989
args.update(
9090
singular=inflection.camelize(prop.name),
9191
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
9292
tableparams=["this", "result"],
93-
doc_name=_humanize(prop.name),
93+
doc_name=_humanize(prop.doc_name or prop.name),
9494
)
9595
elif prop.is_predicate:
9696
args.update(

swift/codegen/lib/schema/defs.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from typing import Callable as _Callable, Union as _Union
2-
from functools import singledispatch as _singledispatch
1+
from typing import Callable as _Callable
32
from swift.codegen.lib import schema as _schema
43
import inspect as _inspect
54
from dataclasses import dataclass as _dataclass
@@ -12,6 +11,16 @@ def modify(self, prop: _schema.Property):
1211
prop.is_child = True
1312

1413

14+
@_dataclass
15+
class _DocnameModifier(_schema.PropertyModifier):
16+
doc_name: str
17+
18+
def modify(self, prop: _schema.Property):
19+
if prop.is_predicate:
20+
raise _schema.Error("Predicates cannot have a doc name")
21+
prop.doc_name = self.doc_name
22+
23+
1524
@_dataclass
1625
class _DescModifier(_schema.PropertyModifier):
1726
description: str
@@ -106,6 +115,7 @@ def f(cls: type) -> type:
106115
list = _TypeModifier(_Listifier())
107116

108117
child = _ChildModifier()
118+
docname = _DocnameModifier
109119
desc = _DescModifier
110120

111121
qltest = _Namespace(

swift/codegen/test/test_qlgen.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,5 +649,40 @@ def test_property_doc(generate_classes):
649649
}
650650

651651

652+
def test_property_doc_name_override(generate_classes):
653+
assert generate_classes([
654+
schema.Class("MyObject", properties=[
655+
schema.SingleProperty("foo", "bar", doc_name="baz")]),
656+
]) == {
657+
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
658+
ql.Class(name="MyObject", final=True,
659+
properties=[
660+
ql.Property(singular="Foo", type="bar", tablename="my_objects",
661+
tableparams=["this", "result"], doc_name="baz"),
662+
])),
663+
}
664+
665+
666+
def test_repeated_property_doc_name_override(generate_classes):
667+
assert generate_classes([
668+
schema.Class("MyObject", properties=[
669+
schema.RepeatedProperty("x", "int", doc_name="children"),
670+
schema.RepeatedOptionalProperty("y", "int", doc_name="child")]),
671+
]) == {
672+
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
673+
ql.Class(name="MyObject", final=True,
674+
properties=[
675+
ql.Property(singular="X", plural="Xes", type="int",
676+
tablename="my_object_xes",
677+
tableparams=["this", "index", "result"],
678+
doc_name="child", doc_name_plural="children"),
679+
ql.Property(singular="Y", plural="Ys", type="int",
680+
tablename="my_object_ies", is_optional=True,
681+
tableparams=["this", "index", "result"],
682+
doc_name="child", doc_name_plural="children"),
683+
])),
684+
}
685+
686+
652687
if __name__ == '__main__':
653688
sys.exit(pytest.main([__file__] + sys.argv[1:]))

swift/codegen/test/test_schema.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,5 +542,25 @@ class A:
542542
}
543543

544544

545+
def test_property_doc_name_override():
546+
@schema.load
547+
class data:
548+
class A:
549+
x: int | defs.docname("y")
550+
551+
assert data.classes == {
552+
'A': schema.Class('A', properties=[
553+
schema.SingleProperty('x', 'int', doc_name="y")]),
554+
}
555+
556+
557+
def test_predicate_cannot_have_doc_name_override():
558+
with pytest.raises(schema.Error):
559+
@schema.load
560+
class data:
561+
class A:
562+
x: defs.predicate | defs.docname("y")
563+
564+
545565
if __name__ == '__main__':
546566
sys.exit(pytest.main([__file__] + sys.argv[1:]))

0 commit comments

Comments
 (0)