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

Side by Side Diff: tools/json_schema_compiler/cc_generator.py

Issue 10825029: Added JSON schema compiler support for serialized types (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: More changes Created 8 years, 4 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 unified diff | Download patch
« no previous file with comments | « no previous file | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import any_helper 7 import any_helper
8 import cpp_util 8 import cpp_util
9 import model 9 import model
10 import schema_util 10 import schema_util
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 .Append('scoped_ptr<base::DictionaryValue> value(' 246 .Append('scoped_ptr<base::DictionaryValue> value('
247 'new base::DictionaryValue());') 247 'new base::DictionaryValue());')
248 .Append() 248 .Append()
249 ) 249 )
250 for prop in type_.properties.values(): 250 for prop in type_.properties.values():
251 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 251 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
252 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) 252 c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
253 else: 253 else:
254 if prop.optional: 254 if prop.optional:
255 if prop.type_ == PropertyType.ENUM: 255 if prop.type_ == PropertyType.ENUM:
256 c.Sblock('if (%s != %s)' % 256 c.Sblock('if (%s != %s) {' %
257 (prop.unix_name, 257 (prop.unix_name,
258 self._cpp_type_generator.GetEnumNoneValue(prop))) 258 self._cpp_type_generator.GetEnumNoneValue(prop)))
259 elif prop.type_ == PropertyType.CHOICES: 259 elif prop.type_ == PropertyType.CHOICES:
260 c.Sblock('if (%s_type != %s)' % 260 c.Sblock('if (%s_type != %s) {' %
261 (prop.unix_name, 261 (prop.unix_name,
262 self._cpp_type_generator.GetEnumNoneValue(prop))) 262 self._cpp_type_generator.GetEnumNoneValue(prop)))
263 else: 263 else:
264 c.Sblock('if (%s.get())' % prop.unix_name) 264 c.Sblock('if (%s.get()) {' % prop.unix_name)
265 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( 265
266 prop.name, 266 if prop.type_ == prop.compiled_type:
267 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) 267 c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
268 prop.name,
269 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name)))
270 else:
271 conversion_src = 'this->' + prop.unix_name
272 if prop.optional:
273 conversion_src = '*' + conversion_src
274 (c.Append('%s %s;' % (self._cpp_type_generator.GetType(prop),
275 prop.unix_name))
276 .Append(cpp_util.GenerateCompiledTypeToTypeConversion(
277 self._cpp_type_generator.GetReferencedProperty(prop),
278 conversion_src,
279 prop.unix_name) + ';')
280 .Append('value->SetWithoutPathExpansion("%s", %s);' % (
281 prop.unix_name,
282 self._CreateValueFromProperty(prop, prop.unix_name)))
283 )
268 if prop.optional: 284 if prop.optional:
269 c.Eblock(); 285 c.Eblock('}');
270 (c.Append() 286 (c.Append()
271 .Append('return value.Pass();') 287 .Append('return value.Pass();')
272 .Eblock('}') 288 .Eblock('}')
273 ) 289 )
274 return c 290 return c
275 291
276 def _GenerateFunction(self, cpp_namespace, function): 292 def _GenerateFunction(self, cpp_namespace, function):
277 """Generates the definitions for function structs. 293 """Generates the definitions for function structs.
278 """ 294 """
279 c = Code() 295 c = Code()
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 vardot = var + '->' 346 vardot = var + '->'
331 else: 347 else:
332 vardot = var + '.' 348 vardot = var + '.'
333 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % 349 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' %
334 (vardot, vardot)) 350 (vardot, vardot))
335 elif self._IsArrayOrArrayRef(prop): 351 elif self._IsArrayOrArrayRef(prop):
336 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( 352 return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
337 self._cpp_type_generator.GetReferencedProperty(prop), var, 353 self._cpp_type_generator.GetReferencedProperty(prop), var,
338 prop.optional) 354 prop.optional)
339 elif self._IsFundamentalOrFundamentalRef(prop): 355 elif self._IsFundamentalOrFundamentalRef(prop):
340 if prop.optional: 356 # If prop.type != prop.compiled_type, then no asterisk is necessary
357 # because the target is a local variable and not a dereferenced scoped
358 # pointer. The asterisk is instead prepended to conversion_src around line
359 # 273.
360 if prop.optional and prop.type_ == prop.compiled_type:
341 var = '*' + var 361 var = '*' + var
342 prop = self._cpp_type_generator.GetReferencedProperty(prop); 362 prop = self._cpp_type_generator.GetReferencedProperty(prop);
343 return { 363 return {
344 PropertyType.STRING: 'base::Value::CreateStringValue(%s)', 364 PropertyType.STRING: 'base::Value::CreateStringValue(%s)',
345 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)', 365 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)',
346 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)', 366 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)',
347 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)', 367 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)',
348 }[prop.type_] % var 368 }[prop.type_] % var
349 else: 369 else:
350 raise NotImplementedError('Conversion of %s to base::Value not ' 370 raise NotImplementedError('Conversion of %s to base::Value not '
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 461
442 if self._IsFundamentalOrFundamentalRef(prop): 462 if self._IsFundamentalOrFundamentalRef(prop):
443 if prop.optional: 463 if prop.optional:
444 (c.Append('%(ctype)s temp;') 464 (c.Append('%(ctype)s temp;')
445 .Append('if (!%s)' % 465 .Append('if (!%s)' %
446 cpp_util.GetAsFundamentalValue( 466 cpp_util.GetAsFundamentalValue(
447 self._cpp_type_generator.GetReferencedProperty(prop), 467 self._cpp_type_generator.GetReferencedProperty(prop),
448 value_var, 468 value_var,
449 '&temp')) 469 '&temp'))
450 .Append(' return %(failure_value)s;') 470 .Append(' return %(failure_value)s;')
451 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
452 ) 471 )
472 if prop.type_ != prop.compiled_type:
473 (c.Append('%(compiled_ctype)s temp2;')
474 .Append('if (!%s)' %
475 cpp_util.GenerateTypeToCompiledTypeConversion(
476 self._cpp_type_generator.GetReferencedProperty(prop),
477 'temp',
478 'temp2'))
479 .Append(' return %(failure_value)s;')
480 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
481 )
482 else:
483 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
484
453 else: 485 else:
486 if prop.type_ == prop.compiled_type:
487 assignment_target = '&%s->%s' % (dst, prop.unix_name)
488 else:
489 c.Append('%(ctype)s temp;')
490 assignment_target = '&temp'
454 (c.Append('if (!%s)' % 491 (c.Append('if (!%s)' %
455 cpp_util.GetAsFundamentalValue( 492 cpp_util.GetAsFundamentalValue(
456 self._cpp_type_generator.GetReferencedProperty(prop), 493 self._cpp_type_generator.GetReferencedProperty(prop),
457 value_var, 494 value_var,
458 '&%s->%s' % (dst, prop.unix_name))) 495 assignment_target))
459 .Append(' return %(failure_value)s;') 496 .Append(' return %(failure_value)s;')
460 ) 497 )
498 if prop.type_ != prop.compiled_type:
499 (c.Append('if (!%s)' %
500 cpp_util.GenerateTypeToCompiledTypeConversion(
501 self._cpp_type_generator.GetReferencedProperty(prop),
502 'temp',
503 '%s->%s' % (dst, prop.unix_name)))
504 .Append(' return %(failure_value)s;')
505 )
506
461 elif self._IsObjectOrObjectRef(prop): 507 elif self._IsObjectOrObjectRef(prop):
462 if prop.optional: 508 if prop.optional:
463 (c.Append('const base::DictionaryValue* dictionary = NULL;') 509 (c.Append('const base::DictionaryValue* dictionary = NULL;')
464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 510 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
465 .Append(' return %(failure_value)s;') 511 .Append(' return %(failure_value)s;')
466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') 512 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') 513 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
468 .Append(' return %(failure_value)s;') 514 .Append(' return %(failure_value)s;')
469 .Append('%(dst)s->%(name)s = temp.Pass();') 515 .Append('%(dst)s->%(name)s = temp.Pass();')
470 ) 516 )
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 raise NotImplementedError(prop.type_) 585 raise NotImplementedError(prop.type_)
540 c.Eblock('}') 586 c.Eblock('}')
541 sub = { 587 sub = {
542 'value_var': value_var, 588 'value_var': value_var,
543 'name': prop.unix_name, 589 'name': prop.unix_name,
544 'dst': dst, 590 'dst': dst,
545 'failure_value': failure_value, 591 'failure_value': failure_value,
546 } 592 }
547 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): 593 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY):
548 sub['ctype'] = self._cpp_type_generator.GetType(prop) 594 sub['ctype'] = self._cpp_type_generator.GetType(prop)
595 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop)
549 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator 596 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator
550 .GetReferencedProperty(prop).type_) 597 .GetReferencedProperty(prop).type_)
551 c.Substitute(sub) 598 c.Substitute(sub)
552 return c 599 return c
553 600
554 def _GenerateListValueToEnumArrayConversion(self, c, prop): 601 def _GenerateListValueToEnumArrayConversion(self, c, prop):
555 """Appends code that converts a ListValue of string contstants to 602 """Appends code that converts a ListValue of string contstants to
556 an array of enums in dst. 603 an array of enums in dst.
557 Leaves dst, name, and failure_value unsubstituted. 604 Leaves dst, name, and failure_value unsubstituted.
558 605
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 'Create(%(declaration_list)s) {') 761 'Create(%(declaration_list)s) {')
715 .Append('scoped_ptr<base::ListValue> create_results(' 762 .Append('scoped_ptr<base::ListValue> create_results('
716 'new base::ListValue());') 763 'new base::ListValue());')
717 ) 764 )
718 declaration_list = [] 765 declaration_list = []
719 for param in param_list: 766 for param in param_list:
720 # We treat this argument as 'required' to avoid wrapping it in a 767 # We treat this argument as 'required' to avoid wrapping it in a
721 # scoped_ptr if it's optional. 768 # scoped_ptr if it's optional.
722 param_copy = param.Copy() 769 param_copy = param.Copy()
723 param_copy.optional = False 770 param_copy.optional = False
771 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
772 param_copy, self._cpp_type_generator.GetCompiledType(param_copy)))
773 param_name = param_copy.unix_name
774 if param_copy.type_ != param_copy.compiled_type:
775 param_name = 'temp_' + param_name
776 (c.Append('%s %s;' % (self._cpp_type_generator.GetType(param_copy),
777 param_name))
778 .Append(cpp_util.GenerateCompiledTypeToTypeConversion(
779 param_copy,
780 param_copy.unix_name,
781 param_name) + ';')
782 )
724 c.Append('create_results->Append(%s);' % 783 c.Append('create_results->Append(%s);' %
725 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) 784 self._CreateValueFromProperty(param_copy, param_name))
726 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
727 param_copy, self._cpp_type_generator.GetType(param_copy)))
728 785
729 c.Append('return create_results.Pass();') 786 c.Append('return create_results.Pass();')
730 c.Eblock('}') 787 c.Eblock('}')
731 if generate_to_json: 788 if generate_to_json:
732 c.Append() 789 c.Append()
733 (c.Sblock('std::string %(function_scope)s::' 790 (c.Sblock('std::string %(function_scope)s::'
734 'ToJson(%(declaration_list)s) {') 791 'ToJson(%(declaration_list)s) {')
735 .Append('scoped_ptr<base::ListValue> create_results = ' 792 .Append('scoped_ptr<base::ListValue> create_results = '
736 '%(function_scope)s::Create(%(param_list)s);') 793 '%(function_scope)s::Create(%(param_list)s);')
737 .Append('std::string json;') 794 .Append('std::string json;')
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 """ 835 """
779 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 836 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
780 PropertyType.ARRAY) 837 PropertyType.ARRAY)
781 838
782 def _IsFundamentalOrFundamentalRef(self, prop): 839 def _IsFundamentalOrFundamentalRef(self, prop):
783 """Determines if this property is a Fundamental type or is a ref to a 840 """Determines if this property is a Fundamental type or is a ref to a
784 Fundamental type. 841 Fundamental type.
785 """ 842 """
786 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 843 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
787 is_fundamental) 844 is_fundamental)
OLDNEW
« no previous file with comments | « no previous file | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698