API: Use string type instead of u8.
[vpp.git] / src / tools / vppapigen / vppapigen_json.py
1 # JSON generation
2 import json
3
4
5 def walk_enums(s):
6     r = []
7     for e in s:
8         d = []
9         d.append(e.name)
10         for b in e.block:
11             d.append(b)
12         d.append({'enumtype': e.enumtype})
13         r.append(d)
14     return r
15
16
17 def walk_services(s):
18     r = {}
19     for e in s:
20         d = {'reply': e.reply}
21         if e.stream:
22             d['stream'] = True
23         if e.events:
24             d['events'] = e.events
25         r[e.caller] = d
26     return r
27
28
29 def walk_defs(s):
30     r = []
31     for t in s:
32         d = []
33         d.append(t.name)
34         for b in t.block:
35             if b.type == 'Field':
36                 d.append([b.fieldtype, b.fieldname])
37             elif b.type == 'Array':
38                 if b.lengthfield:
39                     d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield])
40                 else:
41                     d.append([b.fieldtype, b.fieldname, b.length])
42             elif b.type == 'Union':
43                 pass
44             else:
45                 raise ValueError("Error in processing array type %s" % b)
46
47         if t.crc:
48             c = {}
49             c['crc'] = "{0:#0{1}x}".format(t.crc, 10)
50             d.append(c)
51
52         r.append(d)
53     return r
54
55
56 #
57 # Plugin entry point
58 #
59 def run(filename, s, file_crc):
60     j = {}
61
62     j['types'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Typedef'])
63     j['messages'] = walk_defs(s['Define'])
64     j['unions'] = walk_defs([o for o in s['types'] if o.__class__.__name__ == 'Union'])
65     j['enums'] = walk_enums([o for o in s['types'] if o.__class__.__name__ == 'Enum'])
66     j['services'] = walk_services(s['Service'])
67     j['aliases'] = s['Alias']
68     j['vl_api_version'] = hex(file_crc)
69     return json.dumps(j, indent=4, separators=(',', ': '))