Skip to content

Commit 4d87abe

Browse files
committed
Swift: generate docname in qlgen
1 parent 5f7fa6f commit 4d87abe

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

swift/codegen/generators/qlgen.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,42 @@ class NoClasses(Error):
5555
pass
5656

5757

58+
def _humanize(s):
59+
ret = inflection.humanize(s)
60+
return re.sub(r"^\w", lambda m: m[0].lower(), ret)
61+
62+
5863
def get_ql_property(cls: schema.Class, prop: schema.Property, prev_child: str = "") -> ql.Property:
5964
args = dict(
6065
type=prop.type if not prop.is_predicate else "predicate",
6166
qltest_skip="qltest_skip" in prop.pragmas,
6267
prev_child=prev_child if prop.is_child else None,
6368
is_optional=prop.is_optional,
6469
is_predicate=prop.is_predicate,
70+
doc=prop.doc,
6571
)
6672
if prop.is_single:
6773
args.update(
6874
singular=inflection.camelize(prop.name),
6975
tablename=inflection.tableize(cls.name),
7076
tableparams=["this"] + ["result" if p is prop else "_" for p in cls.properties if p.is_single],
77+
doc_name=_humanize(prop.name),
7178
)
7279
elif prop.is_repeated:
7380
args.update(
7481
singular=inflection.singularize(inflection.camelize(prop.name)),
7582
plural=inflection.pluralize(inflection.camelize(prop.name)),
7683
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
7784
tableparams=["this", "index", "result"],
85+
doc_name=_humanize(inflection.singularize(prop.name)),
86+
doc_name_plural=_humanize(inflection.pluralize(prop.name))
7887
)
7988
elif prop.is_optional:
8089
args.update(
8190
singular=inflection.camelize(prop.name),
8291
tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
8392
tableparams=["this", "result"],
93+
doc_name=_humanize(prop.name),
8494
)
8595
elif prop.is_predicate:
8696
args.update(

swift/codegen/lib/ql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class Property:
3838
is_predicate: bool = False
3939
prev_child: Optional[str] = None
4040
qltest_skip: bool = False
41+
doc: List[str] = field(default_factory=list)
42+
doc_name: Optional[str] = None
43+
doc_name_plural: Optional[str] = None
4144

4245
def __post_init__(self):
4346
if self.tableparams:

swift/codegen/test/test_qlgen.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def test_single_property(generate_classes):
190190
ql.Class(name="MyObject", final=True,
191191
properties=[
192192
ql.Property(singular="Foo", type="bar", tablename="my_objects",
193-
tableparams=["this", "result"]),
193+
tableparams=["this", "result"], doc_name="foo"),
194194
])),
195195
}
196196

@@ -200,13 +200,13 @@ def test_children(generate_classes):
200200
schema.Class("FakeRoot"),
201201
schema.Class("MyObject", properties=[
202202
schema.SingleProperty("a", "int"),
203-
schema.SingleProperty("child1", "int", is_child=True),
204-
schema.RepeatedProperty("b", "int"),
205-
schema.RepeatedProperty("child2", "int", is_child=True),
203+
schema.SingleProperty("child_1", "int", is_child=True),
204+
schema.RepeatedProperty("bs", "int"),
205+
schema.RepeatedProperty("children", "int", is_child=True),
206206
schema.OptionalProperty("c", "int"),
207-
schema.OptionalProperty("child3", "int", is_child=True),
207+
schema.OptionalProperty("child_3", "int", is_child=True),
208208
schema.RepeatedOptionalProperty("d", "int"),
209-
schema.RepeatedOptionalProperty("child4", "int", is_child=True),
209+
schema.RepeatedOptionalProperty("child_4", "int", is_child=True),
210210
]),
211211
]) == {
212212
"FakeRoot.qll": (ql.Stub(name="FakeRoot", base_import=gen_import_prefix + "FakeRoot"),
@@ -215,27 +215,31 @@ def test_children(generate_classes):
215215
ql.Class(name="MyObject", final=True,
216216
properties=[
217217
ql.Property(singular="A", type="int", tablename="my_objects",
218-
tableparams=["this", "result", "_"]),
218+
tableparams=["this", "result", "_"], doc_name="a"),
219219
ql.Property(singular="Child1", type="int", tablename="my_objects",
220-
tableparams=["this", "_", "result"], prev_child=""),
220+
tableparams=["this", "_", "result"], prev_child="",
221+
doc_name="child 1"),
221222
ql.Property(singular="B", plural="Bs", type="int",
222223
tablename="my_object_bs",
223-
tableparams=["this", "index", "result"]),
224-
ql.Property(singular="Child2", plural="Child2s", type="int",
225-
tablename="my_object_child2s",
226-
tableparams=["this", "index", "result"], prev_child="Child1"),
224+
tableparams=["this", "index", "result"],
225+
doc_name="b", doc_name_plural="bs"),
226+
ql.Property(singular="Child", plural="Children", type="int",
227+
tablename="my_object_children",
228+
tableparams=["this", "index", "result"], prev_child="Child1",
229+
doc_name="child", doc_name_plural="children"),
227230
ql.Property(singular="C", type="int", tablename="my_object_cs",
228-
tableparams=["this", "result"], is_optional=True),
229-
ql.Property(singular="Child3", type="int", tablename="my_object_child3s",
231+
tableparams=["this", "result"], is_optional=True, doc_name="c"),
232+
ql.Property(singular="Child3", type="int", tablename="my_object_child_3s",
230233
tableparams=["this", "result"], is_optional=True,
231-
prev_child="Child2"),
234+
prev_child="Child", doc_name="child 3"),
232235
ql.Property(singular="D", plural="Ds", type="int",
233236
tablename="my_object_ds",
234-
tableparams=["this", "index", "result"], is_optional=True),
237+
tableparams=["this", "index", "result"], is_optional=True,
238+
doc_name="d", doc_name_plural="ds"),
235239
ql.Property(singular="Child4", plural="Child4s", type="int",
236-
tablename="my_object_child4s",
240+
tablename="my_object_child_4s",
237241
tableparams=["this", "index", "result"], is_optional=True,
238-
prev_child="Child3"),
242+
prev_child="Child3", doc_name="child 4", doc_name_plural="child 4s"),
239243
])),
240244
}
241245

