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

Side by Side Diff: tools/json_to_struct/json_to_struct.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
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2012 The Chromium Authors. All rights reserved. 2 # Copyright 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 # Format for the JSON schema file: 6 # Format for the JSON schema file:
7 # { 7 # {
8 # "type_name": "DesiredCStructName", 8 # "type_name": "DesiredCStructName",
9 # "headers": [ // Optional list of headers to be included by the .h. 9 # "headers": [ // Optional list of headers to be included by the .h.
10 # "path/to/header.h" 10 # "path/to/header.h"
11 # ], 11 # ],
12 # "schema": [ // Fields of the generated structure. 12 # "schema": [ // Fields of the generated structure.
13 # { 13 # {
14 # "field": "my_enum_field", 14 # "field": "my_enum_field",
15 # "type": "enum", // Either: int, string, string16, enum, array. 15 # "type": "enum", // Either: int, string, string16, enum, array, struct.
16 # "default": "RED", // Optional. Cannot be used for array. 16 # "default": "RED", // Optional. Cannot be used for array.
17 # "ctype": "Color" // Only for enum, specify the C type. 17 # "ctype": "Color" // Only for enum, specify the C type.
18 # }, 18 # },
19 # { 19 # {
20 # "field": "my_int_array_field", // my_int_array_field_size will also 20 # "field": "my_int_array_field", // my_int_array_field_size will also
21 # "type": "array", // be generated. 21 # "type": "array", // be generated.
22 # "contents": { 22 # "contents": {
23 # "type": "int" // Either: int, string, string16, enum, array. 23 # "type": "int" // Either: int, string, string16, enum, array.
24 # } 24 # }
25 # }, 25 # },
26 # {
27 # "field": "my_struct_field",
28 # "type_name": "PointStuct",
29 # "type": "struct",
30 # "fields": [
31 # {"field": "x", "type": "int"},
32 # {"field": "y", "type": "int"}
33 # ]
34 # },
26 # ... 35 # ...
27 # ] 36 # ]
28 # } 37 # }
29 # 38 #
30 # Format for the JSON description file: 39 # Format for the JSON description file:
31 # { 40 # {
32 # "int_variables": { // An optional list of constant int variables. 41 # "int_variables": { // An optional list of constant int variables.
33 # "kDesiredConstantName": 45 42 # "kDesiredConstantName": 45
34 # }, 43 # },
35 # "elements": { // All the elements for which to create static 44 # "elements": { // All the elements for which to create static
36 # // initialization code in the .cc file. 45 # // initialization code in the .cc file.
37 # "my_const_variable": { 46 # "my_const_variable": {
38 # "my_int_field": 10, 47 # "my_int_field": 10,
39 # "my_string_field": "foo bar", 48 # "my_string_field": "foo bar",
40 # "my_enum_field": "BLACK", 49 # "my_enum_field": "BLACK",
41 # "my_int_array_field": [ 1, 2, 3, 5, 7 ], 50 # "my_int_array_field": [ 1, 2, 3, 5, 7 ],
51 # "my_struct_field": {"x": 1, "y": 2}
42 # }, 52 # },
43 # "my_other_const_variable": { 53 # "my_other_const_variable": {
44 # ... 54 # ...
45 # } 55 # }
46 # } 56 # }
47 # } 57 # }
48 58
49 import json 59 import json
50 from datetime import datetime 60 from datetime import datetime
51 import os.path 61 import os.path
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 f.write('\n') 122 f.write('\n')
113 123
114 if namespace: 124 if namespace:
115 f.write('namespace %s {\n' % namespace) 125 f.write('namespace %s {\n' % namespace)
116 f.write('\n') 126 f.write('\n')
117 127
118 f.write(struct_generator.GenerateStruct( 128 f.write(struct_generator.GenerateStruct(
119 schema['type_name'], schema['schema'])) 129 schema['type_name'], schema['schema']))
120 f.write('\n') 130 f.write('\n')
121 131
122 for var_name, value in description.get('int_variables', []).items(): 132 for var_name, value in description.get('int_variables', {}).items():
123 f.write('extern const int %s;\n' % var_name) 133 f.write('extern const int %s;\n' % var_name)
124 f.write('\n') 134 f.write('\n')
125 135
126 for element_name, element in description['elements'].items(): 136 for element_name, element in description['elements'].items():
127 f.write('extern const %s %s;\n' % (schema['type_name'], element_name)) 137 f.write('extern const %s %s;\n' % (schema['type_name'], element_name))
128 138
129 if namespace: 139 if namespace:
130 f.write('\n') 140 f.write('\n')
131 f.write('} // namespace %s\n' % namespace) 141 f.write('} // namespace %s\n' % namespace)
132 142
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 basepath = os.path.normpath(opts.destbase) 212 basepath = os.path.normpath(opts.destbase)
203 else: 213 else:
204 basepath = '' 214 basepath = ''
205 215
206 schema = _Load(opts.schema) 216 schema = _Load(opts.schema)
207 description = _Load(description_filename) 217 description = _Load(description_filename)
208 218
209 head = HEAD % (datetime.now().year, opts.schema, description_filename) 219 head = HEAD % (datetime.now().year, opts.schema, description_filename)
210 _GenerateH(basepath, output_root, head, opts.namespace, schema, description) 220 _GenerateH(basepath, output_root, head, opts.namespace, schema, description)
211 _GenerateCC(basepath, output_root, head, opts.namespace, schema, description) 221 _GenerateCC(basepath, output_root, head, opts.namespace, schema, description)
OLDNEW
« no previous file with comments | « tools/json_to_struct/element_generator_test.py ('k') | tools/json_to_struct/struct_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698