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 13 matching lines...) Expand all Loading... | |
24 """A .cc generator for a namespace. | 24 """A .cc generator for a namespace. |
25 """ | 25 """ |
26 def __init__(self, namespace, cpp_type_generator, cpp_namespace): | 26 def __init__(self, namespace, cpp_type_generator, cpp_namespace): |
27 self._namespace = namespace | 27 self._namespace = namespace |
28 self._type_helper = cpp_type_generator | 28 self._type_helper = cpp_type_generator |
29 self._cpp_namespace = cpp_namespace | 29 self._cpp_namespace = cpp_namespace |
30 self._target_namespace = ( | 30 self._target_namespace = ( |
31 self._type_helper.GetCppNamespaceName(self._namespace)) | 31 self._type_helper.GetCppNamespaceName(self._namespace)) |
32 self._util_cc_helper = ( | 32 self._util_cc_helper = ( |
33 util_cc_helper.UtilCCHelper(self._type_helper)) | 33 util_cc_helper.UtilCCHelper(self._type_helper)) |
34 self._generate_error_messages = namespace.compiler_options.get( | |
35 'generate_error_messages', False) | |
34 | 36 |
35 def Generate(self): | 37 def Generate(self): |
36 """Generates a Code object with the .cc for a single namespace. | 38 """Generates a Code object with the .cc for a single namespace. |
37 """ | 39 """ |
38 c = Code() | 40 c = Code() |
39 (c.Append(cpp_util.CHROMIUM_LICENSE) | 41 (c.Append(cpp_util.CHROMIUM_LICENSE) |
40 .Append() | 42 .Append() |
41 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 43 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
42 .Append() | 44 .Append() |
43 .Append(self._util_cc_helper.GetIncludePath()) | 45 .Append(self._util_cc_helper.GetIncludePath()) |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 | 178 |
177 def _GenerateTypePopulate(self, cpp_namespace, type_): | 179 def _GenerateTypePopulate(self, cpp_namespace, type_): |
178 """Generates the function for populating a type given a pointer to it. | 180 """Generates the function for populating a type given a pointer to it. |
179 | 181 |
180 E.g for type "Foo", generates Foo::Populate() | 182 E.g for type "Foo", generates Foo::Populate() |
181 """ | 183 """ |
182 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 184 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
183 c = Code() | 185 c = Code() |
184 (c.Append('// static') | 186 (c.Append('// static') |
185 .Append('bool %(namespace)s::Populate(') | 187 .Append('bool %(namespace)s::Populate(') |
186 .Sblock(' const base::Value& value, %(name)s* out) {') | 188 .Sblock(' %s) {' % self._GenerateParams( |
187 ) | 189 ['const base::Value& value', '%(name)s* out']))) |
190 | |
188 if type_.property_type == PropertyType.CHOICES: | 191 if type_.property_type == PropertyType.CHOICES: |
189 for choice in type_.choices: | 192 for choice in type_.choices: |
190 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) | 193 value_type = cpp_util.GetValueType(self._type_helper.FollowRef(choice)) |
191 (c.Sblock('if (value.IsType(%s)) {' % value_type) | 194 (c.Sblock('if (value.IsType(%s)) {' % value_type) |
192 .Concat(self._GeneratePopulateVariableFromValue( | 195 .Concat(self._GeneratePopulateVariableFromValue( |
193 choice, | 196 choice, |
194 '(&value)', | 197 '(&value)', |
195 'out->as_%s' % choice.unix_name, | 198 'out->as_%s' % choice.unix_name, |
196 'false', | 199 'false', |
197 is_ptr=True)) | 200 is_ptr=True)) |
198 .Append('return true;') | 201 .Append('return true;') |
199 .Eblock('}') | 202 .Eblock('}') |
200 ) | 203 ) |
204 c.Concat(self._GenerateError( | |
205 '"unexpected type, got " + ' | |
206 'json_schema_compiler::util::ValueTypeToString(value.GetType())')) | |
not at google - send to devlin
2013/06/14 21:48:39
can you use the same style here (and everywhere),
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
201 c.Append('return false;') | 207 c.Append('return false;') |
202 elif type_.property_type == PropertyType.OBJECT: | 208 elif type_.property_type == PropertyType.OBJECT: |
203 (c.Append('if (!value.IsType(base::Value::TYPE_DICTIONARY))') | 209 c.Append('if (!value.IsType(base::Value::TYPE_DICTIONARY)) {') |
204 .Append(' return false;') | 210 c.Concat(self._GenerateError( |
205 ) | 211 '"expected dictionary, got " + ' |
212 'json_schema_compiler::util::ValueTypeToString(value.GetType())', | |
not at google - send to devlin
2013/06/14 21:48:39
and also there are wrapper for all these in util_c
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
213 True)) | |
214 (c.Append(' return false;') | |
215 .Append('}')) | |
216 | |
206 if type_.properties or type_.additional_properties is not None: | 217 if type_.properties or type_.additional_properties is not None: |
207 c.Append('const base::DictionaryValue* dict = ' | 218 c.Append('const base::DictionaryValue* dict = ' |
208 'static_cast<const base::DictionaryValue*>(&value);') | 219 'static_cast<const base::DictionaryValue*>(&value);') |
209 for prop in type_.properties.values(): | 220 for prop in type_.properties.values(): |
210 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 221 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
211 for prop in type_.properties.values(): | 222 for prop in type_.properties.values(): |
212 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 223 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
213 if type_.additional_properties is not None: | 224 if type_.additional_properties is not None: |
214 if type_.additional_properties.property_type == PropertyType.ANY: | 225 if type_.additional_properties.property_type == PropertyType.ANY: |
215 c.Append('out->additional_properties.MergeDictionary(dict);') | 226 c.Append('out->additional_properties.MergeDictionary(dict);') |
(...skipping 30 matching lines...) Expand all Loading... | |
246 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') | 257 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
247 .Concat(self._GeneratePopulatePropertyFromValue( | 258 .Concat(self._GeneratePopulatePropertyFromValue( |
248 prop, value_var, dst, 'false'))) | 259 prop, value_var, dst, 'false'))) |
249 underlying_type = self._type_helper.FollowRef(prop.type_) | 260 underlying_type = self._type_helper.FollowRef(prop.type_) |
250 if underlying_type.property_type == PropertyType.ENUM: | 261 if underlying_type.property_type == PropertyType.ENUM: |
251 (c.Append('} else {') | 262 (c.Append('} else {') |
252 .Append('%%(dst)s->%%(name)s = %s;' % | 263 .Append('%%(dst)s->%%(name)s = %s;' % |
253 self._type_helper.GetEnumNoneValue(prop.type_))) | 264 self._type_helper.GetEnumNoneValue(prop.type_))) |
254 c.Eblock('}') | 265 c.Eblock('}') |
255 else: | 266 else: |
256 (c.Append( | 267 c.Append( |
257 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 268 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') |
258 .Append(' return false;') | 269 c.Concat(self._GenerateError('"%%(key)s is required"', True)) |
not at google - send to devlin
2013/06/14 21:48:39
let's standardise on something. single quotes - I
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
270 (c.Append(' return false;') | |
271 .Append('}') | |
259 .Concat(self._GeneratePopulatePropertyFromValue( | 272 .Concat(self._GeneratePopulatePropertyFromValue( |
260 prop, value_var, dst, 'false')) | 273 prop, value_var, dst, 'false')) |
261 ) | 274 ) |
262 c.Append() | 275 c.Append() |
263 c.Substitute({ | 276 c.Substitute({ |
264 'value_var': value_var, | 277 'value_var': value_var, |
265 'key': prop.name, | 278 'key': prop.name, |
266 'src': src, | 279 'src': src, |
267 'dst': dst, | 280 'dst': dst, |
268 'name': prop.unix_name | 281 'name': prop.unix_name |
269 }) | 282 }) |
270 return c | 283 return c |
271 | 284 |
272 def _GenerateTypeFromValue(self, cpp_namespace, type_): | 285 def _GenerateTypeFromValue(self, cpp_namespace, type_): |
273 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 286 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
274 c = Code() | 287 c = Code() |
275 (c.Append('// static') | 288 (c.Append('// static') |
276 .Append('scoped_ptr<%s> %s::FromValue(const base::Value& value) {' % ( | 289 .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname, |
277 classname, cpp_namespace)) | 290 cpp_namespace, self._GenerateParams(['const base::Value& value']))) |
278 .Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) | 291 .Append(' scoped_ptr<%s> out(new %s());' % (classname, classname)) |
279 .Append(' if (!Populate(value, out.get()))') | 292 .Append(' if (!Populate(%s))' % self._GenerateArgs( |
293 ['value', 'out.get()'])) | |
280 .Append(' return scoped_ptr<%s>();' % classname) | 294 .Append(' return scoped_ptr<%s>();' % classname) |
281 .Append(' return out.Pass();') | 295 .Append(' return out.Pass();') |
282 .Append('}') | 296 .Append('}') |
283 ) | 297 ) |
284 return c | 298 return c |
285 | 299 |
286 def _GenerateTypeToValue(self, cpp_namespace, type_): | 300 def _GenerateTypeToValue(self, cpp_namespace, type_): |
287 """Generates a function that serializes the type into a base::Value. | 301 """Generates a function that serializes the type into a base::Value. |
288 E.g. for type "Foo" generates Foo::ToValue() | 302 E.g. for type "Foo" generates Foo::ToValue() |
289 """ | 303 """ |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 def _GenerateParamsCheck(self, function, var): | 485 def _GenerateParamsCheck(self, function, var): |
472 """Generates a check for the correct number of arguments when creating | 486 """Generates a check for the correct number of arguments when creating |
473 Params. | 487 Params. |
474 """ | 488 """ |
475 c = Code() | 489 c = Code() |
476 num_required = 0 | 490 num_required = 0 |
477 for param in function.params: | 491 for param in function.params: |
478 if not param.optional: | 492 if not param.optional: |
479 num_required += 1 | 493 num_required += 1 |
480 if num_required == len(function.params): | 494 if num_required == len(function.params): |
481 c.Append('if (%(var)s.GetSize() != %(total)d)') | 495 c.Append('if (%(var)s.GetSize() != %(total)d) {') |
482 elif not num_required: | 496 elif not num_required: |
483 c.Append('if (%(var)s.GetSize() > %(total)d)') | 497 c.Append('if (%(var)s.GetSize() > %(total)d) {') |
484 else: | 498 else: |
485 c.Append('if (%(var)s.GetSize() < %(required)d' | 499 c.Append('if (%(var)s.GetSize() < %(required)d' |
486 ' || %(var)s.GetSize() > %(total)d)') | 500 ' || %(var)s.GetSize() > %(total)d) {') |
501 c.Concat(self._GenerateError( | |
502 '"expected %%(total)d arguments, got " + %%(var)s.GetSize()', | |
503 True)) | |
487 c.Append(' return scoped_ptr<Params>();') | 504 c.Append(' return scoped_ptr<Params>();') |
505 c.Append('}') | |
488 c.Substitute({ | 506 c.Substitute({ |
489 'var': var, | 507 'var': var, |
490 'required': num_required, | 508 'required': num_required, |
491 'total': len(function.params), | 509 'total': len(function.params), |
492 }) | 510 }) |
493 return c | 511 return c |
494 | 512 |
495 def _GenerateFunctionParamsCreate(self, function): | 513 def _GenerateFunctionParamsCreate(self, function): |
496 """Generate function to create an instance of Params. The generated | 514 """Generate function to create an instance of Params. The generated |
497 function takes a base::ListValue of arguments. | 515 function takes a base::ListValue of arguments. |
498 | 516 |
499 E.g for function "Bar", generate Bar::Params::Create() | 517 E.g for function "Bar", generate Bar::Params::Create() |
500 """ | 518 """ |
501 c = Code() | 519 c = Code() |
502 (c.Append('// static') | 520 (c.Append('// static') |
503 .Sblock('scoped_ptr<Params> ' | 521 .Sblock('scoped_ptr<Params> Params::Create(%s) {' % self._GenerateParams( |
504 'Params::Create(const base::ListValue& args) {') | 522 ['const base::ListValue& args'])) |
505 .Concat(self._GenerateParamsCheck(function, 'args')) | 523 .Concat(self._GenerateParamsCheck(function, 'args')) |
506 .Append('scoped_ptr<Params> params(new Params());') | 524 .Append('scoped_ptr<Params> params(new Params());')) |
507 ) | |
508 | 525 |
509 for param in function.params: | 526 for param in function.params: |
510 c.Concat(self._InitializePropertyToDefault(param, 'params')) | 527 c.Concat(self._InitializePropertyToDefault(param, 'params')) |
511 | 528 |
512 for i, param in enumerate(function.params): | 529 for i, param in enumerate(function.params): |
513 # Any failure will cause this function to return. If any argument is | 530 # Any failure will cause this function to return. If any argument is |
514 # incorrect or missing, those following it are not processed. Note that | 531 # incorrect or missing, those following it are not processed. Note that |
515 # for optional arguments, we allow missing arguments and proceed because | 532 # for optional arguments, we allow missing arguments and proceed because |
516 # there may be other arguments following it. | 533 # there may be other arguments following it. |
517 failure_value = 'scoped_ptr<Params>()' | 534 failure_value = 'scoped_ptr<Params>()' |
518 c.Append() | 535 c.Append() |
519 value_var = param.unix_name + '_value' | 536 value_var = param.unix_name + '_value' |
520 (c.Append('const base::Value* %(value_var)s = NULL;') | 537 (c.Append('const base::Value* %(value_var)s = NULL;') |
521 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') | 538 .Append('if (args.Get(%(i)s, &%(value_var)s) &&') |
522 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') | 539 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {') |
523 .Concat(self._GeneratePopulatePropertyFromValue( | 540 .Concat(self._GeneratePopulatePropertyFromValue( |
524 param, value_var, 'params', failure_value)) | 541 param, value_var, 'params', failure_value)) |
525 .Eblock('}') | 542 .Eblock('}') |
526 ) | 543 ) |
527 if not param.optional: | 544 if not param.optional: |
528 (c.Sblock('else {') | 545 c.Sblock('else {') |
529 .Append('return %s;' % failure_value) | 546 c.Concat(self._GenerateError('"%%(value_var)s is required"')) |
not at google - send to devlin
2013/06/14 21:48:39
ditto, single quotes around the key in here
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
530 .Eblock('}') | 547 (c.Append('return %s;' % failure_value) |
531 ) | 548 .Eblock('}')) |
532 c.Substitute({'value_var': value_var, 'i': i}) | 549 c.Substitute({'value_var': value_var, 'i': i}) |
533 (c.Append() | 550 (c.Append() |
534 .Append('return params.Pass();') | 551 .Append('return params.Pass();') |
535 .Eblock('}') | 552 .Eblock('}') |
536 .Append() | 553 .Append() |
537 ) | 554 ) |
538 | 555 |
539 return c | 556 return c |
540 | 557 |
541 def _GeneratePopulatePropertyFromValue(self, | 558 def _GeneratePopulatePropertyFromValue(self, |
(...skipping 24 matching lines...) Expand all Loading... | |
566 |failure_value|. | 583 |failure_value|. |
567 """ | 584 """ |
568 c = Code() | 585 c = Code() |
569 c.Sblock('{') | 586 c.Sblock('{') |
570 | 587 |
571 underlying_type = self._type_helper.FollowRef(type_) | 588 underlying_type = self._type_helper.FollowRef(type_) |
572 | 589 |
573 if underlying_type.property_type.is_fundamental: | 590 if underlying_type.property_type.is_fundamental: |
574 if is_ptr: | 591 if is_ptr: |
575 (c.Append('%(cpp_type)s temp;') | 592 (c.Append('%(cpp_type)s temp;') |
576 .Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 593 .Append('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
577 self._type_helper.FollowRef(type_), src_var, '&temp')) | 594 self._type_helper.FollowRef(type_), src_var, '&temp'))) |
578 .Append(' return %(failure_value)s;') | 595 c.Concat(self._GenerateError('"\'%s\': expected %s, got " + ' |
not at google - send to devlin
2013/06/14 21:48:39
ditto, and the inside-parens thing all through her
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
596 'json_schema_compiler::util::ValueTypeToString(%s->GetType())' % | |
597 (src_var, self._type_helper.GetCppType(type_), src_var), True)) | |
not at google - send to devlin
2013/06/14 21:48:39
src_var isn't right, that's a c++ variable name. w
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
598 (c.Append(' return %(failure_value)s;') | |
599 .Append('}') | |
579 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') | 600 .Append('%(dst_var)s.reset(new %(cpp_type)s(temp));') |
580 ) | 601 ) |
581 else: | 602 else: |
582 (c.Append('if (!%s)' % cpp_util.GetAsFundamentalValue( | 603 (c.Append('if (!%s) {' % cpp_util.GetAsFundamentalValue( |
583 self._type_helper.FollowRef(type_), | 604 self._type_helper.FollowRef(type_), |
584 src_var, | 605 src_var, |
585 '&%s' % dst_var)) | 606 '&%s' % dst_var))) |
586 .Append(' return %(failure_value)s;') | 607 c.Concat(self._GenerateError('"\'%s\': expected %s, got " + ' |
608 'json_schema_compiler::util::ValueTypeToString(%s->GetType())' % | |
609 (src_var, self._type_helper.GetCppType(type_), src_var), True)) | |
610 (c.Append(' return %(failure_value)s;') | |
611 .Append('}') | |
587 ) | 612 ) |
588 elif underlying_type.property_type == PropertyType.OBJECT: | 613 elif underlying_type.property_type == PropertyType.OBJECT: |
589 if is_ptr: | 614 if is_ptr: |
590 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 615 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
591 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 616 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')) |
617 c.Concat(self._GenerateError( | |
618 '"\'%%(src_var)s\': expected dictionary, got " + ' | |
619 'json_schema_compiler::util::ValueTypeToString(' | |
620 '%%(src_var)s->GetType())', True)) | |
621 (c.Append(' return %(failure_value)s;') | |
622 .Append('}') | |
623 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | |
624 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( | |
625 ['*dictionary', 'temp.get()'])) | |
592 .Append(' return %(failure_value)s;') | 626 .Append(' return %(failure_value)s;') |
593 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 627 .Append('}') |
594 .Append('if (!%(cpp_type)s::Populate(*dictionary, temp.get()))') | |
595 .Append(' return %(failure_value)s;') | |
596 .Append('%(dst_var)s = temp.Pass();') | 628 .Append('%(dst_var)s = temp.Pass();') |
597 ) | 629 ) |
598 else: | 630 else: |
599 (c.Append('const base::DictionaryValue* dictionary = NULL;') | 631 (c.Append('const base::DictionaryValue* dictionary = NULL;') |
600 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary))') | 632 .Append('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')) |
633 c.Concat(self._GenerateError( | |
634 '"\'%%(src_var)s\': expected dictionary, got " + ' | |
635 'json_schema_compiler::util::ValueTypeToString(' | |
636 '%%(src_var)s->GetType())', True)) | |
637 (c.Append(' return %(failure_value)s;') | |
638 .Append('}') | |
639 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs( | |
640 ['*dictionary', '&%(dst_var)s'])) | |
601 .Append(' return %(failure_value)s;') | 641 .Append(' return %(failure_value)s;') |
602 .Append('if (!%(cpp_type)s::Populate(*dictionary, &%(dst_var)s))') | 642 .Append('}') |
603 .Append(' return %(failure_value)s;') | |
604 ) | 643 ) |
605 elif underlying_type.property_type == PropertyType.FUNCTION: | 644 elif underlying_type.property_type == PropertyType.FUNCTION: |
606 if is_ptr: | 645 if is_ptr: |
607 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') | 646 c.Append('%(dst_var)s.reset(new base::DictionaryValue());') |
608 elif underlying_type.property_type == PropertyType.ANY: | 647 elif underlying_type.property_type == PropertyType.ANY: |
609 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') | 648 c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());') |
610 elif underlying_type.property_type == PropertyType.ARRAY: | 649 elif underlying_type.property_type == PropertyType.ARRAY: |
611 # util_cc_helper deals with optional and required arrays | 650 # util_cc_helper deals with optional and required arrays |
612 (c.Append('const base::ListValue* list = NULL;') | 651 (c.Append('const base::ListValue* list = NULL;') |
613 .Append('if (!%(src_var)s->GetAsList(&list))') | 652 .Append('if (!%(src_var)s->GetAsList(&list)) {')) |
614 .Append(' return %(failure_value)s;')) | 653 c.Concat(self._GenerateError( |
654 '"\'%%(src_var)s\': expected list, got " + ' | |
655 'json_schema_compiler::util::ValueTypeToString(' | |
656 '%%(src_var)s->GetType())', True)) | |
657 (c.Append(' return %(failure_value)s;') | |
658 .Append('}')) | |
615 item_type = underlying_type.item_type | 659 item_type = underlying_type.item_type |
616 if item_type.property_type == PropertyType.ENUM: | 660 if item_type.property_type == PropertyType.ENUM: |
617 c.Concat(self._GenerateListValueToEnumArrayConversion( | 661 c.Concat(self._GenerateListValueToEnumArrayConversion( |
618 item_type, | 662 item_type, |
619 'list', | 663 'list', |
620 dst_var, | 664 dst_var, |
621 failure_value, | 665 failure_value, |
622 is_ptr=is_ptr)) | 666 is_ptr=is_ptr)) |
623 else: | 667 else: |
624 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 668 c.Append('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList( |
625 underlying_type, | 669 underlying_type, |
626 'list', | 670 'list', |
627 dst_var, | 671 dst_var, |
628 is_ptr)) | 672 is_ptr)) |
629 .Append(' return %(failure_value)s;') | 673 c.Concat(self._GenerateError('"unable to populate array"', True)) |
not at google - send to devlin
2013/06/14 21:48:39
key can be known here, right?
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
674 (c.Append(' return %(failure_value)s;') | |
675 .Append('}') | |
630 ) | 676 ) |
631 elif underlying_type.property_type == PropertyType.CHOICES: | 677 elif underlying_type.property_type == PropertyType.CHOICES: |
632 if is_ptr: | 678 if is_ptr: |
633 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') | 679 (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());') |
634 .Append('if (!%(cpp_type)s::Populate(*%(src_var)s, temp.get()))') | 680 .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
681 ['*%(src_var)s', 'temp.get()'])) | |
635 .Append(' return %(failure_value)s;') | 682 .Append(' return %(failure_value)s;') |
636 .Append('%(dst_var)s = temp.Pass();') | 683 .Append('%(dst_var)s = temp.Pass();') |
637 ) | 684 ) |
638 else: | 685 else: |
639 (c.Append('if (!%(cpp_type)s::Populate(*%(src_var)s, &%(dst_var)s))') | 686 c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs( |
640 .Append(' return %(failure_value)s;') | 687 ['*%(src_var)s', '&%(dst_var)s'])) |
641 ) | 688 c.Append(' return %(failure_value)s;') |
642 elif underlying_type.property_type == PropertyType.ENUM: | 689 elif underlying_type.property_type == PropertyType.ENUM: |
643 c.Concat(self._GenerateStringToEnumConversion(type_, | 690 c.Concat(self._GenerateStringToEnumConversion(type_, |
644 src_var, | 691 src_var, |
645 dst_var, | 692 dst_var, |
646 failure_value)) | 693 failure_value)) |
647 elif underlying_type.property_type == PropertyType.BINARY: | 694 elif underlying_type.property_type == PropertyType.BINARY: |
648 (c.Append('if (!%(src_var)s->IsType(%(value_type)s))') | 695 c.Append('if (!%(src_var)s->IsType(%(value_type)s)) {') |
649 .Append(' return %(failure_value)s;') | 696 c.Concat(self._GenerateError( |
697 '"\'%%(src_var)s\': expected %(value_type)s, got " + ' | |
698 'json_schema_compiler::util::ValueTypeToString(' | |
699 '%(src_var)s->GetType())', True)) | |
700 (c.Append(' return %(failure_value)s;') | |
701 .Append('}') | |
650 .Append('const base::BinaryValue* binary_value =') | 702 .Append('const base::BinaryValue* binary_value =') |
651 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') | 703 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);') |
652 ) | 704 ) |
653 if is_ptr: | 705 if is_ptr: |
654 (c.Append('%(dst_var)s.reset(') | 706 (c.Append('%(dst_var)s.reset(') |
655 .Append(' new std::string(binary_value->GetBuffer(),') | 707 .Append(' new std::string(binary_value->GetBuffer(),') |
656 .Append(' binary_value->GetSize()));') | 708 .Append(' binary_value->GetSize()));') |
657 ) | 709 ) |
658 else: | 710 else: |
659 (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),') | 711 (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, | 761 src_var, |
710 dst_var, | 762 dst_var, |
711 failure_value): | 763 failure_value): |
712 """Returns Code that converts a string type in |src_var| to an enum with | 764 """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 | 765 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|. | 766 a valid enum name then the function will return |failure_value|. |
715 """ | 767 """ |
716 c = Code() | 768 c = Code() |
717 enum_as_string = '%s_as_string' % type_.unix_name | 769 enum_as_string = '%s_as_string' % type_.unix_name |
718 (c.Append('std::string %s;' % enum_as_string) | 770 (c.Append('std::string %s;' % enum_as_string) |
719 .Append('if (!%s->GetAsString(&%s))' % (src_var, enum_as_string)) | 771 .Append('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string))) |
720 .Append(' return %s;' % failure_value) | 772 c.Concat(self._GenerateError( |
773 '"\'%s\': expected std::string, got " + ' | |
not at google - send to devlin
2013/06/14 21:48:39
just "string" not "std::string"
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
774 'json_schema_compiler::util::ValueTypeToString(' | |
775 '%s->GetType())' % (src_var, src_var), True)) | |
776 (c.Append(' return %s;' % failure_value) | |
777 .Append('}') | |
721 .Append('%s = Parse%s(%s);' % (dst_var, | 778 .Append('%s = Parse%s(%s);' % (dst_var, |
722 self._type_helper.GetCppType(type_), | 779 self._type_helper.GetCppType(type_), |
723 enum_as_string)) | 780 enum_as_string)) |
724 .Append('if (%s == %s)' % (dst_var, | 781 .Append('if (%s == %s) {' % (dst_var, |
725 self._type_helper.GetEnumNoneValue(type_))) | 782 self._type_helper.GetEnumNoneValue(type_)))) |
726 .Append(' return %s;' % failure_value) | 783 c.Concat(self._GenerateError( |
784 '"got bad enum value from variable \'%s\'"' % dst_var, True)) | |
785 (c.Append(' return %s;' % failure_value) | |
786 .Append('}') | |
727 ) | 787 ) |
728 return c | 788 return c |
729 | 789 |
730 def _GeneratePropertyFunctions(self, namespace, params): | 790 def _GeneratePropertyFunctions(self, namespace, params): |
731 """Generates the member functions for a list of parameters. | 791 """Generates the member functions for a list of parameters. |
732 """ | 792 """ |
733 return self._GenerateTypes(namespace, (param.type_ for param in params)) | 793 return self._GenerateTypes(namespace, (param.type_ for param in params)) |
734 | 794 |
735 def _GenerateTypes(self, namespace, types): | 795 def _GenerateTypes(self, namespace, types): |
736 """Generates the member functions for a list of types. | 796 """Generates the member functions for a list of types. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 """ | 894 """ |
835 c = Code() | 895 c = Code() |
836 underlying_type = self._type_helper.FollowRef(prop.type_) | 896 underlying_type = self._type_helper.FollowRef(prop.type_) |
837 if (underlying_type.property_type == PropertyType.ENUM and | 897 if (underlying_type.property_type == PropertyType.ENUM and |
838 prop.optional): | 898 prop.optional): |
839 c.Append('%s->%s = %s;' % ( | 899 c.Append('%s->%s = %s;' % ( |
840 dst, | 900 dst, |
841 prop.unix_name, | 901 prop.unix_name, |
842 self._type_helper.GetEnumNoneValue(prop.type_))) | 902 self._type_helper.GetEnumNoneValue(prop.type_))) |
843 return c | 903 return c |
904 | |
905 def _GenerateError(self, body, indent = False): | |
906 """Generates an error message pertaining to population failure. | |
907 | |
908 E.g 'expected bool, got int' | |
909 """ | |
910 c = Code() | |
911 if not self._generate_error_messages: | |
912 return c | |
913 padding = ''; | |
914 if indent: | |
915 padding = ' '; | |
916 (c.Append(padding + 'if (error)') | |
917 .Append(padding + ' *error = ' + body + ';')) | |
not at google - send to devlin
2013/06/14 21:48:39
why do you need this padding logic? the caller can
Aaron Jacobs
2013/06/15 00:47:18
Done.
| |
918 return c | |
919 | |
920 def _GenerateParams(self, params): | |
921 """Builds the parameter list for a function, given an array of parameters. | |
922 """ | |
923 if self._generate_error_messages: | |
924 params.append('std::string* error') | |
925 return ', '.join(params) | |
926 | |
927 def _GenerateArgs(self, args): | |
928 """Builds the argument list for a function, given an array of arguments. | |
929 """ | |
930 if self._generate_error_messages: | |
931 args.append('error') | |
932 return ', '.join(args) | |
OLD | NEW |