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

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: added comments/style 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..c50e6d8fe25f9e300670d7561988c86e1ab91d8d 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -255,7 +255,7 @@ class CCGenerator(object):
c.Sblock('if (%s != %s)' %
(prop.unix_name,
self._cpp_type_generator.GetEnumNoneValue(prop)))
- else:
+ elif not prop.type_ == PropertyType.CHOICES:
not at google - send to devlin 2012/07/18 01:21:35 Prefer to structure this like if prop.type_ == Pr
chebert 2012/07/19 00:56:01 Done.
c.Sblock('if (%s.get())' % prop.unix_name)
c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
prop.name,
@@ -276,7 +276,7 @@ class CCGenerator(object):
# Params::Populate function
if function.params:
c.Concat(self._GeneratePropertyFunctions(cpp_namespace + '::Params',
- function.params))
+ function.params, from_function=True))
not at google - send to devlin 2012/07/18 01:21:35 Like I said in the header, use from_client.
chebert 2012/07/19 00:56:01 Done.
(c.Append('%(cpp_namespace)s::Params::Params() {}')
.Append('%(cpp_namespace)s::Params::~Params() {}')
.Append()
@@ -293,6 +293,39 @@ class CCGenerator(object):
return c
+ def _GenerateGetChoiceValue(self, cpp_namespace, prop):
not at google - send to devlin 2012/07/18 01:21:35 nit: define below the place that it's used.
chebert 2012/07/19 00:56:01 Done.
+ """Generates GetChoiceValue() that returns a |scoped_ptr<base::Value>|
not at google - send to devlin 2012/07/18 01:21:35 usually |foo| is used for variables/parameters, no
chebert 2012/07/19 00:56:01 Done.
+ representing the choice value.
+ """
+ c = Code()
+ (c.Sblock('scoped_ptr<base::Value> '
+ '%(cpp_namespace)s::GetChoiceValue(%(arg)s) const {')
+ .Sblock('switch(%s) {' % prop.unix_name)
+ )
+ if prop.optional:
+ (c.Sblock('case %s: {' %
+ (self._cpp_type_generator.GetEnumNoneValue(prop)))
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
not at google - send to devlin 2012/07/18 01:21:35 The {} in each of these case statements is unneces
chebert 2012/07/19 00:56:01 Done.
+ )
+ for choice in self._cpp_type_generator.ExpandParams([prop]):
+ (c.Sblock('case %s: {' %
+ self._cpp_type_generator.GetEnumValue(prop, choice.type_.name))
+ .Append('return scoped_ptr<base::Value>(%s);' %
not at google - send to devlin 2012/07/18 01:21:35 can do "make_scoped_ptr(%s)" here
chebert 2012/07/19 00:56:01 Done.
+ self._CreateValueFromProperty(choice, choice.unix_name))
+ .Eblock('}')
+ )
+ (c.Eblock('}')
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
not at google - send to devlin 2012/07/18 01:21:35 also add a blank line
chebert 2012/07/19 00:56:01 Done.
+ .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.
@@ -337,13 +370,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:
@@ -612,9 +640,11 @@ class CCGenerator(object):
.Append(' return %(failure_value)s;')
)
- def _GeneratePropertyFunctions(self, param_namespace, params):
+ def _GeneratePropertyFunctions(self, param_namespace, params,
+ from_function=False):
"""Generate the functions for structures generated by a property such as
CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects.
+ If from_function is True if these properties belong to an API Function.
"""
c = Code()
for param in params:
@@ -629,6 +659,8 @@ class CCGenerator(object):
elif param.type_ == PropertyType.CHOICES:
c.Concat(self._GeneratePropertyFunctions(
param_namespace, param.choices.values()))
+ if not from_function:
+ c.Concat(self._GenerateGetChoiceValue(param_namespace, param))
elif param.type_ == PropertyType.ENUM:
c.Concat(self._GenerateCreateEnumValue(param_namespace, param))
c.Append()
@@ -647,7 +679,8 @@ class CCGenerator(object):
c = Code()
params = callback.params
expanded_params = self._cpp_type_generator.ExpandParams(params)
- c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))
+ c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params,
+ from_function=True))
param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
for param_list in param_lists:
« 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