about summary refs log tree commit diff
path: root/universe/ac_types/serialize.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-01-29T14·29+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-01-29T14·29+0000
commitfb9380ba268b3cd27372acadb87b14cc96163374 (patch)
treef65d7fc8d8726499165a0949af39afd1abc8118c /universe/ac_types/serialize.py
parent15110e6de9f85537c7847267caa35fa068aea001 (diff)
parent8ad51b24dd8719840aac47134835ea25cfe1b0b8 (diff)
Add 'universe/' from commit '8ad51b24dd8719840aac47134835ea25cfe1b0b8'
git-subtree-dir: universe
git-subtree-mainline: 15110e6de9f85537c7847267caa35fa068aea001
git-subtree-split: 8ad51b24dd8719840aac47134835ea25cfe1b0b8
Diffstat (limited to 'universe/ac_types/serialize.py')
-rw-r--r--universe/ac_types/serialize.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/universe/ac_types/serialize.py b/universe/ac_types/serialize.py
new file mode 100644
index 000000000000..966a9024f3fa
--- /dev/null
+++ b/universe/ac_types/serialize.py
@@ -0,0 +1,67 @@
+from test_utils import simple_assert
+import string as string
+
+
+def literal(x):
+    if x == True:
+        return 'true'
+    elif x == False:
+        return 'false'
+    elif isinstance(x, int):
+        return str(x)
+    elif x is None:
+        raise Exception("None!")
+    # `x` is a string
+    else:
+        x = string.trim_surrounding('"', x)
+        return "\"{}\"".format(x)
+
+
+actual = [
+    literal(True),
+    literal(9249441),
+    literal("COMPLEXITY"),
+    literal("\"doubly wrapped string\"")
+]
+expected = ["true", "9249441", "\"COMPLEXITY\"", "\"doubly wrapped string\""]
+simple_assert(actual, expected, name="literal")
+
+
+def input(input_type=None, fields=None):
+    header = 'inputs {'
+    input_type_field = '  type: {}'.format(input_type)
+    fields = '\n'.join(
+        ["  {}: {}".format(k, literal(v)) for k, v in fields.items()])
+    if input_type == 'SIGNAL':
+        fields += '\n  signal_type: CID'
+    footer = '}'
+    return '\n'.join([header, input_type_field, fields, footer])
+
+
+actual = input(input_type='CONSTANT',
+               fields={
+                   'consult_frd_id': 'FEATURE',
+                   'is_optional': False,
+                   'constant_value': "regular_review",
+               })
+expected = """inputs {
+  type: CONSTANT
+  consult_frd_id: "FEATURE"
+  is_optional: false
+  constant_value: "regular_review"
+}"""
+simple_assert(actual, expected, name='input')
+
+actual = input(input_type='CONSTANT',
+               fields={
+                   'consult_frd_id': 'FEATURE',
+                   'is_optional': False,
+                   'constant_value': "\"doubly wrapped string\"",
+               })
+expected = """inputs {
+  type: CONSTANT
+  consult_frd_id: "FEATURE"
+  is_optional: false
+  constant_value: "doubly wrapped string"
+}"""
+simple_assert(actual, expected, name='input')