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 'package:analyzer/src/codegen/tools.dart'; | 9 import 'package:analyzer/src/codegen/tools.dart'; |
10 import 'package:html/dom.dart' as dom; | 10 import 'package:html/dom.dart' as dom; |
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
963 } else { | 963 } else { |
964 return new FromJsonSnippet((String jsonPath, String json) { | 964 return new FromJsonSnippet((String jsonPath, String json) { |
965 StringBuffer result = new StringBuffer(); | 965 StringBuffer result = new StringBuffer(); |
966 result.write('jsonDecoder.decodeMap($jsonPath, $json'); | 966 result.write('jsonDecoder.decodeMap($jsonPath, $json'); |
967 if (!keyCode.isIdentity) { | 967 if (!keyCode.isIdentity) { |
968 result.write(', keyDecoder: ${keyCode.asClosure}'); | 968 result.write(', keyDecoder: ${keyCode.asClosure}'); |
969 } | 969 } |
970 if (!valueCode.isIdentity) { | 970 if (!valueCode.isIdentity) { |
971 result.write(', valueDecoder: ${valueCode.asClosure}'); | 971 result.write(', valueDecoder: ${valueCode.asClosure}'); |
972 } | 972 } |
973 result.write(')'); | 973 result.write(') as ${dartType(type)}'); |
974 return result.toString(); | 974 return result.toString(); |
975 }); | 975 }); |
976 } | 976 } |
977 } else if (type is TypeList) { | 977 } else if (type is TypeList) { |
978 FromJsonCode itemCode = fromJsonCode(type.itemType); | 978 FromJsonCode itemCode = fromJsonCode(type.itemType); |
979 if (itemCode.isIdentity) { | 979 if (itemCode.isIdentity) { |
980 return new FromJsonFunction('jsonDecoder.decodeList'); | 980 return new FromJsonFunction('jsonDecoder.decodeList'); |
981 } else { | 981 } else { |
982 return new FromJsonSnippet((String jsonPath, String json) => | 982 return new FromJsonSnippet((String jsonPath, String json) => |
983 'jsonDecoder.decodeList($jsonPath, $json, ${itemCode.asClosure})'); | 983 'jsonDecoder.decodeList($jsonPath, $json, ${itemCode.asClosure}) as ${dartType(type)}'); |
Paul Berry
2016/03/31 18:30:44
Would it be possible to avoid the type cast by mak
| |
984 } | 984 } |
985 } else if (type is TypeUnion) { | 985 } else if (type is TypeUnion) { |
986 List<String> decoders = <String>[]; | 986 List<String> decoders = <String>[]; |
987 for (TypeDecl choice in type.choices) { | 987 for (TypeDecl choice in type.choices) { |
988 TypeDecl resolvedChoice = resolveTypeReferenceChain(choice); | 988 TypeDecl resolvedChoice = resolveTypeReferenceChain(choice); |
989 if (resolvedChoice is TypeObject) { | 989 if (resolvedChoice is TypeObject) { |
990 TypeObjectField field = resolvedChoice.getField(type.field); | 990 TypeObjectField field = resolvedChoice.getField(type.field); |
991 if (field == null) { | 991 if (field == null) { |
992 throw new Exception( | 992 throw new Exception( |
993 'Each choice in the union needs a field named ${type.field}'); | 993 'Each choice in the union needs a field named ${type.field}'); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1233 | 1233 |
1234 @override | 1234 @override |
1235 String get asClosure => '($type value) => ${callback('value')}'; | 1235 String get asClosure => '($type value) => ${callback('value')}'; |
1236 | 1236 |
1237 @override | 1237 @override |
1238 bool get isIdentity => false; | 1238 bool get isIdentity => false; |
1239 | 1239 |
1240 @override | 1240 @override |
1241 String asSnippet(String value) => callback(value); | 1241 String asSnippet(String value) => callback(value); |
1242 } | 1242 } |
OLD | NEW |