diff options
author | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-01 03:58:06 +0000 |
---|---|---|
committer | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-01 03:58:06 +0000 |
commit | 0ef7f5a1c14f3378567e942715c442267dab4901 (patch) | |
tree | 7209092892f3868ad7930dc5d93491682be98131 /tools/json_schema_compiler | |
parent | 8011b8f5dd6a4f289de28b8d7045f8a981723cb7 (diff) | |
download | chromium_src-0ef7f5a1c14f3378567e942715c442267dab4901.zip chromium_src-0ef7f5a1c14f3378567e942715c442267dab4901.tar.gz chromium_src-0ef7f5a1c14f3378567e942715c442267dab4901.tar.bz2 |
JSON schema compiler can't handle strings as types
JSON schema compiler now supports having strings as types.
Similar to http://crbug.com/122075
BUG=124771
TEST=cpp_type_generator_test.py
Review URL: http://codereview.chromium.org/10206034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@134674 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
-rw-r--r-- | tools/json_schema_compiler/cc_generator.py | 20 | ||||
-rwxr-xr-x | tools/json_schema_compiler/compiler.py | 4 | ||||
-rw-r--r-- | tools/json_schema_compiler/cpp_type_generator.py | 11 | ||||
-rwxr-xr-x | tools/json_schema_compiler/cpp_type_generator_test.py | 41 | ||||
-rw-r--r-- | tools/json_schema_compiler/h_generator.py | 5 | ||||
-rw-r--r-- | tools/json_schema_compiler/model.py | 2 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/dependencyTester.json | 32 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/fontSettings.json | 555 |
8 files changed, 662 insertions, 8 deletions
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py index eaf6fb3..b8adaaf 100644 --- a/tools/json_schema_compiler/cc_generator.py +++ b/tools/json_schema_compiler/cc_generator.py @@ -354,7 +354,7 @@ class CCGenerator(object): return '%s.release()' % self._util_cc_helper.CreateValueFromArray( self._cpp_type_generator.GetReferencedProperty(prop), var, prop.optional) - elif prop.type_.is_fundamental: + elif self._IsFundamentalOrFundamentalRef(prop): if prop.optional: var = '*' + var return { @@ -461,17 +461,22 @@ class CCGenerator(object): .Append(' return %(failure_value)s;') ) - if prop.type_.is_fundamental: + if self._IsFundamentalOrFundamentalRef(prop): if prop.optional: (c.Append('%(ctype)s temp;') .Append('if (%s)' % - cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) + cpp_util.GetAsFundamentalValue( + self._cpp_type_generator.GetReferencedProperty(prop), + value_var, + '&temp')) .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') ) else: (c.Append('if (!%s)' % cpp_util.GetAsFundamentalValue( - prop, value_var, '&%s->%s' % (dst, prop.unix_name))) + self._cpp_type_generator.GetReferencedProperty(prop), + value_var, + '&%s->%s' % (dst, prop.unix_name))) .Append('return %(failure_value)s;') ) elif self._IsObjectOrObjectRef(prop): @@ -657,3 +662,10 @@ class CCGenerator(object): """ return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == PropertyType.ARRAY) + + def _IsFundamentalOrFundamentalRef(self, prop): + """Determines if this property is a Fundamental type or is a ref to a + Fundamental type. + """ + return (self._cpp_type_generator.GetReferencedProperty(prop).type_. + is_fundamental) diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py index 38235e0..1d08841 100755 --- a/tools/json_schema_compiler/compiler.py +++ b/tools/json_schema_compiler/compiler.py @@ -78,7 +78,7 @@ def handle_single_schema(filename, dest_dir, root, root_namespace): continue type_generator.AddNamespace( referenced_namespace, - referenced_namespace.unix_name) + referenced_namespace.name) h_code = (h_generator.HGenerator(namespace, type_generator) .Generate().Render()) @@ -120,7 +120,7 @@ def handle_bundle_schema(filenames, dest_dir, root, root_namespace): for referenced_namespace in api_model.namespaces.values(): type_generator.AddNamespace( referenced_namespace, - referenced_namespace.unix_name) + referenced_namespace.name) generator = schema_bundle_generator.SchemaBundleGenerator( api_model, api_defs, type_generator) diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py index 0a1768b..5afc915 100644 --- a/tools/json_schema_compiler/cpp_type_generator.py +++ b/tools/json_schema_compiler/cpp_type_generator.py @@ -175,12 +175,19 @@ class CppTypeGenerator(object): for namespace, types in sorted(self._NamespaceTypeDependencies().items()): c.Append('namespace %s {' % namespace.name) for type_ in types: - if namespace.types[type_].type_ != PropertyType.ARRAY: + if namespace.types[type_].type_ == PropertyType.STRING: + c.Append('typedef std::string %s;' % type_) + elif namespace.types[type_].type_ == PropertyType.ARRAY: + c.Append('typedef std::vector<%(item_type)s> %(name)s;') + c.Substitute({'name': type_, 'item_type': + self.GetType(namespace.types[type_].item_type, + wrap_optional=True)}) + else: c.Append('struct %s;' % type_) c.Append('}') c.Concat(self.GetNamespaceStart()) for (name, type_) in self._namespace.types.items(): - if not type_.functions and type_.type_ != PropertyType.ARRAY: + if not type_.functions and type_.type_ == PropertyType.OBJECT: c.Append('struct %s;' % name) c.Concat(self.GetNamespaceEnd()) return c diff --git a/tools/json_schema_compiler/cpp_type_generator_test.py b/tools/json_schema_compiler/cpp_type_generator_test.py index 4770fe3..708bd46 100755 --- a/tools/json_schema_compiler/cpp_type_generator_test.py +++ b/tools/json_schema_compiler/cpp_type_generator_test.py @@ -27,6 +27,14 @@ class CppTypeGeneratorTest(unittest.TestCase): self.model.AddNamespace(self.browser_action_json[0], 'path/to/browserAction.json') self.browser_action = self.model.namespaces.get('browserAction') + self.font_settings_json = CachedLoad('test/fontSettings.json') + self.model.AddNamespace(self.font_settings_json[0], + 'path/to/fontSettings.json') + self.font_settings = self.model.namespaces.get('fontSettings') + self.dependency_tester_json = CachedLoad('test/dependencyTester.json') + self.model.AddNamespace(self.dependency_tester_json[0], + 'path/to/dependencyTester.json') + self.dependency_tester = self.model.namespaces.get('dependencyTester') def testGenerateIncludesAndForwardDeclarations(self): manager = CppTypeGenerator('', self.windows, self.windows.unix_name) @@ -71,6 +79,32 @@ class CppTypeGeneratorTest(unittest.TestCase): '} // windows', manager.GenerateForwardDeclarations().Render()) + def testGenerateIncludesAndForwardDeclarationsDependencies(self): + m = model.Model() + browser_action_namespace = m.AddNamespace(self.browser_action_json[0], + 'path/to/browserAction.json') + font_settings_namespace = m.AddNamespace(self.font_settings_json[0], + 'path/to/fontSettings.json') + manager = CppTypeGenerator('', self.dependency_tester, + self.dependency_tester.unix_name) + manager.AddNamespace(browser_action_namespace, + self.browser_action.unix_name) + manager.AddNamespace(font_settings_namespace, + self.font_settings.unix_name) + self.assertEquals('#include "path/to/browser_action.h"\n' + '#include "path/to/font_settings.h"', + manager.GenerateIncludes().Render()) + self.assertEquals( + 'namespace browserAction {\n' + 'typedef std::vector<int> ColorArray;\n' + '}\n' + 'namespace fontSettings {\n' + 'typedef std::string ScriptCode;\n' + '}\n' + 'namespace dependency_tester {\n' + '} // dependency_tester', + manager.GenerateForwardDeclarations().Render()) + def testChoicesEnum(self): manager = CppTypeGenerator('', self.tabs, self.tabs.unix_name) prop = self.tabs.functions['move'].params[0] @@ -93,6 +127,13 @@ class CppTypeGeneratorTest(unittest.TestCase): manager.GetType( self.tabs.types['Tab'].properties['selected'])) + def testStringAsType(self): + manager = CppTypeGenerator('', self.font_settings, + self.font_settings.unix_name) + self.assertEquals('std::string', + manager.GetType( + self.font_settings.types['ScriptCode'])) + def testArrayAsType(self): manager = CppTypeGenerator('', self.browser_action, self.browser_action.unix_name) diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py index 35e8248..9ce2868 100644 --- a/tools/json_schema_compiler/h_generator.py +++ b/tools/json_schema_compiler/h_generator.py @@ -179,6 +179,11 @@ class HGenerator(object): c.Substitute({'classname': classname, 'item_type': self._cpp_type_generator.GetType(type_.item_type, wrap_optional=True)}) + elif type_.type_ == PropertyType.STRING: + if type_.description: + c.Comment(type_.description) + c.Append('typedef std::string %(classname)s;') + c.Substitute({'classname': classname}) else: if type_.description: c.Comment(type_.description) diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py index 75ae60e..86716a5 100644 --- a/tools/json_schema_compiler/model.py +++ b/tools/json_schema_compiler/model.py @@ -76,6 +76,8 @@ class Type(object): self.item_type = Property(self, name + "Element", json['items'], from_json=True, from_client=True) + elif json.get('type') == 'string': + self.type_ = PropertyType.STRING else: if not ( 'properties' in json or diff --git a/tools/json_schema_compiler/test/dependencyTester.json b/tools/json_schema_compiler/test/dependencyTester.json new file mode 100644 index 0000000..43b145a --- /dev/null +++ b/tools/json_schema_compiler/test/dependencyTester.json @@ -0,0 +1,32 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +[ + { + "namespace": "dependencyTester", + "dependencies": [ "browserAction", "fontSettings" ], + "types": [], + "functions": [ + { + "name": "setTitle", + "type": "function", + "description": "hi", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "color": { + "$ref": "ColorArray" + }, + "scriptCode": { + "$ref": "ScriptCode" + } + } + } + ] + } + ] + } +] diff --git a/tools/json_schema_compiler/test/fontSettings.json b/tools/json_schema_compiler/test/fontSettings.json new file mode 100644 index 0000000..be68083 --- /dev/null +++ b/tools/json_schema_compiler/test/fontSettings.json @@ -0,0 +1,555 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +[ + { + "namespace": "fontSettings", + "types": [ + { + "id": "FontName", + "type": "object", + "description": "Represents a font name.", + "properties": { + "fontName": { + "type": "string", + "description": "The font name." + }, + "localizedName": { + "type": "string", + "description": "The font name localized for the current locale." + } + } + }, + { + "id": "ScriptCode", + "type": "string", + "enum": [ "Arab", "Armn", "Beng", "Cans", "Cher", "Cyrl", "Deva", "Ethi", "Geor", + "Grek", "Gujr", "Guru", "Hang", "Hans", "Hant", "Hebr", "Hrkt", "Knda", + "Khmr", "Laoo", "Mlym", "Mong", "Mymr", "Orya", "Sinh", "Taml", "Telu", + "Thaa", "Thai", "Tibt", "Yiii" + ], + "description": "An ISO 15924 script code." + }, + { + "id": "GenericFamily", + "type": "string", + "enum": ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"], + "description": "A CSS generic font family." + }, + { + "id": "LevelOfControl", + "description": "One of<br><var>not_controllable</var>: cannot be controlled by any extension<br><var>controlled_by_other_extensions</var>: controlled by extensions with higher precedence<br><var>controllable_by_this_extension</var>: can be controlled by this extension<br><var>controlled_by_this_extension</var>: controlled by this extension", + "type": "string", + "enum": ["not_controllable", "controlled_by_other_extensions", "controllable_by_this_extension", "controlled_by_this_extension"] + } + ], + "functions": [ + { + "name": "clearFont", + "description": "Clears the font set by this extension, if any.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "script": { + "$ref": "ScriptCode", + "description": "The script for which the font should be cleared. If omitted, the global script font setting is cleared.", + "optional": true + }, + "genericFamily": { + "$ref": "GenericFamily", + "description": "The generic font family for which the font should be cleared." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getFont", + "description": "Gets the font for a given script and generic font family.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "script": { + "$ref": "ScriptCode", + "description": "The script for which the font should be retrieved. If omitted, the font for the global script is retrieved.", + "optional": true + }, + "genericFamily": { + "$ref": "GenericFamily", + "description": "The generic font family for which the font should be retrieved." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "fontName": { + "type": "string", + "description": "The font name. Rather than the literal font name preference value, this may be the name of the font that the system resolves the preference value to. The empty string signifies fallback to the global script font setting." + } + } + } + ] + } + ] + }, + { + "name": "setFont", + "description": "Sets the font for a given script and generic font family.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "script": { + "$ref": "ScriptCode", + "description": "The script code which the font should be set. If omitted, the font for the global script is set.", + "optional": true + }, + "genericFamily": { + "$ref": "GenericFamily", + "description": "The generic font family for which the font should be set." + }, + "fontName": { + "type": "string", + "description": "The font name. If a script is specified, the empty string means to fallback to the global script font setting." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getFontList", + "description": "Gets a list of fonts on the system.", + "parameters": [ + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "results", + "type": "array", + "items": { "$ref": "FontName" } + } + ] + } + ] + }, + { + "name": "clearDefaultFontSize", + "description": "Clears the default font size set by this extension, if any.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getDefaultFontSize", + "description": "Gets the default font size.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + } + ] + } + ] + }, + { + "name": "setDefaultFontSize", + "description": "Sets the default font size.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "clearDefaultFixedFontSize", + "description": "Clears the default fixed font size set by this extension, if any.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getDefaultFixedFontSize", + "description": "Gets the default size for fixed width fonts.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + } + ] + } + ] + }, + { + "name": "setDefaultFixedFontSize", + "description": "Sets the default size for fixed width fonts.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "clearMinimumFontSize", + "description": "Clears the minimum font size set by this extension, if any.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getMinimumFontSize", + "description": "Gets the minimum font size.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + } + ] + } + ] + }, + { + "name": "setMinimumFontSize", + "description": "Sets the minimum font size.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + } + } + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "clearDefaultCharacterSet", + "description": "Clears the default character set set by this extension, if any.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "type": "function", + "name": "callback", + "optional": true, + "parameters": [] + } + ] + }, + { + "name": "getDefaultCharacterSet", + "description": "Gets the default character set.", + "parameters": [ + { + "name": "details", + "type": "object", + "optional": true, + "description": "This parameter is currently unused.", + "properties": {} + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "charset": { + "type": "string", + "description": "The default character set, such as \"ISO-8859-1\"." + } + } + } + ] + } + ] + }, + { + "name": "setDefaultCharacterSet", + "description": "Sets the default character set.", + "parameters": [ + { + "name": "details", + "type": "object", + "properties": { + "charset": { + "type": "string", + "description": "The character set." + } + } + }, + { + "name": "callback", + "type": "function", + "optional": true, + "parameters": [] + } + ] + } + ], + "events": [ + { + "name": "onFontChanged", + "description": "Fired when a font setting changes.", + "parameters": [ + { + "type": "object", + "name": "details", + "properties": { + "fontName": { "type": "string" }, + "script": { + "$ref": "ScriptCode", + "description": "The script code for which the font setting has changed. If omitted, the global script font setting has changed.", + "optional": true + }, + "genericFamily": { + "$ref": "GenericFamily", + "description": "The generic font family for which the font setting has changed." + }, + "levelOfControl": { + "$ref": "LevelOfControl", + "description": "The level of control this extension has over the setting." + } + } + } + ] + }, + { + "name": "onDefaultFontSizeChanged", + "description": "Fired when the default font size setting changes.", + "parameters": [ + { + "type": "object", + "name": "details", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + }, + "levelOfControl": { + "$ref": "LevelOfControl", + "description": "The level of control this extension has over the setting." + } + } + } + ] + }, + { + "name": "onDefaultFixedFontSizeChanged", + "description": "Fired when the default fixed font size setting changes.", + "parameters": [ + { + "type": "object", + "name": "details", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + }, + "levelOfControl": { + "$ref": "LevelOfControl", + "description": "The level of control this extension has over the setting." + } + } + } + ] + }, + { + "name": "onMinimumFontSizeChanged", + "description": "Fired when the minimum font size setting changes.", + "parameters": [ + { + "type": "object", + "name": "details", + "properties": { + "pixelSize": { + "type": "integer", + "description": "The font size in pixels." + }, + "levelOfControl": { + "$ref": "LevelOfControl", + "description": "The level of control this extension has over the setting." + } + } + } + ] + }, + { + "name": "onDefaultCharacterSetChanged", + "description": "Fired when the default character set setting changes.", + "parameters": [ + { + "type": "object", + "name": "details", + "properties": { + "charset": { + "type": "string", + "description": "The character set." + }, + "levelOfControl": { + "$ref": "LevelOfControl", + "description": "The level of control this extension has over the setting." + } + } + } + ] + } + ] + } +] |