CORD-1335: Support pluralization via xproto + supporting unit test
Change-Id: Ie3c0a5a3354d2846427d51c4675e53e855d25bde
diff --git a/xos/genx/tool/lib.py b/xos/genx/tool/lib.py
index 2230d5c..3c68d46 100644
--- a/xos/genx/tool/lib.py
+++ b/xos/genx/tool/lib.py
@@ -1,5 +1,15 @@
import pdb
import re
+from pattern import en
+
+def xproto_pluralize(field):
+ try:
+ # The user has set a plural, as an exception that cannot be handled automatically
+ plural = field['options']['plural']
+ except KeyError:
+ plural = en.pluralize(field['name'])
+
+ return plural
def django_content_type_string(xptags):
# Check possibility of KeyError in caller
diff --git a/xos/genx/tool/plurals.xtarget b/xos/genx/tool/plurals.xtarget
new file mode 100644
index 0000000..9cdd6c6
--- /dev/null
+++ b/xos/genx/tool/plurals.xtarget
@@ -0,0 +1,6 @@
+{% for m in proto.messages %}
+Model {{ m.name }}:
+{% for f in m.fields -%}
+The plural of {{ f.name}} is {{ xproto_pluralize(f) }}.
+{% endfor %}
+{%- endfor %}
diff --git a/xos/genx/tool/tests/target_test.py b/xos/genx/tool/tests/target_test.py
index b73bf12..f39a994 100644
--- a/xos/genx/tool/tests/target_test.py
+++ b/xos/genx/tool/tests/target_test.py
@@ -28,6 +28,30 @@
self.generate(target=target, kv='what:what is what')
self.assertIn("what is what", self.get_output())
+ def test_pluralize(self):
+ proto = \
+"""
+ message TestPluralize {
+ // The following field has an explicitly specified plural
+ required int anecdote = 1 [plural = "data"];
+ // The following fields have automatically computed plurals
+ required int sheep = 2;
+ required int radius = 2;
+ required int slice = 2;
+ required int network = 2;
+ required int omf_friendly = 2;
+ }
+"""
+
+ target = \
+"""
+{% for m in proto.messages.0.fields -%}
+{{ xproto_pluralize(m) }},
+{%- endfor %}
+"""
+ self.generate(xproto=proto, target=target)
+ self.assertEqual("data,sheep,radii,slices,networks,omf_friendlies", self.get_output().lstrip().rstrip().rstrip(','))
+
if __name__ == '__main__':
unittest.main()