| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library codegen.protocol; | 5 library codegen.protocol; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'api.dart'; | 9 import 'api.dart'; |
| 10 import 'codegen_dart.dart'; | 10 import 'codegen_dart.dart'; |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 List<String> args = <String>[]; | 613 List<String> args = <String>[]; |
| 614 List<String> optionalArgs = <String>[]; | 614 List<String> optionalArgs = <String>[]; |
| 615 List<CodegenCallback> extraInitCode = <CodegenCallback>[]; | 615 List<CodegenCallback> extraInitCode = <CodegenCallback>[]; |
| 616 for (TypeObjectField field in type.fields) { | 616 for (TypeObjectField field in type.fields) { |
| 617 if (field.value != null) { | 617 if (field.value != null) { |
| 618 continue; | 618 continue; |
| 619 } | 619 } |
| 620 String arg = 'this.${field.name}'; | 620 String arg = 'this.${field.name}'; |
| 621 if (isOptionalConstructorArg(className, field)) { | 621 if (isOptionalConstructorArg(className, field)) { |
| 622 optionalArgs.add(arg); | 622 optionalArgs.add(arg); |
| 623 TypeDecl fieldType = field.type; | 623 if (!field.optional) { |
| 624 if (fieldType is TypeList) { | 624 // Optional constructor arg, but non-optional field. If no arg is |
| 625 extraInitCode.add(() { | 625 // given, the constructor should populate with the empty list. |
| 626 writeln('if (${field.name} == null) {'); | 626 TypeDecl fieldType = field.type; |
| 627 indent(() { | 627 if (fieldType is TypeList) { |
| 628 writeln('${field.name} = <${dartType(fieldType.itemType)}>[];'); | 628 extraInitCode.add(() { |
| 629 writeln('if (${field.name} == null) {'); |
| 630 indent(() { |
| 631 writeln('${field.name} = <${dartType(fieldType.itemType)}>[];'); |
| 632 }); |
| 633 writeln('}'); |
| 629 }); | 634 }); |
| 630 writeln('}'); | 635 } else { |
| 631 }); | 636 throw new Exception( |
| 637 "Don't know how to create default field value."); |
| 638 } |
| 632 } | 639 } |
| 633 } else { | 640 } else { |
| 634 args.add(arg); | 641 args.add(arg); |
| 635 } | 642 } |
| 636 } | 643 } |
| 637 if (optionalArgs.isNotEmpty) { | 644 if (optionalArgs.isNotEmpty) { |
| 638 args.add('{${optionalArgs.join(', ')}}'); | 645 args.add('{${optionalArgs.join(', ')}}'); |
| 639 } | 646 } |
| 640 write('$className(${args.join(', ')})'); | 647 write('$className(${args.join(', ')})'); |
| 641 if (extraInitCode.isEmpty) { | 648 if (extraInitCode.isEmpty) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 writeln('Map<String, dynamic> result = {};'); | 681 writeln('Map<String, dynamic> result = {};'); |
| 675 for (TypeObjectField field in type.fields) { | 682 for (TypeObjectField field in type.fields) { |
| 676 String fieldNameString = literalString(field.name); | 683 String fieldNameString = literalString(field.name); |
| 677 if (field.value != null) { | 684 if (field.value != null) { |
| 678 writeln('result[$fieldNameString] = ${literalString(field.value)};'); | 685 writeln('result[$fieldNameString] = ${literalString(field.value)};'); |
| 679 continue; | 686 continue; |
| 680 } | 687 } |
| 681 String fieldToJson = toJsonCode(field.type).asSnippet(field.name); | 688 String fieldToJson = toJsonCode(field.type).asSnippet(field.name); |
| 682 String populateField = 'result[$fieldNameString] = $fieldToJson;'; | 689 String populateField = 'result[$fieldNameString] = $fieldToJson;'; |
| 683 if (field.optional) { | 690 if (field.optional) { |
| 684 String condition; | 691 writeln('if (${field.name} != null) {'); |
| 685 if (field.type is TypeList) { | |
| 686 condition = '${field.name}.isNotEmpty'; | |
| 687 } else { | |
| 688 condition = '${field.name} != null'; | |
| 689 } | |
| 690 writeln('if ($condition) {'); | |
| 691 indent(() { | 692 indent(() { |
| 692 writeln(populateField); | 693 writeln(populateField); |
| 693 }); | 694 }); |
| 694 writeln('}'); | 695 writeln('}'); |
| 695 } else { | 696 } else { |
| 696 writeln(populateField); | 697 writeln(populateField); |
| 697 } | 698 } |
| 698 } | 699 } |
| 699 writeln('return result;'); | 700 writeln('return result;'); |
| 700 }); | 701 }); |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 writeln('${field.name} = $fromJson;'); | 1043 writeln('${field.name} = $fromJson;'); |
| 1043 }); | 1044 }); |
| 1044 write('}'); | 1045 write('}'); |
| 1045 if (!field.optional) { | 1046 if (!field.optional) { |
| 1046 writeln(' else {'); | 1047 writeln(' else {'); |
| 1047 indent(() { | 1048 indent(() { |
| 1048 writeln( | 1049 writeln( |
| 1049 "throw jsonDecoder.missingKey(jsonPath, $fieldNameString);"); | 1050 "throw jsonDecoder.missingKey(jsonPath, $fieldNameString);"); |
| 1050 }); | 1051 }); |
| 1051 writeln('}'); | 1052 writeln('}'); |
| 1052 } else if (fieldType is TypeList) { | |
| 1053 writeln(' else {'); | |
| 1054 indent(() { | |
| 1055 writeln('${field.name} = <${dartType(fieldType.itemType)}>[];'); | |
| 1056 }); | |
| 1057 writeln('}'); | |
| 1058 } else { | 1053 } else { |
| 1059 writeln(); | 1054 writeln(); |
| 1060 } | 1055 } |
| 1061 } | 1056 } |
| 1062 args.addAll(optionalArgs); | 1057 args.addAll(optionalArgs); |
| 1063 writeln('return new $className(${args.join(', ')});'); | 1058 writeln('return new $className(${args.join(', ')});'); |
| 1064 }); | 1059 }); |
| 1065 writeln('} else {'); | 1060 writeln('} else {'); |
| 1066 indent(() { | 1061 indent(() { |
| 1067 writeln( | 1062 writeln( |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1285 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); | 1280 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); |
| 1286 return visitor.collectCode(visitor.visitApi); | 1281 return visitor.collectCode(visitor.visitApi); |
| 1287 }); | 1282 }); |
| 1288 | 1283 |
| 1289 /** | 1284 /** |
| 1290 * Translate spec_input.html into protocol_matchers.dart. | 1285 * Translate spec_input.html into protocol_matchers.dart. |
| 1291 */ | 1286 */ |
| 1292 main() { | 1287 main() { |
| 1293 target.generate(); | 1288 target.generate(); |
| 1294 } | 1289 } |
| OLD | NEW |