OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 def _GenerateArrayField(field_info): | 5 def _GenerateArrayField(field_info): |
6 """Generate a string defining an array field in a C structure. | 6 """Generate a string defining an array field in a C structure. |
7 """ | 7 """ |
8 contents = field_info['contents'] | 8 contents = field_info['contents'] |
9 contents['field'] = '* ' + field_info['field'] | 9 contents['field'] = '* ' + field_info['field'] |
10 if contents['type'] == 'array': | 10 if contents['type'] == 'array': |
(...skipping 10 matching lines...) Expand all Loading... | |
21 if type == 'int': | 21 if type == 'int': |
22 return 'const int %s' % field | 22 return 'const int %s' % field |
23 elif type == 'string': | 23 elif type == 'string': |
24 return 'const char* const %s' % field | 24 return 'const char* const %s' % field |
25 elif type == 'string16': | 25 elif type == 'string16': |
26 return 'const wchar_t* const %s' % field | 26 return 'const wchar_t* const %s' % field |
27 elif type == 'enum': | 27 elif type == 'enum': |
28 return 'const %s %s' % (field_info['ctype'], field) | 28 return 'const %s %s' % (field_info['ctype'], field) |
29 elif type == 'array': | 29 elif type == 'array': |
30 return _GenerateArrayField(field_info) | 30 return _GenerateArrayField(field_info) |
31 elif type == 'object': | |
beaudoin
2015/07/06 16:01:12
Would 'struct' be better than 'object' given that
danduong
2015/07/08 23:07:03
Done.
| |
32 return 'const %s %s' % (field_info['type_name'], field) | |
31 else: | 33 else: |
32 raise RuntimeError('Unknown field type "%s"' % type) | 34 raise RuntimeError('Unknown field type "%s"' % type) |
33 | 35 |
34 def GenerateStruct(type_name, schema): | 36 def GenerateStruct(type_name, schema): |
35 """Generate a string defining a structure containing the fields specified in | 37 """Generate a string defining a structure containing the fields specified in |
36 the schema list. | 38 the schema list. |
37 """ | 39 """ |
38 lines = []; | 40 lines = []; |
39 lines.append('struct %s {' % type_name) | 41 lines.append('struct %s {' % type_name) |
40 for field_info in schema: | 42 for field_info in schema: |
43 if field_info['type'] == 'object': | |
44 lines.insert(0, GenerateStruct(field_info['type_name'], | |
45 field_info['fields'])) | |
46 elif (field_info['type'] == 'array' | |
47 and field_info['contents']['type'] == 'object'): | |
48 contents = field_info['contents'] | |
49 lines.insert(0, GenerateStruct(contents['type_name'], | |
50 contents['fields'])) | |
41 lines.append(' ' + GenerateField(field_info) + ';') | 51 lines.append(' ' + GenerateField(field_info) + ';') |
42 lines.append('};'); | 52 lines.append('};'); |
43 return '\n'.join(lines) + '\n'; | 53 return '\n'.join(lines) + '\n'; |
OLD | NEW |