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

Unified Diff: tools/json_to_struct/element_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: Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_to_struct/element_generator.py
diff --git a/tools/json_to_struct/element_generator.py b/tools/json_to_struct/element_generator.py
index 20d3069a1c07a674e1be8b7e42b1fd24286d95b0..5deacc69800441db16e00c6516c9dd22d17aea76 100644
--- a/tools/json_to_struct/element_generator.py
+++ b/tools/json_to_struct/element_generator.py
@@ -5,6 +5,8 @@
import json
import struct_generator
+field_name_count = {}
beaudoin 2015/07/06 16:01:11 Module-global mutable variables are against the st
danduong 2015/07/08 23:07:03 Done.
+
def _JSONToCString16(json_string_literal):
"""Converts a JSON string literal to a C++ UTF-16 string literal. This is
done by converting \\u#### to \\x####.
@@ -22,42 +24,53 @@ def _JSONToCString16(json_string_literal):
escape_index = c_string_literal.find('\\', escape_index + 6)
return c_string_literal
-def _GenerateString(content, lines):
+def _GenerateString(content, lines, indent=' '):
"""Generates an UTF-8 string to be included in a static structure initializer.
If content is not specified, uses NULL.
"""
if content is None:
- lines.append(' NULL,')
+ lines.append(indent + 'NULL,')
else:
# json.dumps quotes the string and escape characters as required.
- lines.append(' %s,' % json.dumps(content))
+ lines.append(indent + '%s,' % json.dumps(content))
-def _GenerateString16(content, lines):
+def _GenerateString16(content, lines, indent=' '):
"""Generates an UTF-16 string to be included in a static structure
initializer. If content is not specified, uses NULL.
"""
if content is None:
- lines.append(' NULL,')
+ lines.append(indent + 'NULL,')
else:
# json.dumps quotes the string and escape characters as required.
- lines.append(' L%s,' % _JSONToCString16(json.dumps(content)))
+ lines.append(indent + 'L%s,' % _JSONToCString16(json.dumps(content)))
+
+def _GenerateArrayVariableName(element_name, field_name):
+ # Generates a unique variable name for an array variable.
+ global field_name_count
+ var = 'array_%s_%s' % (element_name, field_name)
+ if var not in field_name_count:
+ field_name_count[var] = 0
+ return var
+ new_var = '%s_%d' % (var, field_name_count[var])
+ field_name_count[var] += 1
+ return new_var
-def _GenerateArray(element_name, field_info, content, lines):
+def _GenerateArray(element_name, field_info, content, lines, indent=' '):
"""Generates an array to be included in a static structure initializer. If
content is not specified, uses NULL. The array is assigned to a temporary
variable which is initialized before the structure.
"""
if content is None:
- lines.append(' NULL,')
- lines.append(' 0,') # Size of the array.
+ lines.append(indent + 'NULL,')
+ lines.append(indent + '0,') # Size of the array.
return
# Create a new array variable and use it in the structure initializer.
# This prohibits nested arrays. Add a clash detection and renaming mechanism
# to solve the problem.
- var = 'array_%s_%s' % (element_name, field_info['field']);
- lines.append(' %s,' % var)
- lines.append(' %s,' % len(content)) # Size of the array.
+ var = _GenerateArrayVariableName(element_name, field_info['field'])
+ lines.append(indent + '%s,' % var)
+ lines.append(indent + '%s,' % len(content)) # Size of the array.
# Generate the array content.
array_lines = []
field_info['contents']['field'] = var;
@@ -65,7 +78,7 @@ def _GenerateArray(element_name, field_info, content, lines):
field_info['contents']) + '[] = {')
for subcontent in content:
GenerateFieldContent(element_name, field_info['contents'], subcontent,
- array_lines)
+ array_lines, indent)
array_lines.append('};')
# Prepend the generated array so it is initialized before the structure.
lines.reverse()
@@ -73,7 +86,22 @@ def _GenerateArray(element_name, field_info, content, lines):
lines.extend(array_lines)
lines.reverse()
-def GenerateFieldContent(element_name, field_info, content, lines):
+def _GenerateObject(element_name, field_info, content, lines, indent=' '):
+ """Generates an object to be included in a static structure initializer. If
+ content is not specified, uses {0}.
beaudoin 2015/07/06 16:01:11 Why {0}? Is this always a valid C++ object? Just f
danduong 2015/07/08 23:07:03 {0} should compile. That will initialize the first
+ """
+ if content is None:
+ lines.append(indent + '{0},')
+ return
+
+ fields = field_info['fields']
+ lines.append(indent + '{')
+ for field in fields:
+ subcontent = content.get(field['field'])
+ GenerateFieldContent(element_name, field, subcontent, lines, ' ' + indent)
+ lines.append(indent + '},')
+
+def GenerateFieldContent(element_name, field_info, content, lines, indent=' '):
"""Generate the content of a field to be included in the static structure
initializer. If the field's content is not specified, uses the default value
if one exists.
@@ -82,13 +110,15 @@ def GenerateFieldContent(element_name, field_info, content, lines):
content = field_info.get('default', None)
type = field_info['type']
if type == 'int' or type == 'enum':
- lines.append(' %s,' % content)
+ lines.append('%s%s,' % (indent, content))
elif type == 'string':
- _GenerateString(content, lines)
+ _GenerateString(content, lines, indent)
elif type == 'string16':
- _GenerateString16(content, lines)
+ _GenerateString16(content, lines, indent)
elif type == 'array':
- _GenerateArray(element_name, field_info, content, lines)
+ _GenerateArray(element_name, field_info, content, lines, indent)
+ elif type == 'object':
+ _GenerateObject(element_name, field_info, content, lines, indent)
else:
raise RuntimeError('Unknown field type "%s"' % type)
« no previous file with comments | « no previous file | tools/json_to_struct/element_generator_test.py » ('j') | tools/json_to_struct/json_to_struct.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698