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

Unified Diff: tools/json_schema_compiler/cc_generator.py

Issue 10790040: JSON Schema Compiler now supports conversion from Choice to base::Value. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Trying a refactor of GetChoiceValue and CreateEnumValue Created 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/json_schema_compiler/h_generator.py » ('j') | tools/json_schema_compiler/h_generator.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/json_schema_compiler/cc_generator.py
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py
index d21e160e6266a8362bb31ef99fab2794fef4e31a..e02a6782d0f79d6d6729d626898c8318ce255b33 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -254,7 +254,11 @@ class CCGenerator(object):
if prop.type_ == PropertyType.ENUM:
c.Sblock('if (%s != %s)' %
(prop.unix_name,
- self._cpp_type_generator.GetEnumNoneValue(prop)))
+ self._cpp_type_generator.GetEnumNoneValue(prop)))
+ elif prop.type_ == PropertyType.CHOICES:
+ c.Sblock('if (%s_type != %s)' %
+ (prop.unix_name,
+ self._cpp_type_generator.GetEnumNoneValue(prop)))
else:
c.Sblock('if (%s.get())' % prop.unix_name)
c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
@@ -293,41 +297,6 @@ class CCGenerator(object):
return c
- def _GenerateCreateEnumValue(self, cpp_namespace, prop):
- """Generates CreateEnumValue() that returns the |base::StringValue|
- representation of an enum.
- """
- c = Code()
- c.Append('// static')
- c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
- '%(arg)s) {')
- c.Sblock('switch (%s) {' % prop.unix_name)
- if prop.optional:
- (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop))
- .Append(' return scoped_ptr<base::Value>();')
- .Append('}')
- )
- for enum_value in prop.enum_values:
- (c.Append('case %s: {' %
- self._cpp_type_generator.GetEnumValue(prop, enum_value))
- .Append(' return scoped_ptr<base::Value>('
- 'base::Value::CreateStringValue("%s"));' %
- enum_value)
- .Append('}')
- )
- (c.Append('default: {')
- .Append(' return scoped_ptr<base::Value>();')
- .Append('}')
- )
- c.Eblock('}')
- c.Eblock('}')
- c.Substitute({
- 'cpp_namespace': cpp_namespace,
- 'arg': cpp_util.GetParameterDeclaration(
- prop, self._cpp_type_generator.GetType(prop))
- })
- return c
-
def _CreateValueFromProperty(self, prop, var):
"""Creates a base::Value given a property. Generated code passes ownership
to caller.
@@ -337,13 +306,8 @@ class CCGenerator(object):
E.g for std::string, generate base::Value::CreateStringValue(var)
"""
if prop.type_ == PropertyType.CHOICES:
- # CHOICES conversion not implemented. If needed, write something to
- # generate a function that returns a scoped_ptr<base::Value> and put it in
- # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue()
- # for reference.
- raise NotImplementedError(
- 'Conversion of CHOICES to base::Value not implemented')
- if self._IsObjectOrObjectRef(prop):
+ return 'GetChoiceValue(%s_type).release()' % var
+ elif self._IsObjectOrObjectRef(prop):
if prop.optional:
return '%s->ToValue().release()' % var
else:
@@ -629,11 +593,83 @@ class CCGenerator(object):
elif param.type_ == PropertyType.CHOICES:
c.Concat(self._GeneratePropertyFunctions(
param_namespace, param.choices.values()))
+ if param.from_client:
+ c.Concat(self._GenerateGetChoiceValue(param_namespace, param))
elif param.type_ == PropertyType.ENUM:
c.Concat(self._GenerateCreateEnumValue(param_namespace, param))
c.Append()
return c
+ def _GenerateGetChoiceValue(self, cpp_namespace, prop):
+ """Generates GetChoiceValue() that returns a scoped_ptr<base::Value>
+ representing the choice value.
+ """
+ c = Code()
+ (c.Sblock('scoped_ptr<base::Value> '
not at google - send to devlin 2012/07/19 23:59:53 where's the static? Should be able to be static li
+ '%(cpp_namespace)s::GetChoiceValue(%(arg)s) const {')
+ .Sblock('switch(%s) {' % prop.unix_name)
+ )
+ if prop.optional:
+ c.Concat(self._GenerateReturnCase(
+ self._cpp_type_generator.GetEnumNoneValue(prop),
+ 'scoped_ptr<base::Value>()'))
+ for choice in self._cpp_type_generator.ExpandParams([prop]):
+ c.Concat(self._GenerateReturnCase(
+ self._cpp_type_generator.GetEnumValue(prop, choice.type_.name),
+ 'make_scoped_ptr<base::Value>(%s)' %
+ self._CreateValueFromProperty(choice, choice.unix_name)))
+ (c.Eblock('}')
+ .Append('NOTREACHED();')
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
+ .Append()
+ .Substitute({
+ 'cpp_namespace': cpp_namespace,
+ 'arg': cpp_util.GetParameterDeclaration(
+ prop, self._cpp_type_generator.GetChoicesEnumType(prop))
+ })
+ )
+ return c
+
+ def _GenerateCreateEnumValue(self, cpp_namespace, prop):
+ """Generates CreateEnumValue() that returns the base::StringValue
+ representation of an enum.
+ """
+ c = Code()
+ c.Append('// static')
+ c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
+ '%(arg)s) {')
+ c.Sblock('switch (%s) {' % prop.unix_name)
+ if prop.optional:
+ c.Concat(self._GenerateReturnCase(
+ self._cpp_type_generator.GetEnumNoneValue(prop),
+ 'scoped_ptr<base::Value>()'))
+ for enum_value in prop.enum_values:
+ c.Concat(self._GenerateReturnCase(
+ self._cpp_type_generator.GetEnumValue(prop, enum_value),
+ 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' %
+ enum_value))
+ (c.Eblock('}')
+ .Append('NOTREACHED();')
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
+ .Substitute({
+ 'cpp_namespace': cpp_namespace,
+ 'arg': cpp_util.GetParameterDeclaration(
+ prop, self._cpp_type_generator.GetType(prop))
+ })
+ )
+ return c
+
+ def _GenerateReturnCase(self, case_value, return_value):
+ """Generates a single return case for a switch block.
not at google - send to devlin 2012/07/19 23:59:53 haha, sure that's cool.
+ """
+ c = Code()
+ (c.Append('case %s:' % case_value)
+ .Append(' return %s;' % return_value)
+ )
+ return c
+
def _GenerateCreateCallbackArguments(self, function_scope, callback):
"""Generate all functions to create Value parameters for a callback.
« no previous file with comments | « no previous file | tools/json_schema_compiler/h_generator.py » ('j') | tools/json_schema_compiler/h_generator.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698