| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /** | 5 /** |
| 6 * This file contains code to generate serialization/deserialization logic for | 6 * This file contains code to generate serialization/deserialization logic for |
| 7 * summaries based on an "IDL" description of the summary format (written in | 7 * summaries based on an "IDL" description of the summary format (written in |
| 8 * stylized Dart). | 8 * stylized Dart). |
| 9 * | 9 * |
| 10 * For each class in the "IDL" input, two corresponding classes are generated: | 10 * For each class in the "IDL" input, two corresponding classes are generated: |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 out('${dartType(type)} get $fieldName => _$fieldName$defaultSuffix;'); | 300 out('${dartType(type)} get $fieldName => _$fieldName$defaultSuffix;'); |
| 301 }); | 301 }); |
| 302 }); | 302 }); |
| 303 out('}'); | 303 out('}'); |
| 304 out(); | 304 out(); |
| 305 List<String> builderParams = <String>[]; | 305 List<String> builderParams = <String>[]; |
| 306 out('class ${name}Builder {'); | 306 out('class ${name}Builder {'); |
| 307 indent(() { | 307 indent(() { |
| 308 out('final Map _json = {};'); | 308 out('final Map _json = {};'); |
| 309 out(); | 309 out(); |
| 310 out('bool _finished = false;'); |
| 311 out(); |
| 310 out('${name}Builder(builder.BuilderContext context);'); | 312 out('${name}Builder(builder.BuilderContext context);'); |
| 311 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 313 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 312 out(); | 314 out(); |
| 313 String conversion = '_value'; | 315 String conversion = '_value'; |
| 314 String condition = ''; | 316 String condition = ''; |
| 315 if (type.isList) { | 317 if (type.isList) { |
| 316 if (_idl.classes.containsKey(type.typeName)) { | 318 if (_idl.classes.containsKey(type.typeName)) { |
| 317 conversion = '$conversion.map((b) => b.finish()).toList()'; | 319 conversion = '$conversion.map((b) => b.finish()).toList()'; |
| 318 } else { | 320 } else { |
| 319 conversion = '$conversion.toList()'; | 321 conversion = '$conversion.toList()'; |
| 320 } | 322 } |
| 321 condition = ' || _value.isEmpty'; | 323 condition = ' || _value.isEmpty'; |
| 322 } else if (_idl.enums.containsKey(type.typeName)) { | 324 } else if (_idl.enums.containsKey(type.typeName)) { |
| 323 conversion = '$conversion.index'; | 325 conversion = '$conversion.index'; |
| 324 condition = ' || _value == ${defaultValue(type)}'; | 326 condition = ' || _value == ${defaultValue(type)}'; |
| 325 } else if (_idl.classes.containsKey(type.typeName)) { | 327 } else if (_idl.classes.containsKey(type.typeName)) { |
| 326 conversion = '$conversion.finish()'; | 328 conversion = '$conversion.finish()'; |
| 327 } | 329 } |
| 328 builderParams.add('${encodedType(type)} $fieldName'); | 330 builderParams.add('${encodedType(type)} $fieldName'); |
| 329 out('void set $fieldName(${encodedType(type)} _value) {'); | 331 out('void set $fieldName(${encodedType(type)} _value) {'); |
| 330 indent(() { | 332 indent(() { |
| 333 out('assert(!_finished);'); |
| 331 out('assert(!_json.containsKey(${quoted(fieldName)}));'); | 334 out('assert(!_json.containsKey(${quoted(fieldName)}));'); |
| 332 out('if (_value != null$condition) {'); | 335 out('if (_value != null$condition) {'); |
| 333 indent(() { | 336 indent(() { |
| 334 out('_json[${quoted(fieldName)}] = $conversion;'); | 337 out('_json[${quoted(fieldName)}] = $conversion;'); |
| 335 }); | 338 }); |
| 336 out('}'); | 339 out('}'); |
| 337 }); | 340 }); |
| 338 out('}'); | 341 out('}'); |
| 339 }); | 342 }); |
| 340 if (cls.isTopLevel) { | 343 if (cls.isTopLevel) { |
| 341 out(); | 344 out(); |
| 342 out('List<int> toBuffer() => UTF8.encode(JSON.encode(finish()));'); | 345 out('List<int> toBuffer() => UTF8.encode(JSON.encode(finish()));'); |
| 343 } | 346 } |
| 344 out(); | 347 out(); |
| 345 out('Map finish() => _json;'); | 348 out('Map finish() {'); |
| 349 indent(() { |
| 350 out('assert(!_finished);'); |
| 351 out('_finished = true;'); |
| 352 out('return _json;'); |
| 353 }); |
| 354 out('}'); |
| 346 }); | 355 }); |
| 347 out('}'); | 356 out('}'); |
| 348 out(); | 357 out(); |
| 349 out('${name}Builder encode$name(builder.BuilderContext builderContext, {${
builderParams.join(', ')}}) {'); | 358 out('${name}Builder encode$name(builder.BuilderContext builderContext, {${
builderParams.join(', ')}}) {'); |
| 350 indent(() { | 359 indent(() { |
| 351 out('${name}Builder builder = new ${name}Builder(builderContext);'); | 360 out('${name}Builder builder = new ${name}Builder(builderContext);'); |
| 352 cls.fields.forEach((String fieldName, idlModel.FieldType type) { | 361 cls.fields.forEach((String fieldName, idlModel.FieldType type) { |
| 353 out('builder.$fieldName = $fieldName;'); | 362 out('builder.$fieldName = $fieldName;'); |
| 354 }); | 363 }); |
| 355 out('return builder;'); | 364 out('return builder;'); |
| 356 }); | 365 }); |
| 357 out('}'); | 366 out('}'); |
| 358 out(); | 367 out(); |
| 359 }); | 368 }); |
| 360 } | 369 } |
| 361 | 370 |
| 362 /** | 371 /** |
| 363 * Enclose [s] in quotes, escaping as necessary. | 372 * Enclose [s] in quotes, escaping as necessary. |
| 364 */ | 373 */ |
| 365 String quoted(String s) { | 374 String quoted(String s) { |
| 366 return JSON.encode(s); | 375 return JSON.encode(s); |
| 367 } | 376 } |
| 368 } | 377 } |
| OLD | NEW |