| 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_tools.dart'; | 10 import 'codegen_tools.dart'; |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 void emitObjectClass(String className, TypeObject type, | 284 void emitObjectClass(String className, TypeObject type, |
| 285 ImpliedType impliedType) { | 285 ImpliedType impliedType) { |
| 286 docComment(toHtmlVisitor.collectHtml(() { | 286 docComment(toHtmlVisitor.collectHtml(() { |
| 287 toHtmlVisitor.p(() { | 287 toHtmlVisitor.p(() { |
| 288 toHtmlVisitor.write(impliedType.humanReadableName); | 288 toHtmlVisitor.write(impliedType.humanReadableName); |
| 289 }); | 289 }); |
| 290 if (impliedType.type != null) { | 290 if (impliedType.type != null) { |
| 291 toHtmlVisitor.showType(null, impliedType.type); | 291 toHtmlVisitor.showType(null, impliedType.type); |
| 292 } | 292 } |
| 293 })); | 293 })); |
| 294 writeln('class $className implements HasToJson {'); | 294 write('class $className'); |
| 295 if (impliedType.kind == 'refactoringOptions') { |
| 296 write(' extends RefactoringOptions'); |
| 297 } |
| 298 writeln(' implements HasToJson {'); |
| 295 indent(() { | 299 indent(() { |
| 296 if (emitSpecialStaticMembers(className)) { | 300 if (emitSpecialStaticMembers(className)) { |
| 297 writeln(); | 301 writeln(); |
| 298 } | 302 } |
| 299 for (TypeObjectField field in type.fields) { | 303 for (TypeObjectField field in type.fields) { |
| 300 if (field.value != null) { | 304 if (field.value != null) { |
| 301 continue; | 305 continue; |
| 302 } | 306 } |
| 303 docComment(toHtmlVisitor.collectHtml(() { | 307 docComment(toHtmlVisitor.collectHtml(() { |
| 304 toHtmlVisitor.translateHtml(field.html); | 308 toHtmlVisitor.translateHtml(field.html); |
| (...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 957 "Don't know how to compare for equality: $resolvedType"); | 961 "Don't know how to compare for equality: $resolvedType"); |
| 958 } | 962 } |
| 959 | 963 |
| 960 /** | 964 /** |
| 961 * Emit the method for decoding an object from JSON. | 965 * Emit the method for decoding an object from JSON. |
| 962 */ | 966 */ |
| 963 void emitObjectFromJsonConstructor(String className, TypeObject type, | 967 void emitObjectFromJsonConstructor(String className, TypeObject type, |
| 964 ImpliedType impliedType) { | 968 ImpliedType impliedType) { |
| 965 String humanReadableNameString = | 969 String humanReadableNameString = |
| 966 literalString(impliedType.humanReadableName); | 970 literalString(impliedType.humanReadableName); |
| 971 if (className == 'RefactoringOptions') { |
| 972 writeln('factory RefactoringOptions.fromJson(JsonDecoder jsonDecoder, ' |
| 973 'String jsonPath, Object json, RefactoringKind kind) {'); |
| 974 indent(() { |
| 975 writeln('return _refactoringOptionsFromJson(jsonDecoder, jsonPath, ' |
| 976 'json, kind);'); |
| 977 }); |
| 978 writeln('}'); |
| 979 return; |
| 980 } |
| 967 writeln( | 981 writeln( |
| 968 'factory $className.fromJson(JsonDecoder jsonDecoder, String jsonPath, O
bject json) {'); | 982 'factory $className.fromJson(JsonDecoder jsonDecoder, String jsonPath, O
bject json) {'); |
| 969 indent(() { | 983 indent(() { |
| 970 writeln('if (json == null) {'); | 984 writeln('if (json == null) {'); |
| 971 indent(() { | 985 indent(() { |
| 972 writeln('json = {};'); | 986 writeln('json = {};'); |
| 973 }); | 987 }); |
| 974 writeln('}'); | 988 writeln('}'); |
| 975 writeln('if (json is Map) {'); | 989 writeln('if (json is Map) {'); |
| 976 indent(() { | 990 indent(() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 993 if (isOptionalConstructorArg(className, field)) { | 1007 if (isOptionalConstructorArg(className, field)) { |
| 994 optionalArgs.add('${field.name}: ${field.name}'); | 1008 optionalArgs.add('${field.name}: ${field.name}'); |
| 995 } else { | 1009 } else { |
| 996 args.add(field.name); | 1010 args.add(field.name); |
| 997 } | 1011 } |
| 998 TypeDecl fieldType = field.type; | 1012 TypeDecl fieldType = field.type; |
| 999 String fieldDartType = dartType(fieldType); | 1013 String fieldDartType = dartType(fieldType); |
| 1000 writeln('$fieldDartType ${field.name};'); | 1014 writeln('$fieldDartType ${field.name};'); |
| 1001 writeln('if (json.containsKey($fieldNameString)) {'); | 1015 writeln('if (json.containsKey($fieldNameString)) {'); |
| 1002 indent(() { | 1016 indent(() { |
| 1003 String toJson = | 1017 String fromJson = |
| 1004 fromJsonCode(fieldType).asSnippet(jsonPath, fieldAccessor); | 1018 fromJsonCode(fieldType).asSnippet(jsonPath, fieldAccessor); |
| 1005 writeln('${field.name} = $toJson;'); | 1019 writeln('${field.name} = $fromJson;'); |
| 1006 }); | 1020 }); |
| 1007 write('}'); | 1021 write('}'); |
| 1008 if (!field.optional) { | 1022 if (!field.optional) { |
| 1009 writeln(' else {'); | 1023 writeln(' else {'); |
| 1010 indent(() { | 1024 indent(() { |
| 1011 writeln( | 1025 writeln( |
| 1012 "throw jsonDecoder.missingKey(jsonPath, $fieldNameString);"); | 1026 "throw jsonDecoder.missingKey(jsonPath, $fieldNameString);"); |
| 1013 }); | 1027 }); |
| 1014 writeln('}'); | 1028 writeln('}'); |
| 1015 } else if (fieldType is TypeList) { | 1029 } else if (fieldType is TypeList) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1067 /** | 1081 /** |
| 1068 * Compute the code necessary to translate [type] from JSON. | 1082 * Compute the code necessary to translate [type] from JSON. |
| 1069 */ | 1083 */ |
| 1070 FromJsonCode fromJsonCode(TypeDecl type) { | 1084 FromJsonCode fromJsonCode(TypeDecl type) { |
| 1071 if (type is TypeReference) { | 1085 if (type is TypeReference) { |
| 1072 TypeDefinition referencedDefinition = api.types[type.typeName]; | 1086 TypeDefinition referencedDefinition = api.types[type.typeName]; |
| 1073 if (referencedDefinition != null) { | 1087 if (referencedDefinition != null) { |
| 1074 TypeDecl referencedType = referencedDefinition.type; | 1088 TypeDecl referencedType = referencedDefinition.type; |
| 1075 if (referencedType is TypeObject || referencedType is TypeEnum) { | 1089 if (referencedType is TypeObject || referencedType is TypeEnum) { |
| 1076 return new FromJsonSnippet( | 1090 return new FromJsonSnippet( |
| 1077 (String jsonPath, String json) => | 1091 (String jsonPath, String json) { |
| 1078 'new ${dartType(type)}.fromJson(jsonDecoder, $jsonPath, $json)
'); | 1092 String typeName = dartType(type); |
| 1093 if (typeName == 'RefactoringOptions') { |
| 1094 return 'new $typeName.fromJson(jsonDecoder, $jsonPath, $json,
kind)'; |
| 1095 } else { |
| 1096 return 'new $typeName.fromJson(jsonDecoder, $jsonPath, $json)'
; |
| 1097 } |
| 1098 }); |
| 1079 } else { | 1099 } else { |
| 1080 return fromJsonCode(referencedType); | 1100 return fromJsonCode(referencedType); |
| 1081 } | 1101 } |
| 1082 } else { | 1102 } else { |
| 1083 switch (type.typeName) { | 1103 switch (type.typeName) { |
| 1084 case 'String': | 1104 case 'String': |
| 1085 return new FromJsonFunction('jsonDecoder._decodeString'); | 1105 return new FromJsonFunction('jsonDecoder._decodeString'); |
| 1086 case 'bool': | 1106 case 'bool': |
| 1087 return new FromJsonFunction('jsonDecoder._decodeBool'); | 1107 return new FromJsonFunction('jsonDecoder._decodeBool'); |
| 1088 case 'int': | 1108 case 'int': |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1266 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); | 1286 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); |
| 1267 return visitor.collectCode(visitor.visitApi); | 1287 return visitor.collectCode(visitor.visitApi); |
| 1268 }); | 1288 }); |
| 1269 | 1289 |
| 1270 /** | 1290 /** |
| 1271 * Translate spec_input.html into protocol_matchers.dart. | 1291 * Translate spec_input.html into protocol_matchers.dart. |
| 1272 */ | 1292 */ |
| 1273 main() { | 1293 main() { |
| 1274 target.generate(); | 1294 target.generate(); |
| 1275 } | 1295 } |
| OLD | NEW |