summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/h_generator.py
diff options
context:
space:
mode:
authorcalamity@chromium.org <calamity@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-02 15:05:27 +0000
committercalamity@chromium.org <calamity@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-02 15:05:27 +0000
commitfeba21e3dd6f25b0927817bdad749e47490e2798 (patch)
treeda16581c8c1658b385d6230423cc726df04cf4b7 /tools/json_schema_compiler/h_generator.py
parent7ae87817552c6e8608f62304354fd88cb921a31c (diff)
downloadchromium_src-feba21e3dd6f25b0927817bdad749e47490e2798.zip
chromium_src-feba21e3dd6f25b0927817bdad749e47490e2798.tar.gz
chromium_src-feba21e3dd6f25b0927817bdad749e47490e2798.tar.bz2
json_schema_compiler: any, additionalProperties, functions on types
Add support and tests for more json types. Also fixed a number of API jsons. BUG= TEST= Review URL: http://codereview.chromium.org/9491002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124643 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/h_generator.py')
-rw-r--r--tools/json_schema_compiler/h_generator.py76
1 files changed, 50 insertions, 26 deletions
diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py
index 3af7ccf..3c97bc6 100644
--- a/tools/json_schema_compiler/h_generator.py
+++ b/tools/json_schema_compiler/h_generator.py
@@ -5,6 +5,7 @@
from model import PropertyType
import code
import cpp_util
+import model
import os
class HGenerator(object):
@@ -38,6 +39,7 @@ class HGenerator(object):
.Append('#include "base/memory/linked_ptr.h"')
.Append('#include "base/memory/scoped_ptr.h"')
.Append('#include "base/values.h"')
+ .Append('#include "tools/json_schema_compiler/any.h"')
.Append()
)
@@ -76,8 +78,7 @@ class HGenerator(object):
(c.Concat(self._GenerateFunction(function))
.Append()
)
- (c.Append()
- .Concat(self._cpp_type_generator.GetNamespaceEnd())
+ (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
.Concat(self._cpp_type_generator.GetRootNamespaceEnd())
.Append()
.Append('#endif // %s' % ifndef_name)
@@ -109,6 +110,7 @@ class HGenerator(object):
if prop.type_ == PropertyType.CHOICES:
enum_name = self._cpp_type_generator.GetChoicesEnumType(prop)
c.Append('%s %s_type;' % (enum_name, prop.unix_name))
+ c.Append()
for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props):
if prop.description:
c.Comment(prop.description)
@@ -123,33 +125,50 @@ class HGenerator(object):
"""
classname = cpp_util.Classname(type_.name)
c = code.Code()
- if type_.description:
- c.Comment(type_.description)
- (c.Sblock('struct %(classname)s {')
- .Append('~%(classname)s();')
- .Append('%(classname)s();')
- .Append()
- .Concat(self._GeneratePropertyStructures(type_.properties.values()))
- .Concat(self._GenerateFields(type_.properties.values()))
- )
- if type_.from_json:
- (c.Comment('Populates a %s object from a Value. Returns'
- ' whether |out| was successfully populated.' % classname)
- .Append('static bool Populate(const Value& value, %(classname)s* out);')
- .Append()
+
+ if type_.functions:
+ # Types with functions are not instantiable in C++ because they are
+ # handled in pure Javascript and hence have no properties or
+ # additionalProperties.
+ if type_.properties:
+ raise NotImplementedError('\n'.join(model.GetModelHierarchy(type_)) +
+ '\nCannot generate both functions and properties on a type')
+ c.Sblock('namespace %(classname)s {')
+ for function in type_.functions.values():
+ (c.Concat(self._GenerateFunction(function))
+ .Append()
+ )
+ c.Eblock('}')
+ else:
+ if type_.description:
+ c.Comment(type_.description)
+ (c.Sblock('struct %(classname)s {')
+ .Append('~%(classname)s();')
+ .Append('%(classname)s();')
+ .Append()
+ .Concat(self._GeneratePropertyStructures(type_.properties.values()))
+ .Concat(self._GenerateFields(type_.properties.values()))
)
+ if type_.from_json:
+ (c.Comment('Populates a %s object from a Value. Returns'
+ ' whether |out| was successfully populated.' % classname)
+ .Append(
+ 'static bool Populate(const Value& value, %(classname)s* out);')
+ .Append()
+ )
+
+ if type_.from_client:
+ (c.Comment('Returns a new DictionaryValue representing the'
+ ' serialized form of this %s object. Passes '
+ 'ownership to caller.' % classname)
+ .Append('scoped_ptr<DictionaryValue> ToValue() const;')
+ )
- if type_.from_client:
- (c.Comment('Returns a new DictionaryValue representing the'
- ' serialized form of this %s object. Passes '
- 'ownership to caller.' % classname)
- .Append('scoped_ptr<DictionaryValue> ToValue() const;')
+ (c.Eblock()
+ .Sblock(' private:')
+ .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
+ .Eblock('};')
)
- (c.Eblock()
- .Sblock(' private:')
- .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
- .Eblock('};')
- )
c.Substitute({'classname': classname})
return c
@@ -205,6 +224,7 @@ class HGenerator(object):
self._cpp_type_generator.GetChoicesEnumType(prop),
prop,
[choice.type_.name for choice in prop.choices.values()]))
+ c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
elif prop.type_ == PropertyType.ENUM:
enum_name = self._cpp_type_generator.GetType(prop)
c.Concat(self._GenerateEnumDeclaration(
@@ -234,6 +254,10 @@ class HGenerator(object):
for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
if param.description:
c.Comment(param.description)
+ if param.type_ == PropertyType.ANY:
+ c.Comment("Value* Result::Create(Value*) not generated "
+ "because it's redundant.")
+ continue
c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration(
param, self._cpp_type_generator.GetType(param)))
c.Eblock('};')