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

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

Issue 10828407: JSON Schema Compiler supports Enums as types. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more testing of optional enums Created 8 years, 3 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
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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 .Append() 120 .Append()
121 ) 121 )
122 if type_.from_json: 122 if type_.from_json:
123 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) 123 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_))
124 .Append() 124 .Append()
125 ) 125 )
126 if type_.from_client: 126 if type_.from_client:
127 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) 127 (c.Concat(self._GenerateTypeToValue(cpp_namespace, type_))
128 .Append() 128 .Append()
129 ) 129 )
130 elif self._IsEnumRef(type_):
131 c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_))
132 c.Append()
130 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) 133 c.Substitute({'classname': classname, 'namespace': cpp_namespace})
131 134
132 return c 135 return c
133 136
134 def _GenerateInitializersAndBody(self, type_): 137 def _GenerateInitializersAndBody(self, type_):
135 items = [] 138 items = []
136 for prop in type_.properties.values(): 139 for prop in type_.properties.values():
137 if prop.optional: 140 if prop.optional:
138 continue 141 continue
139 142
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 cpp_namespace) 248 cpp_namespace)
246 .Append('scoped_ptr<base::DictionaryValue> value(' 249 .Append('scoped_ptr<base::DictionaryValue> value('
247 'new base::DictionaryValue());') 250 'new base::DictionaryValue());')
248 .Append() 251 .Append()
249 ) 252 )
250 for prop in type_.properties.values(): 253 for prop in type_.properties.values():
251 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 254 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
252 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) 255 c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
253 else: 256 else:
254 if prop.optional: 257 if prop.optional:
255 if prop.type_ == PropertyType.ENUM: 258 if self._cpp_type_generator.IsEnumOrEnumRef(prop):
256 c.Sblock('if (%s != %s) {' % 259 c.Sblock('if (%s != %s) {' %
257 (prop.unix_name, 260 (prop.unix_name,
258 self._cpp_type_generator.GetEnumNoneValue(prop))) 261 self._cpp_type_generator.GetEnumNoneValue(prop)))
259 elif prop.type_ == PropertyType.CHOICES: 262 elif prop.type_ == PropertyType.CHOICES:
260 c.Sblock('if (%s_type != %s) {' % 263 c.Sblock('if (%s_type != %s) {' %
261 (prop.unix_name, 264 (prop.unix_name,
262 self._cpp_type_generator.GetEnumNoneValue(prop))) 265 self._cpp_type_generator.GetEnumNoneValue(prop)))
263 else: 266 else:
264 c.Sblock('if (%s.get()) {' % prop.unix_name) 267 c.Sblock('if (%s.get()) {' % prop.unix_name)
265 268
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 elif prop.type_ == PropertyType.ANY: 335 elif prop.type_ == PropertyType.ANY:
333 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) 336 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var)
334 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 337 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
335 return '%s.DeepCopy()' % var 338 return '%s.DeepCopy()' % var
336 elif prop.type_ == PropertyType.FUNCTION: 339 elif prop.type_ == PropertyType.FUNCTION:
337 if prop.optional: 340 if prop.optional:
338 vardot = var + '->' 341 vardot = var + '->'
339 else: 342 else:
340 vardot = var + '.' 343 vardot = var + '.'
341 return '%sDeepCopy()' % vardot 344 return '%sDeepCopy()' % vardot
342 elif prop.type_ == PropertyType.ENUM: 345 elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
343 return 'CreateEnumValue(%s).release()' % var 346 return 'CreateEnumValue(%s).release()' % var
344 elif prop.type_ == PropertyType.BINARY: 347 elif prop.type_ == PropertyType.BINARY:
345 if prop.optional: 348 if prop.optional:
346 vardot = var + '->' 349 vardot = var + '->'
347 else: 350 else:
348 vardot = var + '.' 351 vardot = var + '.'
349 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % 352 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' %
350 (vardot, vardot)) 353 (vardot, vardot))
351 elif self._IsArrayOrArrayRef(prop): 354 elif self._IsArrayOrArrayRef(prop):
352 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( 355 return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 dst: the object with |prop| as a member. 456 dst: the object with |prop| as a member.
454 failure_value: the value to return if |prop| cannot be extracted from 457 failure_value: the value to return if |prop| cannot be extracted from
455 |value_var| 458 |value_var|
456 check_type: if true, will check if |value_var| is the correct 459 check_type: if true, will check if |value_var| is the correct
457 base::Value::Type 460 base::Value::Type
458 """ 461 """
459 c = Code() 462 c = Code()
460 c.Sblock('{') 463 c.Sblock('{')
461 464
462 if self._IsFundamentalOrFundamentalRef(prop): 465 if self._IsFundamentalOrFundamentalRef(prop):
463 if prop.optional: 466 self._GenerateFundamentalOrFundamentalRefPopulate(c, prop, value_var, dst)
464 (c.Append('%(ctype)s temp;') 467 elif self._IsObjectOrObjectRef(prop):
465 .Append('if (!%s)' % 468 self._GenerateObjectOrObjectRefPopulate(c, prop)
466 cpp_util.GetAsFundamentalValue( 469 elif prop.type_ == PropertyType.FUNCTION:
467 self._cpp_type_generator.GetReferencedProperty(prop), 470 self._GenerateFunctionPopulate(c, prop)
468 value_var, 471 elif prop.type_ == PropertyType.ANY:
469 '&temp')) 472 self._GenerateAnyPopulate(c, prop, value_var, dst)
470 .Append(' return %(failure_value)s;') 473 elif self._IsArrayOrArrayRef(prop):
471 ) 474 self._GenerateArrayOrArrayRefPopulate(c, prop, dst)
472 if prop.type_ != prop.compiled_type: 475 elif prop.type_ == PropertyType.CHOICES:
473 (c.Append('%(compiled_ctype)s temp2;') 476 self._GenerateChoicePopulate(c, prop, value_var, dst, failure_value)
474 .Append('if (!%s)' % 477 elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
475 cpp_util.GenerateTypeToCompiledTypeConversion( 478 self._GenerateEnumPopulate(c, prop, value_var)
476 self._cpp_type_generator.GetReferencedProperty(prop), 479 elif prop.type_ == PropertyType.BINARY:
477 'temp', 480 self._GenerateBinaryPopulate(c, prop)
478 'temp2')) 481 else:
479 .Append(' return %(failure_value)s;') 482 raise NotImplementedError(prop.type_)
480 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));') 483 c.Eblock('}')
481 ) 484 sub = {
482 else: 485 'value_var': value_var,
483 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') 486 'name': prop.unix_name,
487 'dst': dst,
488 'failure_value': failure_value,
489 }
490 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY):
491 sub['ctype'] = self._cpp_type_generator.GetType(prop)
492 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop)
493 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator
494 .GetReferencedProperty(prop).type_)
495 c.Substitute(sub)
496 return c
484 497
485 else: 498 def _GenerateFundamentalOrFundamentalRefPopulate(self,
486 if prop.type_ == prop.compiled_type: 499 c,
487 assignment_target = '&%s->%s' % (dst, prop.unix_name) 500 prop,
488 else: 501 value_var,
489 c.Append('%(ctype)s temp;') 502 dst):
490 assignment_target = '&temp' 503 if prop.optional:
491 (c.Append('if (!%s)' % 504 (c.Append('%(ctype)s temp;')
505 .Append('if (!%s)' %
492 cpp_util.GetAsFundamentalValue( 506 cpp_util.GetAsFundamentalValue(
493 self._cpp_type_generator.GetReferencedProperty(prop), 507 self._cpp_type_generator.GetReferencedProperty(prop),
494 value_var, 508 value_var,
495 assignment_target)) 509 '&temp'))
510 .Append(' return %(failure_value)s;')
511 )
512 if prop.type_ != prop.compiled_type:
513 (c.Append('%(compiled_ctype)s temp2;')
514 .Append('if (!%s)' %
515 cpp_util.GenerateTypeToCompiledTypeConversion(
516 self._cpp_type_generator.GetReferencedProperty(prop),
517 'temp',
518 'temp2'))
519 .Append(' return %(failure_value)s;')
520 .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
521 )
522 else:
523 c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
524
525 else:
526 if prop.type_ == prop.compiled_type:
527 assignment_target = '&%s->%s' % (dst, prop.unix_name)
528 else:
529 c.Append('%(ctype)s temp;')
530 assignment_target = '&temp'
531 (c.Append('if (!%s)' %
532 cpp_util.GetAsFundamentalValue(
533 self._cpp_type_generator.GetReferencedProperty(prop),
534 value_var,
535 assignment_target))
536 .Append(' return %(failure_value)s;')
537 )
538 if prop.type_ != prop.compiled_type:
539 (c.Append('if (!%s)' %
540 cpp_util.GenerateTypeToCompiledTypeConversion(
541 self._cpp_type_generator.GetReferencedProperty(prop),
542 'temp',
543 '%s->%s' % (dst, prop.unix_name)))
496 .Append(' return %(failure_value)s;') 544 .Append(' return %(failure_value)s;')
497 ) 545 )
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 546
507 elif self._IsObjectOrObjectRef(prop): 547 def _GenerateObjectOrObjectRefPopulate(self, c, prop):
508 if prop.optional: 548 if prop.optional:
509 (c.Append('const base::DictionaryValue* dictionary = NULL;') 549 (c.Append('const base::DictionaryValue* dictionary = NULL;')
510 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 550 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
511 .Append(' return %(failure_value)s;') 551 .Append(' return %(failure_value)s;')
512 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') 552 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
513 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') 553 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
514 .Append(' return %(failure_value)s;') 554 .Append(' return %(failure_value)s;')
515 .Append('%(dst)s->%(name)s = temp.Pass();') 555 .Append('%(dst)s->%(name)s = temp.Pass();')
516 ) 556 )
517 else: 557 else:
518 (c.Append('const base::DictionaryValue* dictionary = NULL;') 558 (c.Append('const base::DictionaryValue* dictionary = NULL;')
519 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 559 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
520 .Append(' return %(failure_value)s;') 560 .Append(' return %(failure_value)s;')
521 .Append( 561 .Append(
522 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') 562 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
523 .Append(' return %(failure_value)s;') 563 .Append(' return %(failure_value)s;')
524 ) 564 )
525 elif prop.type_ == PropertyType.FUNCTION: 565
526 if prop.optional: 566 def _GenerateFunctionPopulate(self, c, prop):
527 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());') 567 if prop.optional:
528 elif prop.type_ == PropertyType.ANY: 568 c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());')
529 if prop.optional: 569
530 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') 570 def _GenerateAnyPopulate(self, c, prop, value_var, dst):
531 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') 571 if prop.optional:
532 elif self._IsArrayOrArrayRef(prop): 572 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
533 # util_cc_helper deals with optional and required arrays 573 c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
534 (c.Append('const base::ListValue* list = NULL;') 574
535 .Append('if (!%(value_var)s->GetAsList(&list))') 575 def _GenerateArrayOrArrayRefPopulate(self, c, prop, dst):
536 .Append(' return %(failure_value)s;')) 576 # util_cc_helper deals with optional and required arrays
537 if prop.item_type.type_ == PropertyType.ENUM: 577 (c.Append('const base::ListValue* list = NULL;')
538 self._GenerateListValueToEnumArrayConversion(c, prop) 578 .Append('if (!%(value_var)s->GetAsList(&list))')
539 else: 579 .Append(' return %(failure_value)s;'))
540 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( 580 if prop.item_type.type_ == PropertyType.ENUM:
541 self._cpp_type_generator.GetReferencedProperty(prop), 'list', 581 self._GenerateListValueToEnumArrayConversion(c, prop)
542 dst + '->' + prop.unix_name, prop.optional)) 582 else:
543 .Append(' return %(failure_value)s;') 583 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
544 ) 584 self._cpp_type_generator.GetReferencedProperty(prop), 'list',
545 elif prop.type_ == PropertyType.CHOICES: 585 dst + '->' + prop.unix_name, prop.optional))
586 .Append(' return %(failure_value)s;')
587 )
588
589 def _GenerateChoicePopulate(self, c, prop, value_var, dst, failure_value):
546 type_var = '%(dst)s->%(name)s_type' 590 type_var = '%(dst)s->%(name)s_type'
Yoyo Zhou 2012/08/27 23:56:04 nit: indent is 2
chebert 2012/08/28 21:31:14 Done.
547 c.Sblock('switch (%(value_var)s->GetType()) {') 591 c.Sblock('switch (%(value_var)s->GetType()) {')
548 for choice in self._cpp_type_generator.ExpandParams([prop]): 592 for choice in self._cpp_type_generator.ExpandParams([prop]):
549 (c.Sblock('case %s: {' % cpp_util.GetValueType( 593 (c.Sblock('case %s: {' % cpp_util.GetValueType(
550 self._cpp_type_generator.GetReferencedProperty(choice).type_)) 594 self._cpp_type_generator.GetReferencedProperty(choice).type_))
551 .Concat(self._GeneratePopulatePropertyFromValue( 595 .Concat(self._GeneratePopulatePropertyFromValue(
552 choice, value_var, dst, failure_value, check_type=False)) 596 choice, value_var, dst, failure_value, check_type=False))
553 .Append('%s = %s;' % 597 .Append('%s = %s;' %
554 (type_var, 598 (type_var,
555 self._cpp_type_generator.GetEnumValue( 599 self._cpp_type_generator.GetEnumValue(
556 prop, choice.type_.name))) 600 prop, choice.type_.name)))
557 .Append('break;') 601 .Append('break;')
558 .Eblock('}') 602 .Eblock('}')
559 ) 603 )
560 (c.Append('default:') 604 (c.Append('default:')
561 .Append(' return %(failure_value)s;') 605 .Append(' return %(failure_value)s;')
562 ) 606 )
563 c.Eblock('}') 607 c.Eblock('}')
564 elif prop.type_ == PropertyType.ENUM: 608
565 c.Sblock('{') 609 def _GenerateEnumTypePopulate(self, c, prop, value_var):
Yoyo Zhou 2012/08/27 23:56:04 Is this ever called?
chebert 2012/08/28 21:31:14 Nope. Deleting it. On 2012/08/27 23:56:04, Yoyo Zh
566 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') 610 c.Sblock('{')
611 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp',
612 self._cpp_type_generator.GetReferencedProperty(prop).enum_values)
Yoyo Zhou 2012/08/27 23:56:04 indentation is either 4 spaces after an open paren
chebert 2012/08/28 21:31:14 Done.
613 if prop.optional:
614 c.Append('%(dst)s->%(name)s.reset(enum_temp);')
615 else:
567 c.Append('%(dst)s->%(name)s = enum_temp;') 616 c.Append('%(dst)s->%(name)s = enum_temp;')
568 c.Eblock('}') 617 c.Eblock('}')
569 elif prop.type_ == PropertyType.BINARY: 618
570 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') 619 def _GenerateEnumPopulate(self, c, prop, value_var):
571 .Append(' return %(failure_value)s;') 620 c.Sblock('{')
572 .Append('const base::BinaryValue* binary_value =') 621 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp',
573 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);') 622 self._cpp_type_generator.GetReferencedProperty(prop).enum_values)
Yoyo Zhou 2012/08/27 23:56:04 Seems like you can use this inside GenerateStringT
chebert 2012/08/28 21:31:14 Done.
623 c.Append('%(dst)s->%(name)s = enum_temp;')
624 c.Eblock('}')
625
626 def _GenerateBinaryPopulate(self, c, prop):
627 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))')
628 .Append(' return %(failure_value)s;')
629 .Append('const base::BinaryValue* binary_value =')
630 .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);')
631 )
632 if prop.optional:
633 (c.Append('%(dst)s->%(name)s.reset(')
634 .Append(' new std::string(binary_value->GetBuffer(),')
635 .Append(' binary_value->GetSize()));')
574 ) 636 )
575 if prop.optional:
576 (c.Append('%(dst)s->%(name)s.reset(')
577 .Append(' new std::string(binary_value->GetBuffer(),')
578 .Append(' binary_value->GetSize()));')
579 )
580 else:
581 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
582 .Append(' binary_value->GetSize());')
583 )
584 else: 637 else:
585 raise NotImplementedError(prop.type_) 638 (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
586 c.Eblock('}') 639 .Append(' binary_value->GetSize());')
587 sub = { 640 )
588 'value_var': value_var,
589 'name': prop.unix_name,
590 'dst': dst,
591 'failure_value': failure_value,
592 }
593 if prop.type_ not in (PropertyType.CHOICES, PropertyType.ANY):
594 sub['ctype'] = self._cpp_type_generator.GetType(prop)
595 sub['compiled_ctype'] = self._cpp_type_generator.GetCompiledType(prop)
596 sub['value_type'] = cpp_util.GetValueType(self._cpp_type_generator
597 .GetReferencedProperty(prop).type_)
598 c.Substitute(sub)
599 return c
600 641
601 def _GenerateListValueToEnumArrayConversion(self, c, prop): 642 def _GenerateListValueToEnumArrayConversion(self, c, prop):
602 """Appends code that converts a ListValue of string contstants to 643 """Appends code that converts a ListValue of string contstants to
603 an array of enums in dst. 644 an array of enums in dst.
604 Leaves dst, name, and failure_value unsubstituted. 645 Leaves dst, name, and failure_value unsubstituted.
605 646
606 c: the Code object that is being appended to. 647 c: the Code object that is being appended to.
607 prop: the property that the code is populating. 648 prop: the property that the code is populating.
608 """ 649 """
609 accessor = '.' 650 accessor = '.'
610 if prop.optional: 651 if prop.optional:
611 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + ( 652 c.Append('%(dst)s->%(name)s.reset(new std::vector<' + (
612 self._cpp_type_generator.GetType(prop.item_type) + '>);')) 653 self._cpp_type_generator.GetType(prop.item_type) + '>);'))
613 accessor = '->' 654 accessor = '->'
614 c.Sblock('for (ListValue::const_iterator it = list->begin(); ' 655 c.Sblock('for (ListValue::const_iterator it = list->begin(); '
615 'it != list->end(); ++it) {') 656 'it != list->end(); ++it) {')
616 self._GenerateStringToEnumConversion(c, prop.item_type, 657 self._GenerateStringToEnumConversion(c, prop.item_type, '(*it)',
617 '(*it)', 'enum_temp') 658 'enum_temp', prop.item_type.enum_values)
Yoyo Zhou 2012/08/27 23:56:04 same comment about indent.
chebert 2012/08/28 21:31:14 Done.
618 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);') 659 c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);')
619 c.Eblock('}') 660 c.Eblock('}')
620 661
621 def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp): 662 def _GenerateStringToEnumConversion(self,
663 c,
664 prop,
665 value_var,
666 enum_temp,
667 enum_values):
622 """Appends code that converts a string to an enum. 668 """Appends code that converts a string to an enum.
623 Leaves failure_value unsubstituded. 669 Leaves failure_value unsubstituded.
Yoyo Zhou 2012/08/27 23:56:04 typo: unsubstituted
chebert 2012/08/28 21:31:14 Done.
624 670
625 c: the code that is appended to. 671 c: the code that is appended to.
626 prop: the property that the code is populating. 672 prop: the property that the code is populating.
627 value_var: the string value that is being converted. 673 value_var: the string value that is being converted.
628 enum_temp: the name used to store the temporary enum value. 674 enum_temp: the name used to store the temporary enum value.
675 enum_values: the list of possible enum values.
629 """ 676 """
630 (c.Append('%s %s;' % (self._cpp_type_generator.GetType(prop), enum_temp)) 677 (c.Append('%s %s;' % (self._cpp_type_generator.GetCompiledType(prop),
678 enum_temp))
631 .Append('std::string enum_as_string;') 679 .Append('std::string enum_as_string;')
632 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var) 680 .Append('if (!%s->GetAsString(&enum_as_string))' % value_var)
633 .Append(' return %(failure_value)s;') 681 .Append(' return %(failure_value)s;')
634 ) 682 )
635 for i, enum_value in enumerate(prop.enum_values): 683 for i, enum_value in enumerate(enum_values):
636 (c.Append( 684 (c.Append(
637 ('if' if i == 0 else 'else if') + 685 ('if' if i == 0 else 'else if') +
638 '(enum_as_string == "%s")' % enum_value) 686 '(enum_as_string == "%s")' % enum_value)
639 .Append(' ' + enum_temp + ' = %s;' % ( 687 .Append(' ' + enum_temp + ' = %s;' % (
640 self._cpp_type_generator.GetEnumValue(prop, enum_value))) 688 self._cpp_type_generator.GetEnumValue(prop, enum_value)))
641 ) 689 )
642 (c.Append('else') 690 (c.Append('else')
643 .Append(' return %(failure_value)s;') 691 .Append(' return %(failure_value)s;')
644 ) 692 )
645 693
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 .Append('return scoped_ptr<base::Value>();') 738 .Append('return scoped_ptr<base::Value>();')
691 .Eblock('}') 739 .Eblock('}')
692 .Append() 740 .Append()
693 .Substitute({ 741 .Substitute({
694 'cpp_namespace': cpp_namespace, 742 'cpp_namespace': cpp_namespace,
695 'choice': cpp_util.Classname(prop.name) 743 'choice': cpp_util.Classname(prop.name)
696 }) 744 })
697 ) 745 )
698 return c 746 return c
699 747
748 def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop):
749 """Generates CreateEnumValue() that returns the base::StringValue
Yoyo Zhou 2012/08/27 23:56:04 Sorry, but I'm confused as to how this function di
chebert 2012/08/28 21:31:14 The plan I believe is to phase out the old-style o
750 representation of an enum type.
751 """
752 c = Code()
753 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name))
754 c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % (
755 classname, classname.lower()))
756 c.Sblock('switch (%s) {' % classname.lower())
757
758 enum_prop = self._cpp_type_generator.GetReferencedProperty(prop)
759 for enum_value in enum_prop.enum_values:
760 c.Concat(self._GenerateReturnCase(
761 '%s_%s' % (classname.upper(), enum_value.upper()),
762 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' %
763 enum_value))
764 (c.Eblock('}')
765 .Append('NOTREACHED();')
766 .Append('return scoped_ptr<base::Value>();')
767 .Eblock('}')
768 )
769 return c
770
700 def _GenerateCreateEnumValue(self, cpp_namespace, prop): 771 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
701 """Generates CreateEnumValue() that returns the base::StringValue 772 """Generates CreateEnumValue() that returns the base::StringValue
702 representation of an enum. 773 representation of an enum.
703 """ 774 """
704 c = Code() 775 c = Code()
705 c.Append('// static') 776 c.Append('// static')
706 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' 777 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
707 '%(arg)s) {') 778 '%(arg)s) {')
708 c.Sblock('switch (%s) {' % prop.unix_name) 779 c.Sblock('switch (%s) {' % prop.unix_name)
709 if prop.optional: 780 if prop.optional:
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 return c 877 return c
807 878
808 def _InitializePropertyToDefault(self, prop, dst): 879 def _InitializePropertyToDefault(self, prop, dst):
809 """Initialize a model.Property to its default value inside an object. 880 """Initialize a model.Property to its default value inside an object.
810 881
811 E.g for optional enum "state", generate dst->state = STATE_NONE; 882 E.g for optional enum "state", generate dst->state = STATE_NONE;
812 883
813 dst: Type* 884 dst: Type*
814 """ 885 """
815 c = Code() 886 c = Code()
816 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): 887 if (self._cpp_type_generator.IsEnumOrEnumRef(prop) or
888 prop.type_ == PropertyType.CHOICES):
817 if prop.optional: 889 if prop.optional:
818 prop_name = prop.unix_name 890 prop_name = prop.unix_name
819 if prop.type_ == PropertyType.CHOICES: 891 if prop.type_ == PropertyType.CHOICES:
820 prop_name = prop.unix_name + '_type' 892 prop_name = prop.unix_name + '_type'
821 c.Append('%s->%s = %s;' % ( 893 c.Append('%s->%s = %s;' % (
822 dst, 894 dst,
823 prop_name, 895 prop_name,
824 self._cpp_type_generator.GetEnumNoneValue(prop))) 896 self._cpp_type_generator.GetEnumNoneValue(prop)))
825 return c 897 return c
826 898
827 def _IsObjectOrObjectRef(self, prop): 899 def _IsObjectOrObjectRef(self, prop):
828 """Determines if this property is an Object or is a ref to an Object. 900 """Determines if this property is an Object or is a ref to an Object.
829 """ 901 """
830 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 902 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
831 PropertyType.OBJECT) 903 PropertyType.OBJECT)
832 904
833 def _IsArrayOrArrayRef(self, prop): 905 def _IsArrayOrArrayRef(self, prop):
834 """Determines if this property is an Array or is a ref to an Array. 906 """Determines if this property is an Array or is a ref to an Array.
835 """ 907 """
836 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 908 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
837 PropertyType.ARRAY) 909 PropertyType.ARRAY)
838 910
911 def _IsEnumRef(self, type_):
912 """Determines if this type is an Enum ref but not an Enum.
913 """
914 return (self._cpp_type_generator.IsEnumOrEnumRef(type_) and
915 type_ != PropertyType.ENUM)
916
839 def _IsFundamentalOrFundamentalRef(self, prop): 917 def _IsFundamentalOrFundamentalRef(self, prop):
840 """Determines if this property is a Fundamental type or is a ref to a 918 """Determines if this property is a Fundamental type or is a ref to a
841 Fundamental type. 919 Fundamental type.
842 """ 920 """
843 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 921 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
844 is_fundamental) 922 is_fundamental)
OLDNEW
« no previous file with comments | « no previous file | tools/json_schema_compiler/cpp_type_generator.py » ('j') | tools/json_schema_compiler/cpp_type_generator.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698