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