@@ -252,11 +256,11 @@ def test_single_properties(generate_classes):
252256
ql.Class(name="MyObject", final=True,
253257
properties=[
254258
ql.Property(singular="One", type="x", tablename="my_objects",
255-
tableparams=["this", "result", "_", "_"]),
259+
tableparams=["this", "result", "_", "_"], doc_name="one"),
256260
ql.Property(singular="Two", type="y", tablename="my_objects",
257-
tableparams=["this", "_", "result", "_"]),
261+
tableparams=["this", "_", "result", "_"], doc_name="two"),
258262
ql.Property(singular="Three", type="z", tablename="my_objects",
259-
tableparams=["this", "_", "_", "result"]),
263+
tableparams=["this", "_", "_", "result"], doc_name="three"),
260264
])),
261265
}
262266

@@ -274,7 +278,7 @@ def test_optional_property(generate_classes, is_child, prev_child):
274278
ql.Class(name="MyObject", final=True, properties=[
275279
ql.Property(singular="Foo", type="bar", tablename="my_object_foos",
276280
tableparams=["this", "result"],
277-
is_optional=True, prev_child=prev_child),
281+
is_optional=True, prev_child=prev_child, doc_name="foo"),
278282
])),
279283
}
280284

@@ -291,7 +295,8 @@ def test_repeated_property(generate_classes, is_child, prev_child):
291295
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
292296
ql.Class(name="MyObject", final=True, properties=[
293297
ql.Property(singular="Foo", plural="Foos", type="bar", tablename="my_object_foos",
294-
tableparams=["this", "index", "result"], prev_child=prev_child),
298+
tableparams=["this", "index", "result"], prev_child=prev_child,
299+
doc_name="foo", doc_name_plural="foos"),
295300
])),
296301
}
297302

@@ -310,7 +315,7 @@ def test_repeated_optional_property(generate_classes, is_child, prev_child):
310315
ql.Class(name="MyObject", final=True, properties=[
311316
ql.Property(singular="Foo", plural="Foos", type="bar", tablename="my_object_foos",
312317
tableparams=["this", "index", "result"], is_optional=True,
313-
prev_child=prev_child),
318+
prev_child=prev_child, doc_name="foo", doc_name_plural="foos"),
314319
])),
315320
}
316321

@@ -323,8 +328,7 @@ def test_predicate_property(generate_classes):
323328
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
324329
ql.Class(name="MyObject", final=True, properties=[
325330
ql.Property(singular="isFoo", type="predicate", tablename="my_object_is_foo",
326-
tableparams=["this"],
327-
is_predicate=True),
331+
tableparams=["this"], is_predicate=True),
328332
])),
329333
}
330334

@@ -342,7 +346,7 @@ def test_single_class_property(generate_classes, is_child, prev_child):
342346
ql.Property(singular="Foo", type="Bar", tablename="my_objects",
343347
tableparams=[
344348
"this", "result"],
345-
prev_child=prev_child),
349+
prev_child=prev_child, doc_name="foo"),
346350
],
347351
)),
348352
"Bar.qll": (ql.Stub(name="Bar", base_import=gen_import_prefix + "Bar"),
@@ -629,5 +633,21 @@ def test_test_class_hierarchy_uncollapse_at_final(opts, generate_tests):
629633
}
630634

631635

636+
def test_property_doc(generate_classes):
637+
doc = ["Lorem", "Ipsum"]
638+
assert generate_classes([
639+
schema.Class("MyObject", properties=[
640+
schema.SingleProperty("foo", "bar", doc=doc),
641+
]),
642+
]) == {
643+
"MyObject.qll": (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
644+
ql.Class(name="MyObject", final=True,
645+
properties=[
646+
ql.Property(singular="Foo", type="bar", tablename="my_objects",
647+
tableparams=["this", "result"], doc_name="foo", doc=doc),
648+
])),
649+
}
650+
651+
632652
if __name__ == '__main__':
633653
sys.exit(pytest.main([__file__] + sys.argv[1:]))

0 commit comments

Comments
 (0)