Chromium Code Reviews| 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 bc552933bf02eea63fc610a735b0b727f2afaae7..cd84c478cddb50628c8867ef09691a518abc2659 100644 |
| --- a/tools/json_schema_compiler/cc_generator.py |
| +++ b/tools/json_schema_compiler/cc_generator.py |
| @@ -13,8 +13,9 @@ import util_cc_helper |
| class CCGenerator(object): |
| """A .cc generator for a namespace. |
| """ |
| - def __init__(self, namespace, cpp_type_generator): |
| + def __init__(self, namespace, cpp_type_generator, referenced_schemas): |
| self._cpp_type_generator = cpp_type_generator |
| + self._referenced_schemas = referenced_schemas |
| self._namespace = namespace |
| self._target_namespace = ( |
| self._cpp_type_generator.GetCppNamespaceName(self._namespace)) |
| @@ -98,7 +99,7 @@ class CCGenerator(object): |
| function)) |
| .Append() |
| ) |
| - else: |
| + elif type_.type_ == PropertyType.OBJECT: |
| (c.Concat(self._GeneratePropertyFunctions( |
| cpp_namespace, type_.properties.values())) |
| .Sblock('%(namespace)s::%(classname)s()') |
| @@ -321,7 +322,7 @@ class CCGenerator(object): |
| # for reference. |
| raise NotImplementedError( |
| 'Conversion of CHOICES to Value not implemented') |
| - if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): |
| + if self._IsObjectOrObjectRef(prop): |
| if prop.optional: |
| return '%s->ToValue().release()' % var |
| else: |
| @@ -332,9 +333,9 @@ class CCGenerator(object): |
| return '%s.DeepCopy()' % var |
| elif prop.type_ == PropertyType.ENUM: |
| return 'CreateEnumValue(%s).release()' % var |
| - elif prop.type_ == PropertyType.ARRAY: |
| + elif self._IsArrayOrArrayRef(prop): |
| return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| - prop, var) |
| + self._GetRef(prop), var, prop.optional) |
| elif prop.type_.is_fundamental: |
| if prop.optional: |
| var = '*' + var |
| @@ -455,7 +456,7 @@ class CCGenerator(object): |
| prop, value_var, '&%s->%s' % (dst, prop.unix_name))) |
| .Append('return %(failure_value)s;') |
| ) |
| - elif prop.type_ in (PropertyType.OBJECT, PropertyType.REF): |
| + elif self._IsObjectOrObjectRef(prop): |
| if prop.optional: |
| (c.Append('DictionaryValue* dictionary = NULL;') |
| .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| @@ -477,20 +478,22 @@ class CCGenerator(object): |
| if prop.optional: |
| c.Append('%(dst)s->%(name)s.reset(new Any());') |
| c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
| - elif prop.type_ == PropertyType.ARRAY: |
| + elif self._IsArrayOrArrayRef(prop): |
| # util_cc_helper deals with optional and required arrays |
| (c.Append('ListValue* list = NULL;') |
| .Append('if (!%(value_var)s->GetAsList(&list))') |
| .Append(' return %(failure_value)s;') |
| .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| - prop, 'list', dst + '->' + prop.unix_name)) |
| + self._GetRef(prop), 'list', dst + '->' + prop.unix_name, |
| + prop.optional)) |
| .Append(' return %(failure_value)s;') |
| ) |
| elif prop.type_ == PropertyType.CHOICES: |
| type_var = '%(dst)s->%(name)s_type' |
| c.Sblock('switch (%(value_var)s->GetType()) {') |
| for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): |
| - (c.Sblock('case %s: {' % cpp_util.GetValueType(choice)) |
| + (c.Sblock('case %s: {' % cpp_util.GetValueType( |
| + self._GetRef(choice).type_)) |
| .Concat(self._GeneratePopulatePropertyFromValue( |
| choice, value_var, dst, failure_value, check_type=False)) |
| .Append('%s = %s;' % |
| @@ -532,7 +535,7 @@ class CCGenerator(object): |
| } |
| if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): |
| sub['ctype'] = self._cpp_type_generator.GetType(prop) |
| - sub['value_type'] = cpp_util.GetValueType(prop) |
| + sub['value_type'] = cpp_util.GetValueType(self._GetRef(prop).type_) |
| c.Substitute(sub) |
| return c |
| @@ -616,3 +619,27 @@ class CCGenerator(object): |
| prop_name, |
| self._cpp_type_generator.GetEnumNoneValue(prop))) |
| return c |
| + |
| + def _IsObjectOrObjectRef(self, prop): |
| + """Determines if this property is an Object or is a ref to an Object. |
| + """ |
| + return self._GetRef(prop).type_ == PropertyType.OBJECT |
| + |
| + def _IsArrayOrArrayRef(self, prop): |
| + """Determines if this property is an Array or is a ref to an Array. |
| + """ |
| + return self._GetRef(prop).type_ == PropertyType.ARRAY |
| + |
| + def _GetRef(self, prop): |
|
Yoyo Zhou
2012/04/10 19:26:48
I'm not sure this name is clear enoguh. How about
cduvall
2012/04/12 00:41:23
This function is actually returning the whole prop
|
| + """Returns the item a $ref is referring to. |
|
Yoyo Zhou
2012/04/10 19:26:48
s/item/type/, and add a note on what it returns if
cduvall
2012/04/12 00:41:23
Done. See previous comment.
|
| + """ |
| + if prop.type_ != PropertyType.REF: |
| + return prop |
| + if prop.ref_type in self._namespace.types.keys(): |
| + return self._namespace.types[prop.ref_type] |
| + for namespace in self._referenced_schemas: |
| + qualified_name = namespace + '.' + prop.ref_type |
| + if qualified_name in self._cpp_type_generator._type_namespaces.keys(): |
| + return (self._cpp_type_generator._type_namespaces[qualified_name] |
|
Yoyo Zhou
2012/04/10 19:26:48
The _ is supposed to prefix private members, so yo
cduvall
2012/04/12 00:41:23
Done.
|
| + .types[prop.ref_type]) |
| + return prop |