Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: lib/src/protobuf/field_set.dart

Issue 1844293003: Add generic types for strong mode (Closed) Base URL: git@github.com:dart-lang/dart-protobuf.git@master
Patch Set: oops, change all callers to use fi._createRepeatedField Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 part of protobuf; 5 part of protobuf;
6 6
7 final _emptyList = new List.unmodifiable([]); 7 final _emptyList = new List.unmodifiable([]);
8 8
9 /// All the data in a GeneratedMessage. 9 /// All the data in a GeneratedMessage.
10 /// 10 ///
(...skipping 14 matching lines...) Expand all
25 _ExtensionFieldSet _extensions; 25 _ExtensionFieldSet _extensions;
26 26
27 /// Contains all the unknown fields, or null if there aren't any. 27 /// Contains all the unknown fields, or null if there aren't any.
28 UnknownFieldSet _unknownFields; 28 UnknownFieldSet _unknownFields;
29 29
30 _FieldSet(this._message, BuilderInfo meta, this._eventPlugin) 30 _FieldSet(this._message, BuilderInfo meta, this._eventPlugin)
31 : this._meta = meta, 31 : this._meta = meta,
32 _values = _makeValueList(meta.fieldInfo); 32 _values = _makeValueList(meta.fieldInfo);
33 33
34 static _makeValueList(Map<int, FieldInfo> infos) { 34 static _makeValueList(Map<int, FieldInfo> infos) {
35 if (infos.isEmpty) return _emptyList; 35 if (infos.isEmpty) return _emptyList;
36 return new List(infos.length); 36 return new List(infos.length);
37 } 37 }
38 38
39 // Metadata about multiple fields 39 // Metadata about multiple fields
40 40
41 String get _messageName => _meta.messageName; 41 String get _messageName => _meta.messageName;
42 bool get _isReadOnly => _message._isReadOnly; 42 bool get _isReadOnly => _message._isReadOnly;
43 bool get _hasRequiredFields => _meta.hasRequiredFields; 43 bool get _hasRequiredFields => _meta.hasRequiredFields;
44 44
45 /// The FieldInfo for each non-extension field. 45 /// The FieldInfo for each non-extension field.
46 Iterable<FieldInfo> get _infos => _meta.fieldInfo.values; 46 Iterable<FieldInfo> get _infos => _meta.fieldInfo.values;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } 103 }
104 if (_hasExtensions) { 104 if (_hasExtensions) {
105 var fi = _extensions._getInfoOrNull(tagNumber); 105 var fi = _extensions._getInfoOrNull(tagNumber);
106 if (fi != null) { 106 if (fi != null) {
107 return _extensions._getFieldOrDefault(fi); 107 return _extensions._getFieldOrDefault(fi);
108 } 108 }
109 } 109 }
110 throw new ArgumentError("tag $tagNumber not defined in $_messageName"); 110 throw new ArgumentError("tag $tagNumber not defined in $_messageName");
111 } 111 }
112 112
113 _getDefault(FieldInfo fi) { 113 /*T*/ _getDefault/*<T>*/(FieldInfo/*<T>*/ fi) {
114 if (!fi.isRepeated) return fi.makeDefault(); 114 if (!fi.isRepeated) return fi.makeDefault();
115 if (_isReadOnly) return _emptyList; 115 if (_isReadOnly) return _emptyList;
116 116
117 // TODO(skybrian) we could avoid this by generating another 117 // TODO(skybrian) we could avoid this by generating another
118 // method for repeated fields: 118 // method for repeated fields:
119 // msg.mutableFoo().add(123); 119 // msg.mutableFoo().add(123);
120 var value = _message.createRepeatedField(fi.tagNumber, fi); 120 var value = fi._createRepeatedField(_message);
121 _setNonExtensionFieldUnchecked(fi, value); 121 _setNonExtensionFieldUnchecked(fi, value);
122 return value; 122 return value;
123 } 123 }
124 124
125 _getFieldOrNullByTag(int tagNumber) { 125 _getFieldOrNullByTag(int tagNumber) {
126 var fi = _getFieldInfoOrNull(tagNumber); 126 var fi = _getFieldInfoOrNull(tagNumber);
127 if (fi == null) return null; 127 if (fi == null) return null;
128 return _getFieldOrNull(fi); 128 return _getFieldOrNull(fi);
129 } 129 }
130 130
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 _setNonExtensionFieldUnchecked(fi, value); 205 _setNonExtensionFieldUnchecked(fi, value);
206 } 206 }
207 } 207 }
208 208
209 /// Returns the list to use for adding to a repeated field. 209 /// Returns the list to use for adding to a repeated field.
210 /// 210 ///
211 /// Works for both extended and non-extended fields. 211 /// Works for both extended and non-extended fields.
212 /// Creates and stores the repeated field if it doesn't exist. 212 /// Creates and stores the repeated field if it doesn't exist.
213 /// If it's an extension and the list doesn't exist, validates and stores it. 213 /// If it's an extension and the list doesn't exist, validates and stores it.
214 /// Suitable for decoders. 214 /// Suitable for decoders.
215 List _ensureRepeatedField(FieldInfo fi) { 215 List/*<T>*/ _ensureRepeatedField/*<T>*/(FieldInfo/*<T>*/ fi) {
216 assert(!_isReadOnly); 216 assert(!_isReadOnly);
217 assert(fi.isRepeated); 217 assert(fi.isRepeated);
218 if (fi.index == null) { 218 if (fi.index == null) {
219 return _ensureExtensions()._ensureRepeatedField(fi); 219 return _ensureExtensions()._ensureRepeatedField(fi);
220 } 220 }
221 var value = _getFieldOrNull(fi); 221 var value = _getFieldOrNull(fi);
222 if (value != null) return value; 222 if (value != null) return value as List/*<T>*/;
Søren Gjesse 2016/04/01 06:24:28 This is more a strong mode question. Why is this
Leaf 2016/04/01 17:09:56 We (strong mode) warn on categories of casts that
skybrian 2016/04/01 18:01:40 The return type of _getFieldOrNull is dynamic. I t
223 223
224 value = _message.createRepeatedField(fi.tagNumber, fi); 224 var newValue = fi._createRepeatedField(_message);
225 _setNonExtensionFieldUnchecked(fi, value); 225 _setNonExtensionFieldUnchecked(fi, newValue);
226 return value; 226 return newValue;
227 } 227 }
228 228
229 /// Sets a non-extended field and fires events. 229 /// Sets a non-extended field and fires events.
230 void _setNonExtensionFieldUnchecked(FieldInfo fi, value) { 230 void _setNonExtensionFieldUnchecked(FieldInfo fi, value) {
231 if (_hasObservers) { 231 if (_hasObservers) {
232 _eventPlugin.beforeSetField(fi, value); 232 _eventPlugin.beforeSetField(fi, value);
233 } 233 }
234 _values[fi.index] = value; 234 _values[fi.index] = value;
235 } 235 }
236 236
237 // Generated method implementations 237 // Generated method implementations
238 238
239 /// The implementation of a generated getter. 239 /// The implementation of a generated getter.
240 _$get(int index, int tagNumber, defaultValue) { 240 /*T*/ _$get/*<T>*/(int index, int tagNumber, /*T*/ defaultValue) {
241 assert(_nonExtensionInfo(tagNumber).index == index); 241 assert(_nonExtensionInfo(tagNumber).index == index);
242 var value = _values[index]; 242 var value = _values[index];
243 if (value != null) return value; 243 if (value != null) return value;
244 if (defaultValue != null) return defaultValue; 244 if (defaultValue != null) return defaultValue;
245 return _getDefault(_nonExtensionInfo(tagNumber)); 245 return _getDefault(_nonExtensionInfo(tagNumber));
246 } 246 }
247 247
248 /// The implementation of a generated has method. 248 /// The implementation of a generated has method.
249 bool _$has(int index, int tagNumber) { 249 bool _$has(int index, int tagNumber) {
250 assert(_nonExtensionInfo(tagNumber).index == index); 250 assert(_nonExtensionInfo(tagNumber).index == index);
251 var value = _values[index]; 251 var value = _values[index];
252 if (value == null) return false; 252 if (value == null) return false;
253 if (value is List) return value.isNotEmpty; 253 if (value is List) return value.isNotEmpty;
254 return true; 254 return true;
255 } 255 }
256 256
257 /// The implementation of a generated setter. 257 /// The implementation of a generated setter.
258 /// 258 ///
259 /// In production, does no validation other than a null check. 259 /// In production, does no validation other than a null check.
260 /// Only handles non-repeated, non-extension fields. 260 /// Only handles non-repeated, non-extension fields.
261 /// Also, doesn't handle enums or messages which need per-type validation. 261 /// Also, doesn't handle enums or messages which need per-type validation.
262 void _$set(int index, int tagNumber, value) { 262 void _$set(int index, int tagNumber, value) {
263 assert(_nonExtensionInfo(tagNumber).index == index); 263 assert(_nonExtensionInfo(tagNumber).index == index);
264 assert(!_nonExtensionInfo(tagNumber).isRepeated); 264 assert(!_nonExtensionInfo(tagNumber).isRepeated);
265 assert(_$check(tagNumber, value)); 265 assert(_$check(tagNumber, value));
266 if (_isReadOnly) { 266 if (_isReadOnly) {
267 throw new UnsupportedError( 267 throw new UnsupportedError(
268 "attempted to call a setter on a read-only message ($_messageName)"); 268 "attempted to call a setter on a read-only message ($_messageName)");
269 } 269 }
270 if (value == null) { 270 if (value == null) {
271 _$check(tagNumber, value); // throw exception for null value 271 _$check(tagNumber, value); // throw exception for null value
272 } 272 }
273 if (_hasObservers) { 273 if (_hasObservers) {
274 _eventPlugin.beforeSetField(_nonExtensionInfo(tagNumber), value); 274 _eventPlugin.beforeSetField(_nonExtensionInfo(tagNumber), value);
275 } 275 }
276 _values[index] = value; 276 _values[index] = value;
277 } 277 }
278 278
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 void _appendInvalidFields(List<String> problems, String prefix) { 511 void _appendInvalidFields(List<String> problems, String prefix) {
512 if (!_hasRequiredFields) return; 512 if (!_hasRequiredFields) return;
513 for (var fi in _infos) { 513 for (var fi in _infos) {
514 var value = _values[fi.index]; 514 var value = _values[fi.index];
515 fi._appendInvalidFields(problems, value, prefix); 515 fi._appendInvalidFields(problems, value, prefix);
516 } 516 }
517 // TODO(skybrian): search extensions as well 517 // TODO(skybrian): search extensions as well
518 // https://github.com/dart-lang/dart-protobuf/issues/46 518 // https://github.com/dart-lang/dart-protobuf/issues/46
519 } 519 }
520 } 520 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698