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

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: 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.optional and prop.type_ == prop.compiled_type:
341 var = '*' + var 357 var = '*' + var
342 prop = self._cpp_type_generator.GetReferencedProperty(prop); 358 prop = self._cpp_type_generator.GetReferencedProperty(prop);
343 return { 359 return {
344 PropertyType.STRING: 'base::Value::CreateStringValue(%s)', 360 PropertyType.STRING: 'base::Value::CreateStringValue(%s)',
345 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)', 361 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)',
346 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)', 362 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)',
347 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)', 363 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)',
348 }[prop.type_] % var 364 }[prop.type_] % var
349 else: 365 else:
350 raise NotImplementedError('Conversion of %s to base::Value not ' 366 raise NotImplementedError('Conversion of %s to base::Value not '
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 457
442 if self._IsFundamentalOrFundamentalRef(prop): 458 if self._IsFundamentalOrFundamentalRef(prop):
443 if prop.optional: 459 if prop.optional:
444 (c.Append('%(ctype)s temp;') 460 (c.Append('%(ctype)s temp;')
445 .Append('if (!%s)' % 461 .Append('if (!%s)' %
446 cpp_util.GetAsFundamentalValue( 462 cpp_util.GetAsFundamentalValue(
447 self._cpp_type_generator.GetReferencedProperty(prop), 463 self._cpp_type_generator.GetReferencedProperty(prop),
448 value_var, 464 value_var,
449 '&temp')) 465 '&temp'))
450 .Append(' return %(failure_value)s;') 466 .Append(' return %(failure_value)s;')
451 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
452 ) 467 )
468 if prop.type_ != prop.compiled_type:
469 (c.Append('%(compiled_ctype)s temp2;')
470 .Append('if (!%s)' %
mitchellwrosen 2012/07/31 00:10:37 Ugh, mis-aligned indents here. I'll fix it in a se
471 cpp_util.GenerateTypeToCompiledTypeConversion(
472 self._cpp_type_generator.GetReferencedProperty(prop),
473 'temp',
474 'temp2'))
475 .Append(' return %(failure_value)s;')
476 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
477 )
478 else:
479 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
480
453 else: 481 else:
482 if prop.type_ == prop.compiled_type:
483 assignment_target = '&%s->%s' % (dst, prop.unix_name)
484 else:
485 c.Append('%(ctype)s temp;')
486 assignment_target = '&temp'
454 (c.Append('if (!%s)' % 487 (c.Append('if (!%s)' %
455 cpp_util.GetAsFundamentalValue( 488 cpp_util.GetAsFundamentalValue(
456 self._cpp_type_generator.GetReferencedProperty(prop), 489 self._cpp_type_generator.GetReferencedProperty(prop),
457 value_var, 490 value_var,
458 '&%s->%s' % (dst, prop.unix_name))) 491 assignment_target))
459 .Append(' return %(failure_value)s;') 492 .Append(' return %(failure_value)s;')
460 ) 493 )
494 if prop.type_ != prop.compiled_type:
495 (c.Append('if (!%s)' %
496 cpp_util.GenerateTypeToCompiledTypeConversion(
497 self._cpp_type_generator.GetReferencedProperty(prop),
498 'temp',
499 '%s->%s' % (dst, prop.unix_name)))
500 .Append(' return %(failure_value)s;')
501 )
502
461 elif self._IsObjectOrObjectRef(prop): 503 elif self._IsObjectOrObjectRef(prop):
462 if prop.optional: 504 if prop.optional:
463 (c.Append('const base::DictionaryValue* dictionary = NULL;') 505 (c.Append('const base::DictionaryValue* dictionary = NULL;')
464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 506 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
465 .Append(' return %(failure_value)s;') 507 .Append(' return %(failure_value)s;')
466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') 508 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') 509 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
468 .Append(' return %(failure_value)s;') 510 .Append(' return %(failure_value)s;')
469 .Append('%(dst)s->%(name)s = temp.Pass();') 511 .Append('%(dst)s->%(name)s = temp.Pass();')
470 ) 512 )
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),') 577 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
536 .Append(' binary_value->GetSize());') 578 .Append(' binary_value->GetSize());')
537 ) 579 )
538 else: 580 else:
539 raise NotImplementedError(prop.type_) 581 raise NotImplementedError(prop.type_)
540 c.Eblock('}') 582 c.Eblock('}')
541 sub = { 583 sub = {
542 'value_var': value_var, 584 'value_var': value_var,
543 'name': prop.unix_name, 585 'name': prop.unix_name,
544 'dst': dst, 586 'dst': dst,
545 'failure_value': failure_value, 587 'failure_value': failure_value
546 } 588 }
547 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY): 589 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY):
548 sub['ctype'] = self._cpp_type_generator.GetType(prop) 590 sub['ctype'] = self._cpp_type_generator.GetType(prop)
591 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop)
549 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator 592 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator
550 .GetReferencedProperty(prop).type_) 593 .GetReferencedProperty(prop).type_)
551 c.Substitute(sub) 594 c.Substitute(sub)
552 return c 595 return c
553 596
554 def _GenerateListValueToEnumArrayConversion(self, c, prop): 597 def _GenerateListValueToEnumArrayConversion(self, c, prop):
555 """Appends code that converts a ListValue of string contstants to 598 """Appends code that converts a ListValue of string contstants to
556 an array of enums in dst. 599 an array of enums in dst.
557 Leaves dst, name, and failure_value unsubstituted. 600 Leaves dst, name, and failure_value unsubstituted.
558 601
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 'Create(%(declaration_list)s) {') 757 'Create(%(declaration_list)s) {')
715 .Append('scoped_ptr<base::ListValue> create_results(' 758 .Append('scoped_ptr<base::ListValue> create_results('
716 'new base::ListValue());') 759 'new base::ListValue());')
717 ) 760 )
718 declaration_list = [] 761 declaration_list = []
719 for param in param_list: 762 for param in param_list:
720 # We treat this argument as 'required' to avoid wrapping it in a 763 # We treat this argument as 'required' to avoid wrapping it in a
721 # scoped_ptr if it's optional. 764 # scoped_ptr if it's optional.
722 param_copy = param.Copy() 765 param_copy = param.Copy()
723 param_copy.optional = False 766 param_copy.optional = False
767 declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
768 param_copy, self._cpp_type_generator.GetCompiledType(param_copy)))
769 param_name = param_copy.unix_name
770 if param_copy.type_ != param_copy.compiled_type:
771 param_name = 'temp_' + param_name
772 (c.Append('%s %s;' % (self._cpp_type_generator.GetType(param_copy),
773 param_name))
774 .Append(cpp_util.GenerateCompiledTypeToTypeConversion(
775 param_copy,
776 param_copy.unix_name,
777 param_name) + ';')
778 )
724 c.Append('create_results->Append(%s);' % 779 c.Append('create_results->Append(%s);' %
725 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) 780 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 781
729 c.Append('return create_results.Pass();') 782 c.Append('return create_results.Pass();')
730 c.Eblock('}') 783 c.Eblock('}')
731 if generate_to_json: 784 if generate_to_json:
732 c.Append() 785 c.Append()
733 (c.Sblock('std::string %(function_scope)s::' 786 (c.Sblock('std::string %(function_scope)s::'
734 'ToJson(%(declaration_list)s) {') 787 'ToJson(%(declaration_list)s) {')
735 .Append('scoped_ptr<base::ListValue> create_results = ' 788 .Append('scoped_ptr<base::ListValue> create_results = '
736 '%(function_scope)s::Create(%(param_list)s);') 789 '%(function_scope)s::Create(%(param_list)s);')
737 .Append('std::string json;') 790 .Append('std::string json;')
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 """ 831 """
779 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 832 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
780 PropertyType.ARRAY) 833 PropertyType.ARRAY)
781 834
782 def _IsFundamentalOrFundamentalRef(self, prop): 835 def _IsFundamentalOrFundamentalRef(self, prop):
783 """Determines if this property is a Fundamental type or is a ref to a 836 """Determines if this property is a Fundamental type or is a ref to a
784 Fundamental type. 837 Fundamental type.
785 """ 838 """
786 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 839 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
787 is_fundamental) 840 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