Skip to content

Commit

Permalink
Fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Apr 10, 2022
1 parent de0c060 commit 6f6631e
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 85 deletions.
4 changes: 2 additions & 2 deletions protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class BuilderInfo {
// Repeated, not a message, group, or enum.
void p<T>(int tagNumber, String name, FieldType fieldType,
{String? protoName}) {
assert(!fieldType.isGroup && !fieldType.isMessage && !fieldType.isEnum);
assert(!fieldType.isGroupOrMessage && !fieldType.isEnum);
addRepeated<T>(tagNumber, name, fieldType, getCheckFunction(fieldType),
null, null, null,
protoName: protoName);
Expand All @@ -184,7 +184,7 @@ class BuilderInfo {
List<ProtobufEnum>? enumValues,
ProtobufEnum? defaultEnumValue,
String? protoName}) {
assert(fieldType.isGroup || fieldType.isMessage || fieldType.isEnum);
assert(fieldType.isGroupOrMessage || fieldType.isEnum);
addRepeated<T>(tagNumber, name, fieldType, _checkNotNull, subBuilder,
valueOf, enumValues,
defaultEnumValue: defaultEnumValue, protoName: protoName);
Expand Down
63 changes: 2 additions & 61 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class CodedBufferWriter {

if (fieldType.isMap) {
final keyWireFormat =
_wireTypes[_valueTypeIndex(fieldValue.keyFieldType)];
_baseTypeToWireType[fieldValue.keyFieldType.baseType.index];
final valueWireFormat =
_wireTypes[_valueTypeIndex(fieldValue.valueFieldType)];
_baseTypeToWireType[fieldValue.valueFieldType.baseType.index];

fieldValue.forEach((key, value) {
_writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
Expand Down Expand Up @@ -436,65 +436,6 @@ class CodedBufferWriter {
}
}

/// This function maps a power-of-2 value (2^0 .. 2^31) to a unique value
/// in the 0..31 range.
///
/// For more details see "Using de Bruijn Sequences to Index a 1 in
/// a Computer Word"[1]
///
/// Note: this is guaranteed to work after compilation to JavaScript
/// where multiplication becomes a floating point multiplication.
///
/// [1] http://supertech.csail.mit.edu/papers/debruijn.pdf
int _valueTypeIndex(int powerOf2) {
assert(powerOf2 & (powerOf2 - 1) == 0, '$powerOf2 is not a power of 2');
return ((0x077CB531 * powerOf2) >> 27) & 31;
}

/// Precomputed indices for all FbFieldType._XYZ_BIT values:
///
/// _XYZ_BIT_INDEX = _valueTypeIndex(FbFieldType._XYZ_BIT)
///
static const _BOOL_BIT_INDEX = 14;
static const _BYTES_BIT_INDEX = 29;
static const _STRING_BIT_INDEX = 27;
static const _DOUBLE_BIT_INDEX = 23;
static const _FLOAT_BIT_INDEX = 15;
static const _ENUM_BIT_INDEX = 31;
static const _GROUP_BIT_INDEX = 30;
static const _INT32_BIT_INDEX = 28;
static const _INT64_BIT_INDEX = 25;
static const _SINT32_BIT_INDEX = 18;
static const _SINT64_BIT_INDEX = 5;
static const _UINT32_BIT_INDEX = 11;
static const _UINT64_BIT_INDEX = 22;
static const _FIXED32_BIT_INDEX = 13;
static const _FIXED64_BIT_INDEX = 26;
static const _SFIXED32_BIT_INDEX = 21;
static const _SFIXED64_BIT_INDEX = 10;
static const _MESSAGE_BIT_INDEX = 20;

/// Mapping from value types to wire-types indexed by _valueTypeIndex(...).
static final Uint8List _wireTypes = Uint8List(32)
..[_BOOL_BIT_INDEX] = WIRETYPE_VARINT
..[_BYTES_BIT_INDEX] = WIRETYPE_LENGTH_DELIMITED
..[_STRING_BIT_INDEX] = WIRETYPE_LENGTH_DELIMITED
..[_DOUBLE_BIT_INDEX] = WIRETYPE_FIXED64
..[_FLOAT_BIT_INDEX] = WIRETYPE_FIXED32
..[_ENUM_BIT_INDEX] = WIRETYPE_VARINT
..[_GROUP_BIT_INDEX] = WIRETYPE_START_GROUP
..[_INT32_BIT_INDEX] = WIRETYPE_VARINT
..[_INT64_BIT_INDEX] = WIRETYPE_VARINT
..[_SINT32_BIT_INDEX] = WIRETYPE_VARINT
..[_SINT64_BIT_INDEX] = WIRETYPE_VARINT
..[_UINT32_BIT_INDEX] = WIRETYPE_VARINT
..[_UINT64_BIT_INDEX] = WIRETYPE_VARINT
..[_FIXED32_BIT_INDEX] = WIRETYPE_FIXED32
..[_FIXED64_BIT_INDEX] = WIRETYPE_FIXED64
..[_SFIXED32_BIT_INDEX] = WIRETYPE_FIXED32
..[_SFIXED64_BIT_INDEX] = WIRETYPE_FIXED64
..[_MESSAGE_BIT_INDEX] = WIRETYPE_LENGTH_DELIMITED;

static final List<int> _baseTypeToWireType = [
WIRETYPE_VARINT, // 0: bool
WIRETYPE_LENGTH_DELIMITED, // 1: bytes
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/extension_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ T _reparseMessage<T extends GeneratedMessage>(
} else if (field is MapFieldInfo) {
final messageMap = message._fieldSet._values[field.index!];
if (messageMap == null) continue;
if (field.valueFieldType!.isGroup || field.valueFieldType!.isMessage) {
if (field.valueFieldType!.isGroupOrMessage) {
for (var key in messageMap.keys) {
final GeneratedMessage value = messageMap[key];
final reparsedValue = _reparseMessage(value, extensionRegistry);
Expand Down
4 changes: 1 addition & 3 deletions protobuf/lib/src/protobuf/field_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ CheckFunc getCheckFunction(FieldType fieldType) {
case FieldBaseType.enum_:
case FieldBaseType.group:
case FieldBaseType.message:
case FieldBaseType.map:
case FieldBaseType.int64:
case FieldBaseType.sint64:
case FieldBaseType.sfixed64:
Expand All @@ -109,9 +110,6 @@ CheckFunc getCheckFunction(FieldType fieldType) {
case FieldBaseType.uint32:
case FieldBaseType.fixed32:
return _checkUnsigned32;

case FieldBaseType.map:
throw ArgumentError('check function not implemented: $fieldType');
}
}

Expand Down
8 changes: 3 additions & 5 deletions protobuf/lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ class FieldInfo<T> {
: makeDefault = findMakeDefault(type, defaultOrMaker),
check = null,
protoName = protoName ?? _unCamelCase(name),
assert((!type.isGroup && !type.isMessage) ||
subBuilder != null ||
type.isMap),
assert(!type.isGroupOrMessage || subBuilder != null || type.isMap),
assert(!type.isEnum || valueOf != null);

// Represents a field that has been removed by a program transformation.
Expand Down Expand Up @@ -141,7 +139,7 @@ class FieldInfo<T> {
/// That is, it doesn't contain any required fields that aren't initialized.
bool _hasRequiredValues(value) {
if (value == null) return !isRequired; // missing is okay if optional
if (!type.isGroup && !type.isMessage) return true; // primitive and present
if (!type.isGroupOrMessage) return true; // primitive and present

if (!isRepeated) {
// A required message: recurse.
Expand All @@ -164,7 +162,7 @@ class FieldInfo<T> {
void _appendInvalidFields(List<String> problems, value, String prefix) {
if (value == null) {
if (isRequired) problems.add('$prefix$name');
} else if (!type.isGroup && !type.isMessage) {
} else if (!type.isGroupOrMessage) {
// primitive and present
} else if (!isRepeated) {
// Required message/group: recurse.
Expand Down
4 changes: 2 additions & 2 deletions protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,11 @@ class _FieldSet {
fi = otherFi;
}

var mustClone = otherFi.type.isGroup || otherFi.type.isMessage;
var mustClone = otherFi.type.isGroupOrMessage;

if (fi!.isMapField) {
var f = fi as MapFieldInfo<dynamic, dynamic>;
mustClone = f.valueFieldType!.isGroup || f.valueFieldType!.isMessage;
mustClone = f.valueFieldType!.isGroupOrMessage;
var map = f._ensureMapField(meta, this) as PbMap<dynamic, dynamic>;
if (mustClone) {
for (MapEntry entry in fieldValue.entries) {
Expand Down
1 change: 0 additions & 1 deletion protobuf/lib/src/protobuf/field_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ enum FieldBaseType {
group,
}

/// Defines constants and functions for dealing with fieldType bits.
class PbFieldType {
static MakeDefaultFunc? _defaultForType(FieldType type) {
switch (type.baseType) {
Expand Down
8 changes: 2 additions & 6 deletions protobuf/lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ Map<String, dynamic> _writeToJsonMap(_FieldSet fs) {

case FieldBaseType.group:
case FieldBaseType.message:
return fieldValue.writeToJsonMap();

case FieldBaseType.map:
throw 'Unknown type $FieldBaseType.map';
return fieldValue.writeToJsonMap();
}
}

Expand Down Expand Up @@ -271,6 +269,7 @@ dynamic _convertJsonValue(BuilderInfo meta, _FieldSet fs, value, int tagNumber,

case FieldBaseType.group:
case FieldBaseType.message:
case FieldBaseType.map:
if (value is Map) {
final messageValue = value as Map<String, dynamic>;
var subMessage = meta._makeEmptyMessage(tagNumber, registry);
Expand All @@ -279,9 +278,6 @@ dynamic _convertJsonValue(BuilderInfo meta, _FieldSet fs, value, int tagNumber,
}
expectedType = 'nested message or group';
break;

case FieldBaseType.map:
throw ArgumentError('Unknown type $fieldType');
}
throw ArgumentError('Expected type $expectedType, got $value');
}
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/pb_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class PbMap<K, V> extends MapBase<K, V> {

PbMap freeze() {
_isReadonly = true;
if (valueFieldType!.isGroup || valueFieldType!.isMessage) {
if (valueFieldType!.isGroupOrMessage) {
for (var subMessage in values as Iterable<GeneratedMessage>) {
subMessage.freeze();
}
Expand Down
4 changes: 1 addition & 3 deletions protobuf/lib/src/protobuf/proto3_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,10 @@ void _mergeFromProto3Json(

case FieldBaseType.group:
case FieldBaseType.message:
case FieldBaseType.map:
var subMessage = fieldInfo.subBuilder!();
recursionHelper(value, subMessage._fieldSet);
return subMessage;

case FieldBaseType.map:
throw StateError('Unknown type $fieldType');
}
}

Expand Down

0 comments on commit 6f6631e

Please sign in to comment.