about summary refs log tree commit diff
path: root/universe/ac_types/serialize.py
blob: 966a9024f3fade3acb467e1539c6c884d3b48fb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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')