Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class BuilderInfo {
int tagNumber,
String name,
int fieldType,
CheckFunc<T> check,
CheckFunc<T>? check,
CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
List<ProtobufEnum>? enumValues, {
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Extension<T> extends FieldInfo<T> {
String name,
int tagNumber,
int fieldType, {
required CheckFunc<T> check,
required CheckFunc<T>? check,
CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
List<ProtobufEnum>? enumValues,
Expand Down
6 changes: 3 additions & 3 deletions protobuf/lib/src/protobuf/field_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions protobuf/lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class FieldInfo<T> {
this.tagNumber,
this.index,
this.type,
CheckFunc<T> this.check,
this.check,
this.subBuilder, {
this.valueOf,
this.enumValues,
Expand Down Expand Up @@ -232,13 +232,13 @@ class FieldInfo<T> {
/// Creates a repeated field.
PbList<T> _createRepeatedField() {
assert(isRepeated);
return PbList<T>(check: check!);
return PbList<T>(check: check);
}

/// Same as above, but allow a tighter typed [PbList] to be created.
PbList<S> _createRepeatedFieldWithType<S extends T>() {
assert(isRepeated);
return PbList<S>(check: check!);
return PbList<S>(check: check);
}

/// Convenience method to thread this FieldInfo's reified type parameter to
Expand Down
42 changes: 29 additions & 13 deletions protobuf/lib/src/protobuf/pb_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ class PbList<E> extends ListBase<E> {
/// We can't use `const []` as it makes the `_wrappedList` field polymorphic.
static final _emptyList = <Never>[];

final CheckFunc<E> _check;
final CheckFunc<E>? _check;

bool _isReadOnly = false;

bool get isFrozen => _isReadOnly;

PbList({CheckFunc<E> check = checkNotNull})
: _wrappedList = <E>[],
_check = check;
PbList({CheckFunc<E>? check}) : _wrappedList = <E>[], _check = check;

PbList.unmodifiable()
: _wrappedList = _emptyList,
Expand All @@ -52,7 +50,9 @@ class PbList<E> extends ListBase<E> {
@pragma('dart2js:never-inline')
void add(E element) {
_checkModifiable('add');
_check(element);
if (_check != null) {
_check(element);
}
_wrappedList.add(element);
}

Expand All @@ -67,7 +67,9 @@ class PbList<E> extends ListBase<E> {
@pragma('dart2js:never-inline')
void addAll(Iterable<E> iterable) {
_checkModifiable('addAll');
iterable.forEach(_check);
if (_check != null) {
iterable.forEach(_check);
}
_wrappedList.addAll(iterable);
}

Expand Down Expand Up @@ -96,21 +98,27 @@ class PbList<E> extends ListBase<E> {
@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<E> iterable) {
_checkModifiable('insertAll');
iterable.forEach(_check);
if (_check != null) {
iterable.forEach(_check);
}
_wrappedList.insertAll(index, iterable);
}

@override
void setAll(int index, Iterable<E> iterable) {
_checkModifiable('setAll');
iterable.forEach(_check);
if (_check != null) {
iterable.forEach(_check);
}
_wrappedList.setAll(index, iterable);
}

Expand Down Expand Up @@ -149,7 +157,9 @@ class PbList<E> extends ListBase<E> {
_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);
}

Expand All @@ -162,15 +172,19 @@ class PbList<E> extends ListBase<E> {
@override
void fillRange(int start, int end, [E? fill]) {
_checkModifiable('fillRange');
_check(fill);
if (_check != null) {
_check(fill);
}
_wrappedList.fillRange(start, end, fill);
}

@override
void replaceRange(int start, int end, Iterable<E> newContents) {
_checkModifiable('replaceRange');
final values = newContents.toList();
newContents.forEach(_check);
if (_check != null) {
newContents.forEach(_check);
}
_wrappedList.replaceRange(start, end, values);
}

Expand Down Expand Up @@ -202,7 +216,9 @@ class PbList<E> extends ListBase<E> {
@override
void operator []=(int index, E value) {
_checkModifiable('set element');
_check(value);
if (_check != null) {
_check(value);
}
_wrappedList[index] = value;
}

Expand Down