summaryrefslogtreecommitdiffstats
path: root/tools/json_to_struct
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-07 23:31:28 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-07 23:31:28 +0000
commitf47127d70b2dd4a83cabcf6b54ff71c5036768ed (patch)
treefdd5ca8f7e6a61dd727af701af915be9cc695977 /tools/json_to_struct
parent8439af1d26c1c3c29cc75995ba643161c15b4763 (diff)
downloadchromium_src-f47127d70b2dd4a83cabcf6b54ff71c5036768ed.zip
chromium_src-f47127d70b2dd4a83cabcf6b54ff71c5036768ed.tar.gz
chromium_src-f47127d70b2dd4a83cabcf6b54ff71c5036768ed.tar.bz2
Make the JSON-to-struct converter insert the root element name in the names of
the global arrays it generates. This allows more than one prepopulated engine to use an alternate_urls field. BUG=none TEST=none R=kalman@chromium.org Review URL: https://codereview.chromium.org/22339011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@216309 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_to_struct')
-rw-r--r--tools/json_to_struct/element_generator.py13
-rwxr-xr-xtools/json_to_struct/element_generator_test.py48
2 files changed, 32 insertions, 29 deletions
diff --git a/tools/json_to_struct/element_generator.py b/tools/json_to_struct/element_generator.py
index 3a592d8..20d3069 100644
--- a/tools/json_to_struct/element_generator.py
+++ b/tools/json_to_struct/element_generator.py
@@ -42,7 +42,7 @@ def _GenerateString16(content, lines):
# json.dumps quotes the string and escape characters as required.
lines.append(' L%s,' % _JSONToCString16(json.dumps(content)))
-def _GenerateArray(field_info, content, lines):
+def _GenerateArray(element_name, field_info, content, lines):
"""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.
@@ -55,7 +55,7 @@ def _GenerateArray(field_info, content, lines):
# 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' % field_info['field'];
+ var = 'array_%s_%s' % (element_name, field_info['field']);
lines.append(' %s,' % var)
lines.append(' %s,' % len(content)) # Size of the array.
# Generate the array content.
@@ -64,7 +64,8 @@ def _GenerateArray(field_info, content, lines):
array_lines.append(struct_generator.GenerateField(
field_info['contents']) + '[] = {')
for subcontent in content:
- GenerateFieldContent(field_info['contents'], subcontent, array_lines)
+ GenerateFieldContent(element_name, field_info['contents'], subcontent,
+ array_lines)
array_lines.append('};')
# Prepend the generated array so it is initialized before the structure.
lines.reverse()
@@ -72,7 +73,7 @@ def _GenerateArray(field_info, content, lines):
lines.extend(array_lines)
lines.reverse()
-def GenerateFieldContent(field_info, content, lines):
+def GenerateFieldContent(element_name, field_info, content, lines):
"""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.
@@ -87,7 +88,7 @@ def GenerateFieldContent(field_info, content, lines):
elif type == 'string16':
_GenerateString16(content, lines)
elif type == 'array':
- _GenerateArray(field_info, content, lines)
+ _GenerateArray(element_name, field_info, content, lines)
else:
raise RuntimeError('Unknown field type "%s"' % type)
@@ -101,7 +102,7 @@ def GenerateElement(type_name, schema, element_name, element):
if (content == None and not field_info.get('optional', False)):
raise RuntimeError('Mandatory field "%s" omitted in element "%s".' %
(field_info['field'], element_name))
- GenerateFieldContent(field_info, content, lines)
+ GenerateFieldContent(element_name, field_info, content, lines)
lines.append('};')
return '\n'.join(lines)
diff --git a/tools/json_to_struct/element_generator_test.py b/tools/json_to_struct/element_generator_test.py
index 484e83f..67459a1 100755
--- a/tools/json_to_struct/element_generator_test.py
+++ b/tools/json_to_struct/element_generator_test.py
@@ -10,70 +10,72 @@ import unittest
class ElementGeneratorTest(unittest.TestCase):
def testGenerateIntFieldContent(self):
lines = [];
- GenerateFieldContent({'type': 'int', 'default': 5}, None, lines)
+ GenerateFieldContent('', {'type': 'int', 'default': 5}, None, lines)
self.assertEquals([' 5,'], lines)
lines = [];
- GenerateFieldContent({'type': 'int', 'default': 5}, 12, lines)
+ GenerateFieldContent('', {'type': 'int', 'default': 5}, 12, lines)
self.assertEquals([' 12,'], lines)
lines = [];
- GenerateFieldContent({'type': 'int'}, -3, lines)
+ GenerateFieldContent('', {'type': 'int'}, -3, lines)
self.assertEquals([' -3,'], lines)
def testGenerateStringFieldContent(self):
lines = [];
- GenerateFieldContent({'type': 'string', 'default': 'foo_bar'}, None, lines)
+ GenerateFieldContent('', {'type': 'string', 'default': 'foo_bar'}, None,
+ lines)
self.assertEquals([' "foo_bar",'], lines)
lines = [];
- GenerateFieldContent({'type': 'string', 'default': 'foo'}, 'bar\n', lines)
+ GenerateFieldContent('', {'type': 'string', 'default': 'foo'}, 'bar\n',
+ lines)
self.assertEquals([' "bar\\n",'], lines)
lines = [];
- GenerateFieldContent({'type': 'string'}, None, lines)
+ GenerateFieldContent('', {'type': 'string'}, None, lines)
self.assertEquals([' NULL,'], lines)
lines = [];
- GenerateFieldContent({'type': 'string'}, 'foo', lines)
+ GenerateFieldContent('', {'type': 'string'}, 'foo', lines)
self.assertEquals([' "foo",'], lines)
def testGenerateString16FieldContent(self):
lines = [];
- GenerateFieldContent({'type': 'string16', 'default': u'f\u00d8\u00d81a'},
- None, lines)
+ GenerateFieldContent('', {'type': 'string16',
+ 'default': u'f\u00d8\u00d81a'}, None, lines)
self.assertEquals([' L"f\\x00d8" L"\\x00d8" L"1a",'], lines)
lines = [];
- GenerateFieldContent({'type': 'string16', 'default': 'foo'}, u'b\uc3a5r',
- lines)
+ GenerateFieldContent('', {'type': 'string16', 'default': 'foo'},
+ u'b\uc3a5r', lines)
self.assertEquals([' L"b\\xc3a5" L"r",'], lines)
lines = [];
- GenerateFieldContent({'type': 'string16'}, None, lines)
+ GenerateFieldContent('', {'type': 'string16'}, None, lines)
self.assertEquals([' NULL,'], lines)
lines = [];
- GenerateFieldContent({'type': 'string16'}, u'foo\\u1234', lines)
+ GenerateFieldContent('', {'type': 'string16'}, u'foo\\u1234', lines)
self.assertEquals([' L"foo\\\\u1234",'], lines)
def testGenerateEnumFieldContent(self):
lines = [];
- GenerateFieldContent({'type': 'enum', 'default': 'RED'}, None, lines)
+ GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, None, lines)
self.assertEquals([' RED,'], lines)
lines = [];
- GenerateFieldContent({'type': 'enum', 'default': 'RED'}, 'BLACK', lines)
+ GenerateFieldContent('', {'type': 'enum', 'default': 'RED'}, 'BLACK', lines)
self.assertEquals([' BLACK,'], lines)
lines = [];
- GenerateFieldContent({'type': 'enum'}, 'BLUE', lines)
+ GenerateFieldContent('', {'type': 'enum'}, 'BLUE', lines)
self.assertEquals([' BLUE,'], lines)
def testGenerateArrayFieldContent(self):
lines = ['STRUCT BEGINS'];
- GenerateFieldContent({'type': 'array', 'contents': {'type': 'int'}},
+ GenerateFieldContent('test', {'type': 'array', 'contents': {'type': 'int'}},
None, lines)
self.assertEquals(['STRUCT BEGINS', ' NULL,', ' 0,'], lines)
lines = ['STRUCT BEGINS'];
- GenerateFieldContent({'field': 'my_array', 'type': 'array',
- 'contents': {'type': 'int'}}, [3, 4], lines)
- self.assertEquals('const int array_my_array[] = {\n' +
+ GenerateFieldContent('test', {'field': 'my_array', 'type': 'array',
+ 'contents': {'type': 'int'}}, [3, 4], lines)
+ self.assertEquals('const int array_test_my_array[] = {\n' +
' 3,\n' +
' 4,\n' +
'};\n' +
'STRUCT BEGINS\n' +
- ' array_my_array,\n' +
+ ' array_test_my_array,\n' +
' 2,', '\n'.join(lines))
def testGenerateElements(self):
@@ -115,7 +117,7 @@ class ElementGeneratorTest(unittest.TestCase):
' NULL,\n' +
' 0,\n'
'};\n',
- 'elem2': 'const wchar_t* const array_f3[] = {\n' +
+ 'elem2': 'const wchar_t* const array_elem2_f3[] = {\n' +
' L"bar",\n' +
' L"foo",\n' +
'};\n' +
@@ -123,7 +125,7 @@ class ElementGeneratorTest(unittest.TestCase):
' 1000,\n' +
' "foo_bar",\n' +
' MAYBE,\n' +
- ' array_f3,\n' +
+ ' array_elem2_f3,\n' +
' 2,\n'
'};\n'
}