Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: tools/json_to_struct/struct_generator.py

Issue 1209733002: Add the ability to code generated prepopulated static nested structs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add test Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/json_to_struct/json_to_struct.py ('k') | tools/json_to_struct/struct_generator_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 == 'struct':
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'] == 'struct':
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'] == 'struct'):
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';
OLDNEW
« no previous file with comments | « tools/json_to_struct/json_to_struct.py ('k') | tools/json_to_struct/struct_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698