| 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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 void emitObjectClass(String className, TypeObject type, ImpliedType | 223 void emitObjectClass(String className, TypeObject type, ImpliedType |
| 224 impliedType) { | 224 impliedType) { |
| 225 docComment(toHtmlVisitor.collectHtml(() { | 225 docComment(toHtmlVisitor.collectHtml(() { |
| 226 toHtmlVisitor.p(() { | 226 toHtmlVisitor.p(() { |
| 227 toHtmlVisitor.write(impliedType.humanReadableName); | 227 toHtmlVisitor.write(impliedType.humanReadableName); |
| 228 }); | 228 }); |
| 229 if (impliedType.type != null) { | 229 if (impliedType.type != null) { |
| 230 toHtmlVisitor.showType(null, impliedType.type); | 230 toHtmlVisitor.showType(null, impliedType.type); |
| 231 } | 231 } |
| 232 })); | 232 })); |
| 233 writeln('class $className {'); | 233 writeln('class $className implements HasToJson {'); |
| 234 indent(() { | 234 indent(() { |
| 235 for (TypeObjectField field in type.fields) { | 235 for (TypeObjectField field in type.fields) { |
| 236 if (field.value != null) { | 236 if (field.value != null) { |
| 237 continue; | 237 continue; |
| 238 } | 238 } |
| 239 docComment(toHtmlVisitor.collectHtml(() { | 239 docComment(toHtmlVisitor.collectHtml(() { |
| 240 toHtmlVisitor.translateHtml(field.html); | 240 toHtmlVisitor.translateHtml(field.html); |
| 241 })); | 241 })); |
| 242 writeln('final ${dartType(field.type)} ${field.name};'); | 242 writeln('final ${dartType(field.type)} ${field.name};'); |
| 243 writeln(); | 243 writeln(); |
| 244 } | 244 } |
| 245 emitObjectConstructor(type, className); | 245 emitObjectConstructor(type, className); |
| 246 writeln(); | 246 writeln(); |
| 247 emitObjectFromJsonConstructor(className, type, impliedType); | 247 emitObjectFromJsonConstructor(className, type, impliedType); |
| 248 writeln(); | 248 writeln(); |
| 249 if (emitConvenienceConstructor(className, impliedType)) { | 249 if (emitConvenienceConstructor(className, impliedType)) { |
| 250 writeln(); | 250 writeln(); |
| 251 } | 251 } |
| 252 emitToJsonMember(type); | 252 emitToJsonMember(type); |
| 253 writeln(); | 253 writeln(); |
| 254 if (emitToRequestMember(impliedType)) { |
| 255 writeln(); |
| 256 } |
| 254 writeln('@override'); | 257 writeln('@override'); |
| 255 writeln('String toString() => JSON.encode(toJson());'); | 258 writeln('String toString() => JSON.encode(toJson());'); |
| 256 writeln(); | 259 writeln(); |
| 257 emitObjectEqualsMember(type, className); | 260 emitObjectEqualsMember(type, className); |
| 258 writeln(); | 261 writeln(); |
| 259 emitObjectHashCode(type); | 262 emitObjectHashCode(type); |
| 260 }); | 263 }); |
| 261 writeln('}'); | 264 writeln('}'); |
| 262 } | 265 } |
| 263 | 266 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 } else { | 311 } else { |
| 309 writeln(populateField); | 312 writeln(populateField); |
| 310 } | 313 } |
| 311 } | 314 } |
| 312 writeln('return result;'); | 315 writeln('return result;'); |
| 313 }); | 316 }); |
| 314 writeln('}'); | 317 writeln('}'); |
| 315 } | 318 } |
| 316 | 319 |
| 317 /** | 320 /** |
| 321 * Emit the toRequest() code for a class, if appropriate. Returns true if |
| 322 * code was emitted. |
| 323 */ |
| 324 bool emitToRequestMember(ImpliedType impliedType) { |
| 325 if (impliedType.kind == 'requestParams') { |
| 326 writeln('Request toRequest(String id) {'); |
| 327 indent(() { |
| 328 String methodString = literalString((impliedType.apiNode as |
| 329 Request).longMethod); |
| 330 writeln('return new Request(id, $methodString, toJson());'); |
| 331 }); |
| 332 writeln('}'); |
| 333 return true; |
| 334 } |
| 335 return false; |
| 336 } |
| 337 |
| 338 /** |
| 318 * Emit the operator== code for an object class. | 339 * Emit the operator== code for an object class. |
| 319 */ | 340 */ |
| 320 void emitObjectEqualsMember(TypeObject type, String className) { | 341 void emitObjectEqualsMember(TypeObject type, String className) { |
| 321 writeln('@override'); | 342 writeln('@override'); |
| 322 writeln('bool operator==(other) {'); | 343 writeln('bool operator==(other) {'); |
| 323 indent(() { | 344 indent(() { |
| 324 writeln('if (other is $className) {'); | 345 writeln('if (other is $className) {'); |
| 325 indent(() { | 346 indent(() { |
| 326 var comparisons = <String>[]; | 347 var comparisons = <String>[]; |
| 327 for (TypeObjectField field in type.fields) { | 348 for (TypeObjectField field in type.fields) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 ToJsonCode toJsonCode(TypeDecl type) { | 454 ToJsonCode toJsonCode(TypeDecl type) { |
| 434 TypeDecl resolvedType = resolveTypeReferenceChain(type); | 455 TypeDecl resolvedType = resolveTypeReferenceChain(type); |
| 435 if (resolvedType is TypeReference) { | 456 if (resolvedType is TypeReference) { |
| 436 return new ToJsonIdentity(dartType(type)); | 457 return new ToJsonIdentity(dartType(type)); |
| 437 } else if (resolvedType is TypeList) { | 458 } else if (resolvedType is TypeList) { |
| 438 ToJsonCode itemCode = toJsonCode(resolvedType.itemType); | 459 ToJsonCode itemCode = toJsonCode(resolvedType.itemType); |
| 439 if (itemCode.isIdentity) { | 460 if (itemCode.isIdentity) { |
| 440 return new ToJsonIdentity(dartType(type)); | 461 return new ToJsonIdentity(dartType(type)); |
| 441 } else { | 462 } else { |
| 442 return new ToJsonSnippet(dartType(type), (String value) => | 463 return new ToJsonSnippet(dartType(type), (String value) => |
| 443 '$value.map(${itemCode.asClosure})'); | 464 '$value.map(${itemCode.asClosure}).toList()'); |
| 444 } | 465 } |
| 445 } else if (resolvedType is TypeMap) { | 466 } else if (resolvedType is TypeMap) { |
| 446 ToJsonCode keyCode; | 467 ToJsonCode keyCode; |
| 447 if (dartType(resolvedType.keyType) != 'String') { | 468 if (dartType(resolvedType.keyType) != 'String') { |
| 448 keyCode = toJsonCode(resolvedType.keyType); | 469 keyCode = toJsonCode(resolvedType.keyType); |
| 449 } else { | 470 } else { |
| 450 keyCode = new ToJsonIdentity(dartType(resolvedType.keyType)); | 471 keyCode = new ToJsonIdentity(dartType(resolvedType.keyType)); |
| 451 } | 472 } |
| 452 ToJsonCode valueCode = toJsonCode(resolvedType.valueType); | 473 ToJsonCode valueCode = toJsonCode(resolvedType.valueType); |
| 453 if (keyCode.isIdentity && valueCode.isIdentity) { | 474 if (keyCode.isIdentity && valueCode.isIdentity) { |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); | 823 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); |
| 803 return visitor.collectCode(visitor.visitApi); | 824 return visitor.collectCode(visitor.visitApi); |
| 804 }); | 825 }); |
| 805 | 826 |
| 806 /** | 827 /** |
| 807 * Translate spec_input.html into protocol_matchers.dart. | 828 * Translate spec_input.html into protocol_matchers.dart. |
| 808 */ | 829 */ |
| 809 main() { | 830 main() { |
| 810 target.generate(); | 831 target.generate(); |
| 811 } | 832 } |
| OLD | NEW |