diff --git a/protobuf/lib/src/protobuf/builder_info.dart b/protobuf/lib/src/protobuf/builder_info.dart index 5d1019e7..28997e4c 100644 --- a/protobuf/lib/src/protobuf/builder_info.dart +++ b/protobuf/lib/src/protobuf/builder_info.dart @@ -120,7 +120,7 @@ class BuilderInfo { int tagNumber, String name, int fieldType, - CheckFunc check, + CheckFunc? check, CreateBuilderFunc? subBuilder, ValueOfFunc? valueOf, List? enumValues, { diff --git a/protobuf/lib/src/protobuf/extension.dart b/protobuf/lib/src/protobuf/extension.dart index dc37b91f..1af1529a 100644 --- a/protobuf/lib/src/protobuf/extension.dart +++ b/protobuf/lib/src/protobuf/extension.dart @@ -35,7 +35,7 @@ class Extension extends FieldInfo { String name, int tagNumber, int fieldType, { - required CheckFunc check, + required CheckFunc? check, CreateBuilderFunc? subBuilder, ValueOfFunc? valueOf, List? enumValues, diff --git a/protobuf/lib/src/protobuf/field_error.dart b/protobuf/lib/src/protobuf/field_error.dart index c6d7e40c..a9116023 100644 --- a/protobuf/lib/src/protobuf/field_error.dart +++ b/protobuf/lib/src/protobuf/field_error.dart @@ -72,8 +72,8 @@ String? _getFieldError(int fieldType, var value) { /// unsigned 32 bit ints where there also is a range check. /// /// @nodoc -CheckFunc getCheckFunction(int fieldType) { - switch (fieldType & ~0x7) { +CheckFunc? getCheckFunction(int fieldType) { + switch (PbFieldType.baseType(fieldType)) { case PbFieldType.BOOL_BIT: case PbFieldType.BYTES_BIT: case PbFieldType.STRING_BIT: @@ -89,7 +89,7 @@ CheckFunc getCheckFunction(int fieldType) { // We always use the full range of the same Dart type. // It's up to the caller to treat the Int64 as signed or unsigned. // See: https://github.com/google/protobuf.dart/issues/44 - return checkNotNull; + return null; case PbFieldType.FLOAT_BIT: return _checkFloat; diff --git a/protobuf/lib/src/protobuf/field_info.dart b/protobuf/lib/src/protobuf/field_info.dart index cf5b28cc..775b3c0f 100644 --- a/protobuf/lib/src/protobuf/field_info.dart +++ b/protobuf/lib/src/protobuf/field_info.dart @@ -138,7 +138,7 @@ class FieldInfo { this.tagNumber, this.index, this.type, - CheckFunc this.check, + this.check, this.subBuilder, { this.valueOf, this.enumValues, @@ -232,13 +232,13 @@ class FieldInfo { /// Creates a repeated field. PbList _createRepeatedField() { assert(isRepeated); - return PbList(check: check!); + return PbList(check: check); } /// Same as above, but allow a tighter typed [PbList] to be created. PbList _createRepeatedFieldWithType() { assert(isRepeated); - return PbList(check: check!); + return PbList(check: check); } /// Convenience method to thread this FieldInfo's reified type parameter to diff --git a/protobuf/lib/src/protobuf/pb_list.dart b/protobuf/lib/src/protobuf/pb_list.dart index 4736f6c3..1c2c8ce4 100644 --- a/protobuf/lib/src/protobuf/pb_list.dart +++ b/protobuf/lib/src/protobuf/pb_list.dart @@ -29,15 +29,13 @@ class PbList extends ListBase { /// We can't use `const []` as it makes the `_wrappedList` field polymorphic. static final _emptyList = []; - final CheckFunc _check; + final CheckFunc? _check; bool _isReadOnly = false; bool get isFrozen => _isReadOnly; - PbList({CheckFunc check = checkNotNull}) - : _wrappedList = [], - _check = check; + PbList({CheckFunc? check}) : _wrappedList = [], _check = check; PbList.unmodifiable() : _wrappedList = _emptyList, @@ -52,7 +50,9 @@ class PbList extends ListBase { @pragma('dart2js:never-inline') void add(E element) { _checkModifiable('add'); - _check(element); + if (_check != null) { + _check(element); + } _wrappedList.add(element); } @@ -67,7 +67,9 @@ class PbList extends ListBase { @pragma('dart2js:never-inline') void addAll(Iterable iterable) { _checkModifiable('addAll'); - iterable.forEach(_check); + if (_check != null) { + iterable.forEach(_check); + } _wrappedList.addAll(iterable); } @@ -96,21 +98,27 @@ class PbList extends ListBase { @override void insert(int index, E element) { _checkModifiable('insert'); - _check(element); + if (_check != null) { + _check(element); + } _wrappedList.insert(index, element); } @override void insertAll(int index, Iterable iterable) { _checkModifiable('insertAll'); - iterable.forEach(_check); + if (_check != null) { + iterable.forEach(_check); + } _wrappedList.insertAll(index, iterable); } @override void setAll(int index, Iterable iterable) { _checkModifiable('setAll'); - iterable.forEach(_check); + if (_check != null) { + iterable.forEach(_check); + } _wrappedList.setAll(index, iterable); } @@ -149,7 +157,9 @@ class PbList extends ListBase { _checkModifiable('setRange'); // NOTE: In case `take()` returns less than `end - start` elements, the // _wrappedList will fail with a `StateError`. - iterable.skip(skipCount).take(end - start).forEach(_check); + if (_check != null) { + iterable.skip(skipCount).take(end - start).forEach(_check); + } _wrappedList.setRange(start, end, iterable, skipCount); } @@ -162,7 +172,9 @@ class PbList extends ListBase { @override void fillRange(int start, int end, [E? fill]) { _checkModifiable('fillRange'); - _check(fill); + if (_check != null) { + _check(fill); + } _wrappedList.fillRange(start, end, fill); } @@ -170,7 +182,9 @@ class PbList extends ListBase { void replaceRange(int start, int end, Iterable newContents) { _checkModifiable('replaceRange'); final values = newContents.toList(); - newContents.forEach(_check); + if (_check != null) { + newContents.forEach(_check); + } _wrappedList.replaceRange(start, end, values); } @@ -202,7 +216,9 @@ class PbList extends ListBase { @override void operator []=(int index, E value) { _checkModifiable('set element'); - _check(value); + if (_check != null) { + _check(value); + } _wrappedList[index] = value; }