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

Unified Diff: tools/json_schema_compiler/cc_generator.py

Issue 276603003: Support converting referenced enum array into string. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 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
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 b6dfb6f22403584bf19449dea3055c9f4c205d37..a9e9f9ec9d8d788e62e6bbffc999badcaa781500 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -366,12 +366,12 @@ class _Generator(object):
# ANY is a base::Value which is abstract and cannot be a direct member, so
# it will always be a pointer.
is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY
- c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
+ c.Cblock(self._CreateValueFromType(
+ 'value->SetWithoutPathExpansion("%s", %%s);' % prop.name,
prop.name,
- self._CreateValueFromType(cpp_namespace,
- prop.type_,
- prop_var,
- is_ptr=is_ptr)))
+ prop.type_,
+ prop_var,
+ is_ptr=is_ptr))
if prop.optional:
c.Eblock('}')
@@ -390,11 +390,11 @@ class _Generator(object):
cpp_util.PadForGenerics(cpp_type))
.Append(' additional_properties.begin();')
.Append(' it != additional_properties.end(); ++it) {')
- .Append('value->SetWithoutPathExpansion(it->first, %s);' %
- self._CreateValueFromType(
- cpp_namespace,
- type_.additional_properties,
- '%sit->second' % ('*' if needs_unwrap else '')))
+ .Cblock(self._CreateValueFromType(
+ 'value->SetWithoutPathExpansion(it->first, %s);',
+ type_.additional_properties.name,
+ type_.additional_properties,
+ '%sit->second' % ('*' if needs_unwrap else '')))
.Eblock('}')
)
@@ -414,10 +414,10 @@ class _Generator(object):
(c.Sblock('if (%s) {' % choice_var)
.Append('DCHECK(!result) << "Cannot set multiple choices for %s";' %
type_.unix_name)
- .Append('result.reset(%s);' % self._CreateValueFromType(
- cpp_namespace,
- choice,
- '*%s' % choice_var))
+ .Cblock(self._CreateValueFromType('result.reset(%s);',
+ choice.name,
+ choice,
+ '*%s' % choice_var))
.Eblock('}')
)
(c.Append('DCHECK(result) << "Must set at least one choice for %s";' %
@@ -474,7 +474,7 @@ class _Generator(object):
)
return c
- def _CreateValueFromType(self, cpp_namespace, type_, var, is_ptr=False):
+ def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False):
"""Creates a base::Value given a type. Generated code passes ownership
to caller.
@@ -482,6 +482,40 @@ class _Generator(object):
E.g for std::string, generate new base::StringValue(var)
"""
+ c = Code()
+ underlying_type = self._type_helper.FollowRef(type_)
+ if underlying_type.property_type == PropertyType.ARRAY:
+ # Enums are treated specially because C++ templating thinks that they're
+ # ints, but really they're strings.
+ underlying_item_type = self._type_helper.FollowRef(
not at google - send to devlin 2014/05/14 22:52:45 just |item_type| will be fine.
+ underlying_type.item_type)
+ if underlying_item_type.property_type == PropertyType.ENUM:
+ if is_ptr:
+ vardot = '(%s)->' % var
+ else:
+ vardot = '(%s).' % var
not at google - send to devlin 2014/05/14 22:52:45 how about just: vardot = '(%s)%s' % (var, '->' if
+
+ maybe_namespace = ''
+ if type_.item_type.property_type == PropertyType.REF:
+ maybe_namespace = '%s::' % underlying_item_type.namespace.unix_name
+
+ enum_list_var = '%s_list' % prop_name
not at google - send to devlin 2014/05/14 22:52:45 try to scope variable declarations inside braces.
wjywbs 2014/05/16 00:47:37 Do you mean something like this: enum_array_dict =
not at google - send to devlin 2014/05/16 17:13:47 I meant the C++ generated code. The python is fine
not at google - send to devlin 2014/05/16 17:36:34 Because i'm worried that somewhere up in the stack
wjywbs 2014/05/16 19:12:23 Done, but this makes the python code here more com
+ (c.Append('std::vector<std::string> %s;' % enum_list_var)
+ .Append('for (typename std::vector<%s>::const_iterator '
+ 'it = %sbegin();'
+ % (self._type_helper.GetCppType(underlying_item_type), vardot))
+ .Sblock(' it != %send(); ++it) {' % vardot)
+ .Append('%s.push_back(%sToString(*it));' % (enum_list_var,
+ maybe_namespace))
not at google - send to devlin 2014/05/14 22:52:45 the reason why this logic was implemented in C++ b
not at google - send to devlin 2014/05/16 17:36:34 When I said "but note that" I mean add a comment f
wjywbs 2014/05/16 19:12:23 I added the comment above in the paragraph of "Enu
+ .Eblock('}'))
+
+ is_ptr = False
+ var = enum_list_var
not at google - send to devlin 2014/05/14 22:52:45 needs comments around here explaining what is goin
+
+ c.Append(code % self._CreateValueFromTypeInternal(type_, var, is_ptr))
not at google - send to devlin 2014/05/14 22:52:45 why "internal"? this separation seems mostly arbit
+ return c
+
+ def _CreateValueFromTypeInternal(self, type_, var, is_ptr):
underlying_type = self._type_helper.FollowRef(type_)
if (underlying_type.property_type == PropertyType.CHOICES or
underlying_type.property_type == PropertyType.OBJECT):
@@ -509,8 +543,6 @@ class _Generator(object):
(vardot, vardot))
elif underlying_type.property_type == PropertyType.ARRAY:
return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
- cpp_namespace,
- underlying_type,
var,
is_ptr)
elif underlying_type.property_type.is_fundamental:
@@ -961,10 +993,10 @@ class _Generator(object):
for param in params:
declaration_list.append(cpp_util.GetParameterDeclaration(
param, self._type_helper.GetCppType(param.type_)))
- c.Append('create_results->Append(%s);' % self._CreateValueFromType(
- cpp_namespace,
- param.type_,
- param.unix_name))
+ c.Cblock(self._CreateValueFromType('create_results->Append(%s);',
+ param.name,
+ param.type_,
+ param.unix_name))
c.Append('return create_results.Pass();')
c.Eblock('}')
c.Substitute({
« no previous file with comments | « no previous file | tools/json_schema_compiler/test/arrays.json » ('j') | tools/json_schema_compiler/test/arrays_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698