Chromium Code Reviews| OLD | NEW |
|---|---|
| 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, Type | 6 from model import PropertyType, Type |
| 7 import cpp_util | 7 import cpp_util |
| 8 import model | 8 import model |
| 9 import schema_util | 9 import schema_util |
| 10 import sys | 10 import sys |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 return Code().Append(s) | 175 return Code().Append(s) |
| 176 | 176 |
| 177 def _GenerateTypePopulate(self, cpp_namespace, type_): | 177 def _GenerateTypePopulate(self, cpp_namespace, type_): |
| 178 """Generates the function for populating a type given a pointer to it. | 178 """Generates the function for populating a type given a pointer to it. |
| 179 | 179 |
| 180 E.g for type "Foo", generates Foo::Populate() | 180 E.g for type "Foo", generates Foo::Populate() |
| 181 """ | 181 """ |
| 182 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 182 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 183 c = Code() | 183 c = Code() |
| 184 (c.Append('// static') | 184 (c.Append('// static') |
| 185 .Append('bool %(namespace)s::Populate(') | 185 .Append('bool %(namespace)s::Populate(')) |
| 186 .Sblock(' const base::Value& value, %(name)s* out) {') | 186 if self._namespace.generate_error_messages: |
| 187 ) | 187 c.Sblock(' const base::Value& value, %(name)s* out, ' |
| 188 'std::string* error) {') | |
|
cduvall
2013/06/07 19:10:42
line this up with the "'" on the line above
Aaron Jacobs
2013/06/07 20:36:13
Done.
| |
| 189 else: | |
| 190 c.Sblock(' const base::Value& value, %(name)s* out) {') | |
| 191 | |
| 188 if type_.property_type == PropertyType.CHOICES: | 192 if type_.property_type == PropertyType.CHOICES: |
| 189 for choice in type_.choices: | 193 for choice in type_.choices: |
| 190 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) | 194 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) |
| 191 (c.Sblock('if (value.IsType(%s)) {' % value_type) | 195 (c.Sblock('if (value.IsType(%s)) {' % value_type) |
| 192 .Concat(self._GeneratePopulateVariableFromValue( | 196 .Concat(self._GeneratePopulateVariableFromValue( |
| 193 choice, | 197 choice, |
| 194 '(&value)', | 198 '(&value)', |
| 195 'out->as_%s' % choice.unix_name, | 199 'out->as_%s' % choice.unix_name, |
| 196 'false', | 200 'false', |
| 197 is_ptr=True)) | 201 is_ptr=True)) |
| 198 .Append('return true;') | 202 .Append('return true;') |
| 199 .Eblock('}') | 203 .Eblock('}') |
| 200 ) | 204 ) |
| 205 if self._namespace.generate_error_messages: | |
| 206 self._GenerateError(c, | |
| 207 'type-check', | |
| 208 '"unexpected type, got " << value.GetType()') | |
| 201 c.Append('return false;') | 209 c.Append('return false;') |
| 202 elif type_.property_type == PropertyType.OBJECT: | 210 elif type_.property_type == PropertyType.OBJECT: |
| 203 (c.Append('if (!value.IsType(base::Value::TYPE_DICTIONARY))') | 211 c.Append('if (!value.IsType(base::Value::TYPE_DICTIONARY)) {') |
| 204 .Append(' return false;') | 212 if self._namespace.generate_error_messages: |
| 205 ) | 213 self._GenerateError(c, |
| 214 'type-check', | |
| 215 '"expected dictionary, got " << value.GetType()', | |
| 216 True) | |
| 217 (c.Append(' return false;') | |
| 218 .Append('}')) | |
| 219 | |
| 206 if type_.properties or type_.additional_properties is not None: | 220 if type_.properties or type_.additional_properties is not None: |
| 207 c.Append('const base::DictionaryValue* dict = ' | 221 c.Append('const base::DictionaryValue* dict = ' |
| 208 'static_cast<const base::DictionaryValue*>(&value);') | 222 'static_cast<const base::DictionaryValue*>(&value);') |
| 209 for prop in type_.properties.values(): | 223 for prop in type_.properties.values(): |
| 210 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 224 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
| 211 for prop in type_.properties.values(): | 225 for prop in type_.properties.values(): |
| 212 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 226 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
| 213 if type_.additional_properties is not None: | 227 if type_.additional_properties is not None: |
| 214 if type_.additional_properties.property_type == PropertyType.ANY: | 228 if type_.additional_properties.property_type == PropertyType.ANY: |
| 215 c.Append('out->additional_properties.MergeDictionary(dict);') | 229 c.Append('out->additional_properties.MergeDictionary(dict);') |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 246 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') | 260 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
| 247 .Concat(self._GeneratePopulatePropertyFromValue( | 261 .Concat(self._GeneratePopulatePropertyFromValue( |
| 248 prop, value_var, dst, 'false'))) | 262 prop, value_var, dst, 'false'))) |
| 249 underlying_type = self._type_helper.FollowRef(prop.type_) | 263 underlying_type = self._type_helper.FollowRef(prop.type_) |
| 250 if underlying_type.property_type == PropertyType.ENUM: | 264 if underlying_type.property_type == PropertyType.ENUM: |
| 251 (c.Append('} else {') | 265 (c.Append('} else {') |
| 252 .Append('%%(dst)s->%%(name)s = %s;' % | 266 .Append('%%(dst)s->%%(name)s = %s;' % |
| 253 self._type_helper.GetEnumNoneValue(prop.type_))) | 267 self._type_helper.GetEnumNoneValue(prop.type_))) |
| 254 c.Eblock('}') | 268 c.Eblock('}') |
| 255 else: | 269 else: |
| 256 (c.Append( | 270 c.Append( |
| 257 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 271 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
| 258 .Append(' return false;') | 272 if self._namespace.generate_error_messages: |
| 273 self._GenerateError(c, | |
| 274 'get-from-dict', | |
| 275 '"key not found: %(key)s"', | |
| 276 True) | |
| 277 (c.Append(' return false;') | |
| 278 .Append('}') | |
| 259 .Concat(self._GeneratePopulatePropertyFromValue( | 279 .Concat(self._GeneratePopulatePropertyFromValue( |
| 260 prop, value_var, dst, 'false')) | 280 prop, value_var, dst, 'false')) |
| 261 ) | 281 ) |
| 262 c.Append() | 282 c.Append() |
| 263 c.Substitute({ | 283 c.Substitute({ |
| 264 'value_var': value_var, | 284 'value_var': value_var, |
| 265 'key': prop.name, | 285 'key': prop.name, |
| 266 'src': src, | 286 'src': src, |
| 267 'dst': dst, | 287 'dst': dst, |
| 268 'name': prop.unix_name | 288 'name': prop.unix_name |
| 269 }) | 289 }) |
| 270 return c | 290 return c |
| 271 | 291 |
| 272 def _GenerateTypeFromValue(self, cpp_namespace, type_): | 292 def _GenerateTypeFromValue(self, cpp_namespace, type_): |
| 273 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 293 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
| 274 c = Code() | 294 c = Code() |
| 275 (c.Append('// static') | 295 c.Append('// static') |
| 276 .Append('scoped_ptr<%s> %s::FromValue(const base::Value& value) {' % ( | 296 if self._namespace.generate_error_messages: |
| 297 c.Append('scoped_ptr<%s> %s::FromValue(const base::Value& value, ' | |
| 298 'std::string* error) {' % (classname, cpp_namespace)) | |
| 299 else: | |
| 300 c.Append('scoped_ptr<%s> %s::FromValue(const base::Value& value) {' % ( | |
| 277 classname, cpp_namespace)) | 301 classname, cpp_namespace)) |
| 278 .Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) | 302 c.Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) |
| 279 .Append(' if (!Populate(value, out.get()))') | 303 if self._namespace.generate_error_messages: |
| 280 .Append(' return scoped_ptr<%s>();' % classname) | 304 c.Append(' if (!Populate(value, out.get(), error))') |
| 305 else: | |
| 306 c.Append(' if (!Populate(value, out.get()))') | |
| 307 (c.Append(' return scoped_ptr<%s>();' % classname) | |
| 281 .Append(' return out.Pass();') | 308 .Append(' return out.Pass();') |
| 282 .Append('}') | 309 .Append('}') |
| 283 ) | 310 ) |
| 284 return c | 311 return c |
| 285 | 312 |
| 286 def _GenerateTypeToValue(self, cpp_namespace, type_): | 313 def _GenerateTypeToValue(self, cpp_namespace, type_): |
| 287 """Generates a function that serializes the type into a base::Value. | 314 """Generates a function that serializes the type into a base::Value. |
| 288 E.g. for type "Foo" generates Foo::ToValue() | 315 E.g. for type "Foo" generates Foo::ToValue() |
| 289 """ | 316 """ |
| 290 if type_.property_type == PropertyType.OBJECT: | 317 if type_.property_type == PropertyType.OBJECT: |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 def _GenerateParamsCheck(self, function, var): | 498 def _GenerateParamsCheck(self, function, var): |
| 472 """Generates a check for the correct number of arguments when creating | 499 """Generates a check for the correct number of arguments when creating |
| 473 Params. | 500 Params. |
| 474 """ | 501 """ |
| 475 c = Code() | 502 c = Code() |
| 476 num_required = 0 | 503 num_required = 0 |
| 477 for param in function.params: | 504 for param in function.params: |
| 478 if not param.optional: | 505 if not param.optional: |
| 479 num_required += 1 | 506 num_required += 1 |
| 480 if num_required == len(function.params): | 507 if num_required == len(function.params): |
| 481 c.Append('if (%(var)s.GetSize() != %(total)d)') | 508 c.Append('if (%(var)s.GetSize() != %(total)d) {') |
| 482 elif not num_required: | 509 elif not num_required: |
| 483 c.Append('if (%(var)s.GetSize() > %(total)d)') | 510 c.Append('if (%(var)s.GetSize() > %(total)d) {') |
| 484 else: | 511 else: |
| 485 c.Append('if (%(var)s.GetSize() < %(required)d' | 512 c.Append('if (%(var)s.GetSize() < %(required)d' |
| 486 ' || %(var)s.GetSize() > %(total)d)') | 513 ' || %(var)s.GetSize() > %(total)d) {') |
| 514 if self._namespace.generate_error_messages: | |
| 515 self._GenerateError( | |
| 516 c, | |
|
cduvall
2013/06/07 19:10:42
move this up in the (
Aaron Jacobs
2013/06/07 20:36:13
Done.
| |
| 517 'params', | |
| 518 '"expected %(total)d arguments, got " << %(var)s.GetSize()', | |
| 519 True) | |
| 487 c.Append(' return scoped_ptr<Params>();') | 520 c.Append(' return scoped_ptr<Params>();') |
| 521 c.Append('}') | |
| 488 c.Substitute({ | 522 c.Substitute({ |
| 489 'var': var, | 523 'var': var, |
| 490 'required': num_required, | 524 'required': num_required, |
| 491 'total': len(function.params), | 525 'total': len(function.params), |
| 492 }) | 526 }) |
| 493 return c | 527 return c |
| 494 | 528 |
| 495 def _GenerateFunctionParamsCreate(self, function): | 529 def _GenerateFunctionParamsCreate(self, function): |
| 496 """Generate function to create an instance of Params. The generated | 530 """Generate function to create an instance of Params. The generated |
| 497 function takes a base::ListValue of arguments. | 531 function takes a base::ListValue of arguments. |
| 498 | 532 |
| 499 E.g for function "Bar", generate Bar::Params::Create() | 533 E.g for function "Bar", generate Bar::Params::Create() |
| 500 """ | 534 """ |
| 501 c = Code() | 535 c = Code() |
| 502 (c.Append('// static') | 536 c.Append('// static') |
| 503 .Sblock('scoped_ptr<Params> ' | 537 if self._namespace.generate_error_messages: |
| 538 c.Sblock('scoped_ptr<Params> Params::Create(' | |
| 539 'const base::ListValue& args, std::string* error) {') | |
|
cduvall
2013/06/07 19:10:42
line up with (
Aaron Jacobs
2013/06/07 20:36:13
Done.
| |
| 540 else: | |
| 541 c.Sblock('scoped_ptr<Params> ' | |
| 504 'Params::Create(const base::ListValue& args) {') | 542 'Params::Create(const base::ListValue& args) {') |
| 505 .Concat(self._GenerateParamsCheck(function, 'args')) | 543 (c.Concat(self._GenerateParamsCheck(function, 'args')) |
| 506 .Append('scoped_ptr<Params> params(new Params());') | 544 .Append('scoped_ptr<Params> params(new Params());')) |
| 507 ) | |
| 508 | 545 |
| 509 for param in function.params: | 546 for param in function.params: |
| 510 c.Concat(self._InitializePropertyToDefault(param, 'params')) | 547 c.Concat(self._InitializePropertyToDefault(param, 'params')) |
| 511 | 548 |
| 512 for i, param in enumerate(function.params): | 549 for i, param in enumerate(function.params): |
| 513 # Any failure will cause this function to return. If any argument is | 550 # Any failure will cause this function to return. If any argument is |
| 514 # incorrect or missing, those following it are not processed. Note that | 551 # incorrect or missing, those following it are not processed. Note that |
| 515 # for optional arguments, we allow missing arguments and proceed because | 552 # for optional arguments, we allow missing arguments and proceed because |
| 516 # there may be other arguments following it. | 553 # there may be other arguments following it. |
| 517 failure_value = 'scoped_ptr<Params>()' | 554 failure_value = 'scoped_ptr<Params>()' |
| 518 c.Append() | 555 c.Append() |
| 519 value_var = param.unix_name + '_value' | 556 value_var = param.unix_name + '_value' |
| 520 (c.Append('const base::Value* %(value_var)s = NULL;') | 557 (c.Append('const base::Value* %(value_var)s = NULL;') |
| 521 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') | 558 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') |
| 522 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') | 559 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') |
| 523 .Concat(self._GeneratePopulatePropertyFromValue( | 560 .Concat(self._GeneratePopulatePropertyFromValue( |
| 524 param, value_var, 'params', failure_value)) | 561 param, value_var, 'params', failure_value)) |
| 525 .Eblock('}') | 562 .Eblock('}') |
| 526 ) | 563 ) |
| 527 if not param.optional: | 564 if not param.optional: |
| 528 (c.Sblock('else {') | 565 c.Sblock('else {') |
| 529 .Append('return %s;' % failure_value) | 566 if self._namespace.generate_error_messages: |
| 530 .Eblock('}') | 567 self._GenerateError(c, |
| 531 ) | 568 'params', |
| 569 '"missing non-optional value: %(value_var)s"') | |
| 570 (c.Append('return %s;' % failure_value) | |
| 571 .Eblock('}')) | |
| 532 c.Substitute({'value_var': value_var, 'i': i}) | 572 c.Substitute({'value_var': value_var, 'i': i}) |
| 533 (c.Append() | 573 (c.Append() |
| 534 .Append('return params.Pass();') | 574 .Append('return params.Pass();') |
| 535 .Eblock('}') | 575 .Eblock('}') |
| 536 .Append() | 576 .Append() |
| 537 ) | 577 ) |
| 538 | 578 |
| 539 return c | 579 return c |
| 540 | 580 |
| 541 def _GeneratePopulatePropertyFromValue(self, | 581 def _GeneratePopulatePropertyFromValue(self, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 566 |failure_value|. | 606 |failure_value|. |
| 567 """ | 607 """ |
| 568 c = Code() | 608 c = Code() |
| 569 c.Sblock('{') | 609 c.Sblock('{') |
| 570 | 610 |
| 571 underlying_type = self._type_helper.FollowRef(type_) | 611 underlying_type = self._type_helper.FollowRef(type_) |
| 572 | 612 |
| 573 if underlying_type.property_type.is_fundamental: | 613 if underlying_type.property_type.is_fundamental: |
| 574 if is_ptr: | 614 if is_ptr: |
| 575 (c.Append('%(cpp_type)s temp;') | 615 (c.Append('%(cpp_type)s temp;') |
| 576 .Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 616 .Append('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
| 577 self._type_helper.FollowRef(type_), src_var, '&temp')) | 617 self._type_helper.FollowRef(type_), src_var, '&temp'))) |
| 578 .Append(' return %(failure_value)s;') | 618 if self._namespace.generate_error_messages: |
| 619 self._GenerateError( | |
| 620 c, | |
|
cduvall
2013/06/07 19:10:42
put after (
Aaron Jacobs
2013/06/07 20:36:13
Done.
| |
| 621 'get-as-type', | |
| 622 '"expected %s, got " << %s->GetType()' % | |
| 623 (self._type_helper.GetCppType(type_), src_var), True) | |
| 624 (c.Append(' return %(failure_value)s;') | |
| 625 .Append('}') | |
| 579 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') | 626 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') |
| 580 ) | 627 ) |
| 581 else: | 628 else: |
| 582 (c.Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 629 (c.Append('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
| 583 self._type_helper.FollowRef(type_), | 630 self._type_helper.FollowRef(type_), |
| 584 src_var, | 631 src_var, |
| 585 '&%s' % dst_var)) | 632 '&%s' % dst_var))) |
| 586 .Append(' return %(failure_value)s;') | 633 if self._namespace.generate_error_messages: |
| 634 self._GenerateError( | |
| 635 c, | |
|
cduvall
2013/06/07 19:10:42
put after ( if it fits (everywhere)
Aaron Jacobs
2013/06/07 20:36:13
Done.
| |
| 636 'get-as-type', | |
| 637 '"expected %s, got " << %s->GetType()' % | |
| 638 (self._type_helper.GetCppType(type_), src_var), True) | |
| 639 (c.Append(' return %(failure_value)s;') | |
| 640 .Append('}') | |
| 587 ) | 641 ) |
| 588 elif underlying_type.property_type == PropertyType.OBJECT: | 642 elif underlying_type.property_type == PropertyType.OBJECT: |
| 589 if is_ptr: | 643 if is_ptr: |
| 590 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 644 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
| 591 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 645 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')) |
| 592 .Append(' return %(failure_value)s;') | 646 if self._namespace.generate_error_messages: |
| 593 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 647 self._GenerateError( |
| 594 .Append('if (!%(cpp_type)s::Populate(*dictionary, temp.get()))') | 648 c, |
| 595 .Append(' return %(failure_value)s;') | 649 'get-as-dict', |
| 650 '"expected dictionary, got " << %(src_var)s->GetType()', True) | |
| 651 (c.Append(' return %(failure_value)s;') | |
| 652 .Append('}') | |
| 653 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());')) | |
| 654 if self._namespace.generate_error_messages: | |
| 655 c.Append('if (!%(cpp_type)s::Populate(' | |
| 656 '*dictionary, temp.get(), error)) {') | |
| 657 else: | |
| 658 c.Append('if (!%(cpp_type)s::Populate(*dictionary, temp.get())) {') | |
| 659 (c.Append(' return %(failure_value)s;') | |
| 660 .Append('}') | |
| 596 .Append('%(dst_var)s = temp.Pass();') | 661 .Append('%(dst_var)s = temp.Pass();') |
| 597 ) | 662 ) |
| 598 else: | 663 else: |
| 599 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 664 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
| 600 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 665 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')) |
| 601 .Append(' return %(failure_value)s;') | 666 if self._namespace.generate_error_messages: |
| 602 .Append('if (!%(cpp_type)s::Populate(*dictionary, &%(dst_var)s))') | 667 self._GenerateError( |
| 603 .Append(' return %(failure_value)s;') | 668 c, |
| 669 'get-as-dict', | |
| 670 '"expected dictionary, got " << %(src_var)s->GetType()', True) | |
| 671 (c.Append(' return %(failure_value)s;') | |
| 672 .Append('}')) | |
| 673 if self._namespace.generate_error_messages: | |
| 674 c.Append('if (!%(cpp_type)s::Populate(' | |
| 675 '*dictionary, &%(dst_var)s, error)) {') | |
| 676 else: | |
| 677 c.Append('if (!%(cpp_type)s::Populate(*dictionary, &%(dst_var)s)) {') | |
| 678 (c.Append(' return %(failure_value)s;') | |
| 679 .Append('}') | |
| 604 ) | 680 ) |
| 605 elif underlying_type.property_type == PropertyType.FUNCTION: | 681 elif underlying_type.property_type == PropertyType.FUNCTION: |
| 606 if is_ptr: | 682 if is_ptr: |
| 607 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') | 683 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') |
| 608 elif underlying_type.property_type == PropertyType.ANY: | 684 elif underlying_type.property_type == PropertyType.ANY: |
| 609 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') | 685 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') |
| 610 elif underlying_type.property_type == PropertyType.ARRAY: | 686 elif underlying_type.property_type == PropertyType.ARRAY: |
| 611 # util_cc_helper deals with optional and required arrays | 687 # util_cc_helper deals with optional and required arrays |
| 612 (c.Append('const base::ListValue* list = NULL;') | 688 (c.Append('const base::ListValue* list = NULL;') |
| 613 .Append('if (!%(src_var)s->GetAsList(&list))') | 689 .Append('if (!%(src_var)s->GetAsList(&list)) {')) |
| 614 .Append(' return %(failure_value)s;')) | 690 if self._namespace.generate_error_messages: |
| 691 self._GenerateError( | |
| 692 c, | |
| 693 'get-as-list', | |
| 694 '"expected list, got " << %(src_var)s->GetType()', True) | |
| 695 (c.Append(' return %(failure_value)s;') | |
| 696 .Append('}')) | |
| 615 item_type = underlying_type.item_type | 697 item_type = underlying_type.item_type |
| 616 if item_type.property_type == PropertyType.ENUM: | 698 if item_type.property_type == PropertyType.ENUM: |
| 617 c.Concat(self._GenerateListValueToEnumArrayConversion( | 699 c.Concat(self._GenerateListValueToEnumArrayConversion( |
| 618 item_type, | 700 item_type, |
| 619 'list', | 701 'list', |
| 620 dst_var, | 702 dst_var, |
| 621 failure_value, | 703 failure_value, |
| 622 is_ptr=is_ptr)) | 704 is_ptr=is_ptr)) |
| 623 else: | 705 else: |
| 624 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 706 c.Append('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList( |
| 625 underlying_type, | 707 underlying_type, |
| 626 'list', | 708 'list', |
| 627 dst_var, | 709 dst_var, |
| 628 is_ptr)) | 710 is_ptr)) |
| 629 .Append(' return %(failure_value)s;') | 711 if self._namespace.generate_error_messages: |
| 712 self._GenerateError(c, | |
| 713 'populate-array', | |
| 714 '"unable to populate array"', | |
| 715 True) | |
| 716 (c.Append(' return %(failure_value)s;') | |
| 717 .Append('}') | |
| 630 ) | 718 ) |
| 631 elif underlying_type.property_type == PropertyType.CHOICES: | 719 elif underlying_type.property_type == PropertyType.CHOICES: |
| 632 if is_ptr: | 720 if is_ptr: |
| 633 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 721 c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
| 634 .Append('if (!%(cpp_type)s::Populate(*%(src_var)s, temp.get()))') | 722 if self._namespace.generate_error_messages: |
| 635 .Append(' return %(failure_value)s;') | 723 c.Append('if (!%(cpp_type)s::Populate(' |
| 724 '*%(src_var)s, temp.get(), error))') | |
| 725 else: | |
| 726 c.Append('if (!%(cpp_type)s::Populate(*%(src_var)s, temp.get()))') | |
| 727 (c.Append(' return %(failure_value)s;') | |
| 636 .Append('%(dst_var)s = temp.Pass();') | 728 .Append('%(dst_var)s = temp.Pass();') |
| 637 ) | 729 ) |
| 638 else: | 730 else: |
| 639 (c.Append('if (!%(cpp_type)s::Populate(*%(src_var)s, &%(dst_var)s))') | 731 if self._namespace.generate_error_messages: |
| 640 .Append(' return %(failure_value)s;') | 732 c.Append('if (!%(cpp_type)s::Populate(' |
| 641 ) | 733 '*%(src_var)s, &%(dst_var)s, error))') |
| 734 else: | |
| 735 c.Append('if (!%(cpp_type)s::Populate(*%(src_var)s, &%(dst_var)s))') | |
| 736 c.Append(' return %(failure_value)s;') | |
| 642 elif underlying_type.property_type == PropertyType.ENUM: | 737 elif underlying_type.property_type == PropertyType.ENUM: |
| 643 c.Concat(self._GenerateStringToEnumConversion(type_, | 738 c.Concat(self._GenerateStringToEnumConversion(type_, |
| 644 src_var, | 739 src_var, |
| 645 dst_var, | 740 dst_var, |
| 646 failure_value)) | 741 failure_value)) |
| 647 elif underlying_type.property_type == PropertyType.BINARY: | 742 elif underlying_type.property_type == PropertyType.BINARY: |
| 648 (c.Append('if (!%(src_var)s->IsType(%(value_type)s))') | 743 c.Append('if (!%(src_var)s->IsType(%(value_type)s)) {') |
| 649 .Append(' return %(failure_value)s;') | 744 if self._namespace.generate_error_messages: |
| 745 self._GenerateError( | |
| 746 c, | |
| 747 'type-check', | |
| 748 '"expected %(value_type)s, got " << %(src_var)s->GetType()', True) | |
| 749 (c.Append(' return %(failure_value)s;') | |
| 750 .Append('}') | |
| 650 .Append('const base::BinaryValue* binary_value =') | 751 .Append('const base::BinaryValue* binary_value =') |
| 651 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') | 752 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') |
| 652 ) | 753 ) |
| 653 if is_ptr: | 754 if is_ptr: |
| 654 (c.Append('%(dst_var)s.reset(') | 755 (c.Append('%(dst_var)s.reset(') |
| 655 .Append(' new std::string(binary_value->GetBuffer(),') | 756 .Append(' new std::string(binary_value->GetBuffer(),') |
| 656 .Append(' binary_value->GetSize()));') | 757 .Append(' binary_value->GetSize()));') |
| 657 ) | 758 ) |
| 658 else: | 759 else: |
| 659 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') | 760 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 src_var, | 810 src_var, |
| 710 dst_var, | 811 dst_var, |
| 711 failure_value): | 812 failure_value): |
| 712 """Returns Code that converts a string type in |src_var| to an enum with | 813 """Returns Code that converts a string type in |src_var| to an enum with |
| 713 type |type_| in |dst_var|. In the generated code, if |src_var| is not | 814 type |type_| in |dst_var|. In the generated code, if |src_var| is not |
| 714 a valid enum name then the function will return |failure_value|. | 815 a valid enum name then the function will return |failure_value|. |
| 715 """ | 816 """ |
| 716 c = Code() | 817 c = Code() |
| 717 enum_as_string = '%s_as_string' % type_.unix_name | 818 enum_as_string = '%s_as_string' % type_.unix_name |
| 718 (c.Append('std::string %s;' % enum_as_string) | 819 (c.Append('std::string %s;' % enum_as_string) |
| 719 .Append('if (!%s->GetAsString(&%s))' % (src_var, enum_as_string)) | 820 .Append('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string))) |
| 720 .Append(' return %s;' % failure_value) | 821 if self._namespace.generate_error_messages: |
| 822 self._GenerateError( | |
| 823 c, | |
| 824 'get-as-string', | |
| 825 '"expected std::string, got " << %s->GetType()' % src_var, True) | |
| 826 (c.Append(' return %s;' % failure_value) | |
| 827 .Append('}') | |
| 721 .Append('%s = Parse%s(%s);' % (dst_var, | 828 .Append('%s = Parse%s(%s);' % (dst_var, |
| 722 self._type_helper.GetCppType(type_), | 829 self._type_helper.GetCppType(type_), |
| 723 enum_as_string)) | 830 enum_as_string)) |
| 724 .Append('if (%s == %s)' % (dst_var, | 831 .Append('if (%s == %s) {' % (dst_var, |
| 725 self._type_helper.GetEnumNoneValue(type_))) | 832 self._type_helper.GetEnumNoneValue(type_)))) |
| 726 .Append(' return %s;' % failure_value) | 833 if self._namespace.generate_error_messages: |
| 834 self._GenerateError( | |
| 835 c, | |
| 836 'get-from-string', | |
| 837 '"got bad enum value from variable \'%s\'"' % dst_var, True) | |
| 838 (c.Append(' return %s;' % failure_value) | |
| 839 .Append('}') | |
| 727 ) | 840 ) |
| 728 return c | 841 return c |
| 729 | 842 |
| 730 def _GeneratePropertyFunctions(self, namespace, params): | 843 def _GeneratePropertyFunctions(self, namespace, params): |
| 731 """Generates the member functions for a list of parameters. | 844 """Generates the member functions for a list of parameters. |
| 732 """ | 845 """ |
| 733 return self._GenerateTypes(namespace, (param.type_ for param in params)) | 846 return self._GenerateTypes(namespace, (param.type_ for param in params)) |
| 734 | 847 |
| 735 def _GenerateTypes(self, namespace, types): | 848 def _GenerateTypes(self, namespace, types): |
| 736 """Generates the member functions for a list of types. | 849 """Generates the member functions for a list of types. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 834 """ | 947 """ |
| 835 c = Code() | 948 c = Code() |
| 836 underlying_type = self._type_helper.FollowRef(prop.type_) | 949 underlying_type = self._type_helper.FollowRef(prop.type_) |
| 837 if (underlying_type.property_type == PropertyType.ENUM and | 950 if (underlying_type.property_type == PropertyType.ENUM and |
| 838 prop.optional): | 951 prop.optional): |
| 839 c.Append('%s->%s = %s;' % ( | 952 c.Append('%s->%s = %s;' % ( |
| 840 dst, | 953 dst, |
| 841 prop.unix_name, | 954 prop.unix_name, |
| 842 self._type_helper.GetEnumNoneValue(prop.type_))) | 955 self._type_helper.GetEnumNoneValue(prop.type_))) |
| 843 return c | 956 return c |
| 957 | |
| 958 def _GenerateError(self, c, location, body, indent = False): | |
| 959 """Generates an error message pertaining to population failure, and appends | |
| 960 it to |c|. | |
| 961 | |
| 962 E.g... | |
| 963 (get-from-dict): 'key not found: content' at | |
| 964 gen/chrome/common/extensions/api/omnibox.cc:241 | |
| 965 """ | |
| 966 padding = ''; | |
| 967 if indent: | |
| 968 padding = ' '; | |
| 969 (c.Append(padding + 'if (error) {') | |
| 970 .Append(padding + ' std::stringstream ss;') | |
| 971 .Append(padding + ' ss << "' + location + ': \'" << ' + body + | |
| 972 ' << "\' at " << __FILE__ << ":" << (__LINE__ - 3);') | |
| 973 .Append(padding + ' *error = ss.str();') | |
| 974 .Append(padding + '}')) | |
| OLD | NEW |