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

Unified Diff: lib/src/protobuf/generated_message.dart

Issue 1277863003: cleanup: move fieldType constants and functions to a separate file (Closed) Base URL: git@github.com:dart-lang/dart-protobuf.git@master
Patch Set: _validateMessage should call _baseType, not callers Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/protobuf/field_type.dart ('k') | lib/src/protobuf/unknown_field_set.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/protobuf/generated_message.dart
diff --git a/lib/src/protobuf/generated_message.dart b/lib/src/protobuf/generated_message.dart
index 9334872cbc5de056228ba1392ae42f4f7faacfbb..9f39778f6dd5206166853f9fa4623ba04105a559 100644
--- a/lib/src/protobuf/generated_message.dart
+++ b/lib/src/protobuf/generated_message.dart
@@ -18,219 +18,90 @@ bool _isFloat32(double value) => value.isNaN || value.isInfinite ||
_inRange(-3.4028234663852886E38, value, 3.4028234663852886E38);
abstract class GeneratedMessage {
- static const int _REQUIRED_BIT = 0x1;
- static const int _REPEATED_BIT = 0x2;
- static const int _PACKED_BIT = 0x4;
-
- static const int _BOOL_BIT = 0x10;
- static const int _BYTES_BIT = 0x20;
- static const int _STRING_BIT = 0x40;
- static const int _DOUBLE_BIT = 0x80;
- static const int _FLOAT_BIT = 0x100;
- static const int _ENUM_BIT = 0x200;
- static const int _GROUP_BIT = 0x400;
- static const int _INT32_BIT = 0x800;
- static const int _INT64_BIT = 0x1000;
- static const int _SINT32_BIT = 0x2000;
- static const int _SINT64_BIT = 0x4000;
- static const int _UINT32_BIT = 0x8000;
- static const int _UINT64_BIT = 0x10000;
- static const int _FIXED32_BIT = 0x20000;
- static const int _FIXED64_BIT = 0x40000;
- static const int _SFIXED32_BIT = 0x80000;
- static const int _SFIXED64_BIT = 0x100000;
- static const int _MESSAGE_BIT = 0x200000;
-
- static const int _OPTIONAL_BOOL = _BOOL_BIT;
- static const int _OPTIONAL_BYTES = _BYTES_BIT;
- static const int _OPTIONAL_STRING = _STRING_BIT;
- static const int _OPTIONAL_FLOAT = _FLOAT_BIT;
- static const int _OPTIONAL_DOUBLE = _DOUBLE_BIT;
- static const int _OPTIONAL_ENUM = _ENUM_BIT;
- static const int _OPTIONAL_GROUP = _GROUP_BIT;
- static const int _OPTIONAL_INT32 = _INT32_BIT;
- static const int _OPTIONAL_INT64 = _INT64_BIT;
- static const int _OPTIONAL_SINT32 = _SINT32_BIT;
- static const int _OPTIONAL_SINT64 = _SINT64_BIT;
- static const int _OPTIONAL_UINT32 = _UINT32_BIT;
- static const int _OPTIONAL_UINT64 = _UINT64_BIT;
- static const int _OPTIONAL_FIXED32 = _FIXED32_BIT;
- static const int _OPTIONAL_FIXED64 = _FIXED64_BIT;
- static const int _OPTIONAL_SFIXED32 = _SFIXED32_BIT;
- static const int _OPTIONAL_SFIXED64 = _SFIXED64_BIT;
- static const int _OPTIONAL_MESSAGE = _MESSAGE_BIT;
-
- static const int _REQUIRED_BOOL = _REQUIRED_BIT | _BOOL_BIT;
- static const int _REQUIRED_BYTES = _REQUIRED_BIT | _BYTES_BIT;
- static const int _REQUIRED_STRING = _REQUIRED_BIT | _STRING_BIT;
- static const int _REQUIRED_FLOAT = _REQUIRED_BIT | _FLOAT_BIT;
- static const int _REQUIRED_DOUBLE = _REQUIRED_BIT | _DOUBLE_BIT;
- static const int _REQUIRED_ENUM = _REQUIRED_BIT | _ENUM_BIT;
- static const int _REQUIRED_GROUP = _REQUIRED_BIT | _GROUP_BIT;
- static const int _REQUIRED_INT32 = _REQUIRED_BIT | _INT32_BIT;
- static const int _REQUIRED_INT64 = _REQUIRED_BIT | _INT64_BIT;
- static const int _REQUIRED_SINT32 = _REQUIRED_BIT | _SINT32_BIT;
- static const int _REQUIRED_SINT64 = _REQUIRED_BIT | _SINT64_BIT;
- static const int _REQUIRED_UINT32 = _REQUIRED_BIT | _UINT32_BIT;
- static const int _REQUIRED_UINT64 = _REQUIRED_BIT | _UINT64_BIT;
- static const int _REQUIRED_FIXED32 = _REQUIRED_BIT | _FIXED32_BIT;
- static const int _REQUIRED_FIXED64 = _REQUIRED_BIT | _FIXED64_BIT;
- static const int _REQUIRED_SFIXED32 = _REQUIRED_BIT | _SFIXED32_BIT;
- static const int _REQUIRED_SFIXED64 = _REQUIRED_BIT | _SFIXED64_BIT;
- static const int _REQUIRED_MESSAGE = _REQUIRED_BIT | _MESSAGE_BIT;
-
- static const int _REPEATED_BOOL = _REPEATED_BIT | _BOOL_BIT;
- static const int _REPEATED_BYTES = _REPEATED_BIT | _BYTES_BIT;
- static const int _REPEATED_STRING = _REPEATED_BIT | _STRING_BIT;
- static const int _REPEATED_FLOAT = _REPEATED_BIT | _FLOAT_BIT;
- static const int _REPEATED_DOUBLE = _REPEATED_BIT | _DOUBLE_BIT;
- static const int _REPEATED_ENUM = _REPEATED_BIT | _ENUM_BIT;
- static const int _REPEATED_GROUP = _REPEATED_BIT | _GROUP_BIT;
- static const int _REPEATED_INT32 = _REPEATED_BIT | _INT32_BIT;
- static const int _REPEATED_INT64 = _REPEATED_BIT | _INT64_BIT;
- static const int _REPEATED_SINT32 = _REPEATED_BIT | _SINT32_BIT;
- static const int _REPEATED_SINT64 = _REPEATED_BIT | _SINT64_BIT;
- static const int _REPEATED_UINT32 = _REPEATED_BIT | _UINT32_BIT;
- static const int _REPEATED_UINT64 = _REPEATED_BIT | _UINT64_BIT;
- static const int _REPEATED_FIXED32 = _REPEATED_BIT | _FIXED32_BIT;
- static const int _REPEATED_FIXED64 = _REPEATED_BIT | _FIXED64_BIT;
- static const int _REPEATED_SFIXED32 = _REPEATED_BIT | _SFIXED32_BIT;
- static const int _REPEATED_SFIXED64 = _REPEATED_BIT | _SFIXED64_BIT;
- static const int _REPEATED_MESSAGE = _REPEATED_BIT | _MESSAGE_BIT;
-
- static const int _PACKED_BOOL = _REPEATED_BIT | _PACKED_BIT | _BOOL_BIT;
- static const int _PACKED_FLOAT = _REPEATED_BIT | _PACKED_BIT | _FLOAT_BIT;
- static const int _PACKED_DOUBLE = _REPEATED_BIT | _PACKED_BIT | _DOUBLE_BIT;
- static const int _PACKED_ENUM = _REPEATED_BIT | _PACKED_BIT | _ENUM_BIT;
- static const int _PACKED_INT32 = _REPEATED_BIT | _PACKED_BIT | _INT32_BIT;
- static const int _PACKED_INT64 = _REPEATED_BIT | _PACKED_BIT | _INT64_BIT;
- static const int _PACKED_SINT32 = _REPEATED_BIT | _PACKED_BIT | _SINT32_BIT;
- static const int _PACKED_SINT64 = _REPEATED_BIT | _PACKED_BIT | _SINT64_BIT;
- static const int _PACKED_UINT32 = _REPEATED_BIT | _PACKED_BIT | _UINT32_BIT;
- static const int _PACKED_UINT64 = _REPEATED_BIT | _PACKED_BIT | _UINT64_BIT;
- static const int _PACKED_FIXED32 = _REPEATED_BIT | _PACKED_BIT | _FIXED32_BIT;
- static const int _PACKED_FIXED64 = _REPEATED_BIT | _PACKED_BIT | _FIXED64_BIT;
- static const int _PACKED_SFIXED32 = _REPEATED_BIT | _PACKED_BIT |
- _SFIXED32_BIT;
- static const int _PACKED_SFIXED64 = _REPEATED_BIT | _PACKED_BIT |
- _SFIXED64_BIT;
// Short names for use in generated code.
+ // TODO(skybrian) change generated code to use FieldType instead.
// _O_ptional.
- static const int OB = _OPTIONAL_BOOL;
- static const int OY = _OPTIONAL_BYTES;
- static const int OS = _OPTIONAL_STRING;
- static const int OF = _OPTIONAL_FLOAT;
- static const int OD = _OPTIONAL_DOUBLE;
- static const int OE = _OPTIONAL_ENUM;
- static const int OG = _OPTIONAL_GROUP;
- static const int O3 = _OPTIONAL_INT32;
- static const int O6 = _OPTIONAL_INT64;
- static const int OS3 = _OPTIONAL_SINT32;
- static const int OS6 = _OPTIONAL_SINT64;
- static const int OU3 = _OPTIONAL_UINT32;
- static const int OU6 = _OPTIONAL_UINT64;
- static const int OF3 = _OPTIONAL_FIXED32;
- static const int OF6 = _OPTIONAL_FIXED64;
- static const int OSF3 = _OPTIONAL_SFIXED32;
- static const int OSF6 = _OPTIONAL_SFIXED64;
- static const int OM = _OPTIONAL_MESSAGE;
+ static const int OB = FieldType._OPTIONAL_BOOL;
+ static const int OY = FieldType._OPTIONAL_BYTES;
+ static const int OS = FieldType._OPTIONAL_STRING;
+ static const int OF = FieldType._OPTIONAL_FLOAT;
+ static const int OD = FieldType._OPTIONAL_DOUBLE;
+ static const int OE = FieldType._OPTIONAL_ENUM;
+ static const int OG = FieldType._OPTIONAL_GROUP;
+ static const int O3 = FieldType._OPTIONAL_INT32;
+ static const int O6 = FieldType._OPTIONAL_INT64;
+ static const int OS3 = FieldType._OPTIONAL_SINT32;
+ static const int OS6 = FieldType._OPTIONAL_SINT64;
+ static const int OU3 = FieldType._OPTIONAL_UINT32;
+ static const int OU6 = FieldType._OPTIONAL_UINT64;
+ static const int OF3 = FieldType._OPTIONAL_FIXED32;
+ static const int OF6 = FieldType._OPTIONAL_FIXED64;
+ static const int OSF3 = FieldType._OPTIONAL_SFIXED32;
+ static const int OSF6 = FieldType._OPTIONAL_SFIXED64;
+ static const int OM = FieldType._OPTIONAL_MESSAGE;
// re_Q_uired.
- static const int QB = _REQUIRED_BOOL;
- static const int QY = _REQUIRED_BYTES;
- static const int QS = _REQUIRED_STRING;
- static const int QF = _REQUIRED_FLOAT;
- static const int QD = _REQUIRED_DOUBLE;
- static const int QE = _REQUIRED_ENUM;
- static const int QG = _REQUIRED_GROUP;
- static const int Q3 = _REQUIRED_INT32;
- static const int Q6 = _REQUIRED_INT64;
- static const int QS3 = _REQUIRED_SINT32;
- static const int QS6 = _REQUIRED_SINT64;
- static const int QU3 = _REQUIRED_UINT32;
- static const int QU6 = _REQUIRED_UINT64;
- static const int QF3 = _REQUIRED_FIXED32;
- static const int QF6 = _REQUIRED_FIXED64;
- static const int QSF3 = _REQUIRED_SFIXED32;
- static const int QSF6 = _REQUIRED_SFIXED64;
- static const int QM = _REQUIRED_MESSAGE;
+ static const int QB = FieldType._REQUIRED_BOOL;
+ static const int QY = FieldType._REQUIRED_BYTES;
+ static const int QS = FieldType._REQUIRED_STRING;
+ static const int QF = FieldType._REQUIRED_FLOAT;
+ static const int QD = FieldType._REQUIRED_DOUBLE;
+ static const int QE = FieldType._REQUIRED_ENUM;
+ static const int QG = FieldType._REQUIRED_GROUP;
+ static const int Q3 = FieldType._REQUIRED_INT32;
+ static const int Q6 = FieldType._REQUIRED_INT64;
+ static const int QS3 = FieldType._REQUIRED_SINT32;
+ static const int QS6 = FieldType._REQUIRED_SINT64;
+ static const int QU3 = FieldType._REQUIRED_UINT32;
+ static const int QU6 = FieldType._REQUIRED_UINT64;
+ static const int QF3 = FieldType._REQUIRED_FIXED32;
+ static const int QF6 = FieldType._REQUIRED_FIXED64;
+ static const int QSF3 = FieldType._REQUIRED_SFIXED32;
+ static const int QSF6 = FieldType._REQUIRED_SFIXED64;
+ static const int QM = FieldType._REQUIRED_MESSAGE;
// re_P_eated.
- static const int PB = _REPEATED_BOOL;
- static const int PY = _REPEATED_BYTES;
- static const int PS = _REPEATED_STRING;
- static const int PF = _REPEATED_FLOAT;
- static const int PD = _REPEATED_DOUBLE;
- static const int PE = _REPEATED_ENUM;
- static const int PG = _REPEATED_GROUP;
- static const int P3 = _REPEATED_INT32;
- static const int P6 = _REPEATED_INT64;
- static const int PS3 = _REPEATED_SINT32;
- static const int PS6 = _REPEATED_SINT64;
- static const int PU3 = _REPEATED_UINT32;
- static const int PU6 = _REPEATED_UINT64;
- static const int PF3 = _REPEATED_FIXED32;
- static const int PF6 = _REPEATED_FIXED64;
- static const int PSF3 = _REPEATED_SFIXED32;
- static const int PSF6 = _REPEATED_SFIXED64;
- static const int PM = _REPEATED_MESSAGE;
+ static const int PB = FieldType._REPEATED_BOOL;
+ static const int PY = FieldType._REPEATED_BYTES;
+ static const int PS = FieldType._REPEATED_STRING;
+ static const int PF = FieldType._REPEATED_FLOAT;
+ static const int PD = FieldType._REPEATED_DOUBLE;
+ static const int PE = FieldType._REPEATED_ENUM;
+ static const int PG = FieldType._REPEATED_GROUP;
+ static const int P3 = FieldType._REPEATED_INT32;
+ static const int P6 = FieldType._REPEATED_INT64;
+ static const int PS3 = FieldType._REPEATED_SINT32;
+ static const int PS6 = FieldType._REPEATED_SINT64;
+ static const int PU3 = FieldType._REPEATED_UINT32;
+ static const int PU6 = FieldType._REPEATED_UINT64;
+ static const int PF3 = FieldType._REPEATED_FIXED32;
+ static const int PF6 = FieldType._REPEATED_FIXED64;
+ static const int PSF3 = FieldType._REPEATED_SFIXED32;
+ static const int PSF6 = FieldType._REPEATED_SFIXED64;
+ static const int PM = FieldType._REPEATED_MESSAGE;
// pac_K_ed.
- static const int KB = _PACKED_BOOL;
- static const int KE = _PACKED_ENUM;
- static const int KF = _PACKED_FLOAT;
- static const int KD = _PACKED_DOUBLE;
- static const int K3 = _PACKED_INT32;
- static const int K6 = _PACKED_INT64;
- static const int KS3 = _PACKED_SINT32;
- static const int KS6 = _PACKED_SINT64;
- static const int KU3 = _PACKED_UINT32;
- static const int KU6 = _PACKED_UINT64;
- static const int KF3 = _PACKED_FIXED32;
- static const int KF6 = _PACKED_FIXED64;
- static const int KSF3 = _PACKED_SFIXED32;
- static const int KSF6 = _PACKED_SFIXED64;
+ static const int KB = FieldType._PACKED_BOOL;
+ static const int KE = FieldType._PACKED_ENUM;
+ static const int KF = FieldType._PACKED_FLOAT;
+ static const int KD = FieldType._PACKED_DOUBLE;
+ static const int K3 = FieldType._PACKED_INT32;
+ static const int K6 = FieldType._PACKED_INT64;
+ static const int KS3 = FieldType._PACKED_SINT32;
+ static const int KS6 = FieldType._PACKED_SINT64;
+ static const int KU3 = FieldType._PACKED_UINT32;
+ static const int KU6 = FieldType._PACKED_UINT64;
+ static const int KF3 = FieldType._PACKED_FIXED32;
+ static const int KF6 = FieldType._PACKED_FIXED64;
+ static const int KSF3 = FieldType._PACKED_SFIXED32;
+ static const int KSF6 = FieldType._PACKED_SFIXED64;
// Range of integers in JSON (53-bit integers).
static Int64 MAX_JSON_INT = new Int64.fromInts(0x200000, 0);
static Int64 MIN_JSON_INT = -MAX_JSON_INT;
- // Closures commonly used by initializers.
- static String _STRING_EMPTY() => '';
- static List<int> _BYTES_EMPTY() => new PbList<int>();
- static bool _BOOL_FALSE() => false;
- static int _INT_ZERO() => 0;
- static double _DOUBLE_ZERO() => 0.0;
-
- static MakeDefaultFunc _defaultForType(int type) {
- switch (type) {
- case _OPTIONAL_BOOL: case _REQUIRED_BOOL:
- return _BOOL_FALSE;
- case _OPTIONAL_BYTES: case _REQUIRED_BYTES:
- return _BYTES_EMPTY;
- case _OPTIONAL_STRING: case _REQUIRED_STRING:
- return _STRING_EMPTY;
- case _OPTIONAL_FLOAT: case _REQUIRED_FLOAT:
- case _OPTIONAL_DOUBLE: case _REQUIRED_DOUBLE:
- return _DOUBLE_ZERO;
- case _OPTIONAL_INT32: case _REQUIRED_INT32:
- case _OPTIONAL_INT64: case _REQUIRED_INT64:
- case _OPTIONAL_SINT32: case _REQUIRED_SINT32:
- case _OPTIONAL_SINT64: case _REQUIRED_SINT64:
- case _OPTIONAL_UINT32: case _REQUIRED_UINT32:
- case _OPTIONAL_UINT64: case _REQUIRED_UINT64:
- case _OPTIONAL_FIXED32: case _REQUIRED_FIXED32:
- case _OPTIONAL_FIXED64: case _REQUIRED_FIXED64:
- case _OPTIONAL_SFIXED32: case _REQUIRED_SFIXED32:
- case _OPTIONAL_SFIXED64: case _REQUIRED_SFIXED64:
- return _INT_ZERO;
- default:
- return null;
- }
- }
-
final Map<int, dynamic> _fieldValues = new Map<int, dynamic>();
final Map<int, Extension> _extensions = new Map<int, Extension>();
final UnknownFieldSet unknownFields = new UnknownFieldSet();
@@ -317,10 +188,10 @@ abstract class GeneratedMessage {
var value = _fieldValues[tagNumber];
hash = ((37 * hash) + tagNumber) & 0x3fffffff;
int fieldType = _getFieldType(tagNumber);
- if (_toBaseFieldType(fieldType) != _ENUM_BIT) {
+ if (!_isEnum(fieldType)) {
// TODO(sgjesse): Remove 'as Object' here when issue 14951 is fixed.
hash = ((53 * hash) + (value as Object).hashCode) & 0x3fffffff;
- } else if ((fieldType & _REPEATED_BIT) != 0) {
+ } else if (_isRepeated(fieldType)) {
hashEnumList(value);
} else {
ProtobufEnum enm = value;
@@ -444,39 +315,6 @@ abstract class GeneratedMessage {
readPackableToList(wireType, tagNumber, readToList);
}
- bool wireTypeMatch(int tagNumber, int fieldType, int wireType) {
- switch (_toBaseFieldType(fieldType)) {
- case _BOOL_BIT:
- case _ENUM_BIT:
- case _INT32_BIT:
- case _INT64_BIT:
- case _SINT32_BIT:
- case _SINT64_BIT:
- case _UINT32_BIT:
- case _UINT64_BIT:
- return wireType == WIRETYPE_VARINT ||
- wireType == WIRETYPE_LENGTH_DELIMITED;
- case _FLOAT_BIT:
- case _FIXED32_BIT:
- case _SFIXED32_BIT:
- return wireType == WIRETYPE_FIXED32 ||
- wireType == WIRETYPE_LENGTH_DELIMITED;
- case _DOUBLE_BIT:
- case _FIXED64_BIT:
- case _SFIXED64_BIT:
- return wireType == WIRETYPE_FIXED64 ||
- wireType == WIRETYPE_LENGTH_DELIMITED;
- case _BYTES_BIT:
- case _STRING_BIT:
- case _MESSAGE_BIT:
- return wireType == WIRETYPE_LENGTH_DELIMITED;
- case _GROUP_BIT:
- return wireType == WIRETYPE_START_GROUP;
- default:
- return false;
- }
- }
-
while (true) {
int tag = input.readTag();
if (tag == 0) {
@@ -496,7 +334,7 @@ abstract class GeneratedMessage {
fieldType = extension.type;
}
}
- if (fieldType == -1 || !wireTypeMatch(tagNumber, fieldType, wireType)) {
+ if (fieldType == -1 || !_wireTypeMatches(fieldType, wireType)) {
if (!unknownFields.mergeFieldFromBuffer(tag, input)) {
return;
} else {
@@ -505,24 +343,24 @@ abstract class GeneratedMessage {
}
// Ignore required/optional packed/unpacked.
- fieldType &= ~(_PACKED_BIT | _REQUIRED_BIT);
+ fieldType &= ~(FieldType._PACKED_BIT | FieldType._REQUIRED_BIT);
switch (fieldType) {
- case _OPTIONAL_BOOL:
+ case FieldType._OPTIONAL_BOOL:
_setField(tagNumber, input.readBool());
break;
- case _OPTIONAL_BYTES:
+ case FieldType._OPTIONAL_BYTES:
_setField(tagNumber, input.readBytes());
break;
- case _OPTIONAL_STRING:
+ case FieldType._OPTIONAL_STRING:
_setField(tagNumber, input.readString());
break;
- case _OPTIONAL_FLOAT:
+ case FieldType._OPTIONAL_FLOAT:
_setField(tagNumber, input.readFloat());
break;
- case _OPTIONAL_DOUBLE:
+ case FieldType._OPTIONAL_DOUBLE:
_setField(tagNumber, input.readDouble());
break;
- case _OPTIONAL_ENUM:
+ case FieldType._OPTIONAL_ENUM:
int rawValue = input.readEnum();
var value = _getValueOfFunc(tagNumber, extensionRegistry)(rawValue);
if (value == null) {
@@ -531,7 +369,7 @@ abstract class GeneratedMessage {
_setField(tagNumber, value);
}
break;
- case _OPTIONAL_GROUP:
+ case FieldType._OPTIONAL_GROUP:
GeneratedMessage subMessage =
_getEmptyMessage(tagNumber, extensionRegistry);
if (_fieldValues.containsKey(tagNumber)) {
@@ -540,37 +378,37 @@ abstract class GeneratedMessage {
input.readGroup(tagNumber, subMessage, extensionRegistry);
_setField(tagNumber, subMessage);
break;
- case _OPTIONAL_INT32:
+ case FieldType._OPTIONAL_INT32:
_setField(tagNumber, input.readInt32());
break;
- case _OPTIONAL_INT64:
+ case FieldType._OPTIONAL_INT64:
_setField(tagNumber, input.readInt64());
break;
- case _OPTIONAL_SINT32:
+ case FieldType._OPTIONAL_SINT32:
_setField(tagNumber, input.readSint32());
break;
- case _OPTIONAL_SINT64:
+ case FieldType._OPTIONAL_SINT64:
_setField(tagNumber, input.readSint64());
break;
- case _OPTIONAL_UINT32:
+ case FieldType._OPTIONAL_UINT32:
_setField(tagNumber, input.readUint32());
break;
- case _OPTIONAL_UINT64:
+ case FieldType._OPTIONAL_UINT64:
_setField(tagNumber, input.readUint64());
break;
- case _OPTIONAL_FIXED32:
+ case FieldType._OPTIONAL_FIXED32:
_setField(tagNumber, input.readFixed32());
break;
- case _OPTIONAL_FIXED64:
+ case FieldType._OPTIONAL_FIXED64:
_setField(tagNumber, input.readFixed64());
break;
- case _OPTIONAL_SFIXED32:
+ case FieldType._OPTIONAL_SFIXED32:
_setField(tagNumber, input.readSfixed32());
break;
- case _OPTIONAL_SFIXED64:
+ case FieldType._OPTIONAL_SFIXED64:
_setField(tagNumber, input.readSfixed64());
break;
- case _OPTIONAL_MESSAGE:
+ case FieldType._OPTIONAL_MESSAGE:
GeneratedMessage subMessage =
_getEmptyMessage(tagNumber, extensionRegistry);
if (_fieldValues.containsKey(tagNumber)) {
@@ -579,22 +417,22 @@ abstract class GeneratedMessage {
input.readMessage(subMessage, extensionRegistry);
_setField(tagNumber, subMessage);
break;
- case _REPEATED_BOOL:
+ case FieldType._REPEATED_BOOL:
readPackable(wireType, tagNumber, input.readBool);
break;
- case _REPEATED_BYTES:
+ case FieldType._REPEATED_BYTES:
appendToRepeated(tagNumber, input.readBytes());
break;
- case _REPEATED_STRING:
+ case FieldType._REPEATED_STRING:
appendToRepeated(tagNumber, input.readString());
break;
- case _REPEATED_FLOAT:
+ case FieldType._REPEATED_FLOAT:
readPackable(wireType, tagNumber, input.readFloat);
break;
- case _REPEATED_DOUBLE:
+ case FieldType._REPEATED_DOUBLE:
readPackable(wireType, tagNumber, input.readDouble);
break;
- case _REPEATED_ENUM:
+ case FieldType._REPEATED_ENUM:
readPackableToList(wireType, tagNumber, (List list) {
int rawValue = input.readEnum();
var value = _getValueOfFunc(tagNumber, extensionRegistry)(rawValue);
@@ -605,43 +443,43 @@ abstract class GeneratedMessage {
}
});
break;
- case _REPEATED_GROUP:
+ case FieldType._REPEATED_GROUP:
GeneratedMessage subMessage =
_getEmptyMessage(tagNumber, extensionRegistry);
input.readGroup(tagNumber, subMessage, extensionRegistry);
appendToRepeated(tagNumber, subMessage);
break;
- case _REPEATED_INT32:
+ case FieldType._REPEATED_INT32:
readPackable(wireType, tagNumber, input.readInt32);
break;
- case _REPEATED_INT64:
+ case FieldType._REPEATED_INT64:
readPackable(wireType, tagNumber, input.readInt64);
break;
- case _REPEATED_SINT32:
+ case FieldType._REPEATED_SINT32:
readPackable(wireType, tagNumber, input.readSint32);
break;
- case _REPEATED_SINT64:
+ case FieldType._REPEATED_SINT64:
readPackable(wireType, tagNumber, input.readSint64);
break;
- case _REPEATED_UINT32:
+ case FieldType._REPEATED_UINT32:
readPackable(wireType, tagNumber, input.readUint32);
break;
- case _REPEATED_UINT64:
+ case FieldType._REPEATED_UINT64:
readPackable(wireType, tagNumber, input.readUint64);
break;
- case _REPEATED_FIXED32:
+ case FieldType._REPEATED_FIXED32:
readPackable(wireType, tagNumber, input.readFixed32);
break;
- case _REPEATED_FIXED64:
+ case FieldType._REPEATED_FIXED64:
readPackable(wireType, tagNumber, input.readFixed64);
break;
- case _REPEATED_SFIXED32:
+ case FieldType._REPEATED_SFIXED32:
readPackable(wireType, tagNumber, input.readSfixed32);
break;
- case _REPEATED_SFIXED64:
+ case FieldType._REPEATED_SFIXED64:
readPackable(wireType, tagNumber, input.readSfixed64);
break;
- case _REPEATED_MESSAGE:
+ case FieldType._REPEATED_MESSAGE:
GeneratedMessage subMessage =
_getEmptyMessage(tagNumber, extensionRegistry);
input.readMessage(subMessage, extensionRegistry);
@@ -676,40 +514,40 @@ abstract class GeneratedMessage {
/// The encoding is described in [GeneratedMessage.writeToJson].
Map<String, dynamic> writeToJsonMap() {
convertToMap(fieldValue, fieldType) {
- int scalarType = fieldType & ~(_REPEATED_BIT | _PACKED_BIT);
+ int baseType = FieldType._baseType(fieldType);
- if ((fieldType & _REPEATED_BIT) != 0) {
- return new List.from(fieldValue.map((e) => convertToMap(e, scalarType)));
+ if (_isRepeated(fieldType)) {
+ return new List.from(fieldValue.map((e) => convertToMap(e, baseType)));
}
- switch (scalarType) {
- case _BOOL_BIT:
- case _STRING_BIT:
- case _FLOAT_BIT:
- case _DOUBLE_BIT:
- case _INT32_BIT:
- case _SINT32_BIT:
- case _UINT32_BIT:
- case _FIXED32_BIT:
- case _SFIXED32_BIT:
+ switch (baseType) {
+ case FieldType._BOOL_BIT:
+ case FieldType._STRING_BIT:
+ case FieldType._FLOAT_BIT:
+ case FieldType._DOUBLE_BIT:
+ case FieldType._INT32_BIT:
+ case FieldType._SINT32_BIT:
+ case FieldType._UINT32_BIT:
+ case FieldType._FIXED32_BIT:
+ case FieldType._SFIXED32_BIT:
return fieldValue;
- case _BYTES_BIT:
+ case FieldType._BYTES_BIT:
// Encode 'bytes' as a base64-encoded string.
return CryptoUtils.bytesToBase64(fieldValue);
- case _ENUM_BIT:
+ case FieldType._ENUM_BIT:
return fieldValue.value; // assume |value| < 2^52
- case _INT64_BIT:
- case _SINT64_BIT:
- case _UINT64_BIT:
- case _FIXED64_BIT:
- case _SFIXED64_BIT:
+ case FieldType._INT64_BIT:
+ case FieldType._SINT64_BIT:
+ case FieldType._UINT64_BIT:
+ case FieldType._FIXED64_BIT:
+ case FieldType._SFIXED64_BIT:
// Use strings for 64-bit integers which cannot fit in doubles.
if (MIN_JSON_INT <= fieldValue && fieldValue <= MAX_JSON_INT) {
return fieldValue.toInt();
}
return fieldValue.toString();
- case _GROUP_BIT:
- case _MESSAGE_BIT:
+ case FieldType._GROUP_BIT:
+ case FieldType._MESSAGE_BIT:
return fieldValue.writeToJsonMap();
default:
throw 'Unknown type $fieldType';
@@ -721,7 +559,7 @@ abstract class GeneratedMessage {
if (!hasField(tagNumber)) continue;
String key = '$tagNumber';
var fieldValue = _fieldValues[tagNumber];
- int fieldType = _getFieldType(tagNumber) & ~_REQUIRED_BIT;
+ int fieldType = _getFieldType(tagNumber);
result[key] = convertToMap(fieldValue, fieldType);
}
return result;
@@ -768,7 +606,7 @@ abstract class GeneratedMessage {
if (fieldType == -1) {
throw new StateError('Unknown field type for tag number $tagNumber');
}
- if ((fieldType & _REPEATED_BIT) != 0) {
+ if (_isRepeated(fieldType)) {
List thisList = getField(tagNumber);
for (var value in fieldValue) {
thisList.add(_convertJsonValue(value, tagNumber, fieldType,
@@ -785,8 +623,8 @@ abstract class GeneratedMessage {
_convertJsonValue(value, int tagNumber, int fieldType,
ExtensionRegistry extensionRegistry) {
String expectedType; // for exception message
- switch (_toBaseFieldType(fieldType)) {
- case _BOOL_BIT:
+ switch (FieldType._baseType(fieldType)) {
+ case FieldType._BOOL_BIT:
if (value is bool) {
return value;
} else if (value is String) {
@@ -805,20 +643,20 @@ abstract class GeneratedMessage {
expectedType = 'bool, 0, or 1';
}
break;
- case _BYTES_BIT:
+ case FieldType._BYTES_BIT:
if (value is String) {
return CryptoUtils.base64StringToBytes(value);
}
expectedType = 'Base64 String';
break;
- case _STRING_BIT:
+ case FieldType._STRING_BIT:
if (value is String) {
return value;
}
expectedType = 'String';
break;
- case _FLOAT_BIT:
- case _DOUBLE_BIT:
+ case FieldType._FLOAT_BIT:
+ case FieldType._DOUBLE_BIT:
// Allow quoted values, although we don't emit them.
if (value is double) {
return value;
@@ -829,7 +667,7 @@ abstract class GeneratedMessage {
}
expectedType = 'num or stringified num';
break;
- case _ENUM_BIT:
+ case FieldType._ENUM_BIT:
// Allow quoted values, although we don't emit them.
if (value is String) {
value = int.parse(value);
@@ -839,11 +677,11 @@ abstract class GeneratedMessage {
}
expectedType = 'int or stringified int';
break;
- case _INT32_BIT:
- case _SINT32_BIT:
- case _UINT32_BIT:
- case _FIXED32_BIT:
- case _SFIXED32_BIT:
+ case FieldType._INT32_BIT:
+ case FieldType._SINT32_BIT:
+ case FieldType._UINT32_BIT:
+ case FieldType._FIXED32_BIT:
+ case FieldType._SFIXED32_BIT:
if (value is String) {
value = int.parse(value);
}
@@ -853,11 +691,11 @@ abstract class GeneratedMessage {
}
expectedType = 'int or stringified int';
break;
- case _INT64_BIT:
- case _SINT64_BIT:
- case _UINT64_BIT:
- case _FIXED64_BIT:
- case _SFIXED64_BIT:
+ case FieldType._INT64_BIT:
+ case FieldType._SINT64_BIT:
+ case FieldType._UINT64_BIT:
+ case FieldType._FIXED64_BIT:
+ case FieldType._SFIXED64_BIT:
// Allow quoted values, although we don't emit them.
if (value is String) {
return Int64.parseRadix(value, 10);
@@ -867,8 +705,8 @@ abstract class GeneratedMessage {
}
expectedType = 'int or stringified int';
break;
- case _GROUP_BIT:
- case _MESSAGE_BIT:
+ case FieldType._GROUP_BIT:
+ case FieldType._MESSAGE_BIT:
if (value is Map) {
GeneratedMessage subMessage =
_getEmptyMessage(tagNumber, extensionRegistry);
@@ -905,12 +743,12 @@ abstract class GeneratedMessage {
/// The backing [List] will be created if necessary.
void addExtension(Extension extension, var value) {
_checkExtension(extension);
- if ((extension.type & _REPEATED_BIT) == 0) {
+ if (!_isRepeated(extension.type)) {
throw new ArgumentError(
'Cannot add to a non-repeated field (use setExtension())');
}
// Validate type and range.
- _validate(extension.tagNumber, extension.type & ~0x7, value);
+ _validate(extension.tagNumber, extension.type, value);
var list = _fieldValues[extension.tagNumber];
if (list == null) {
@@ -1045,10 +883,10 @@ abstract class GeneratedMessage {
}
int fieldType = other._getFieldType(tagNumber);
var cloner = (x) => x;
- if ((fieldType & (_GROUP_BIT | _MESSAGE_BIT)) != 0) {
+ if ((fieldType & (FieldType._GROUP_BIT | FieldType._MESSAGE_BIT)) != 0) {
cloner = (message) => message.clone();
}
- if ((fieldType & _REPEATED_BIT) != 0) {
+ if (_isRepeated(fieldType)) {
getField(tagNumber).addAll(new List.from(fieldValue).map(cloner));
} else {
setField(tagNumber, cloner(fieldValue), fieldType);
@@ -1086,13 +924,13 @@ abstract class GeneratedMessage {
}
fieldType = info_.fieldType(tagNumber);
}
- if ((fieldType & _REPEATED_BIT) != 0) {
+ if (_isRepeated(fieldType)) {
throw new ArgumentError(_generateMessage(tagNumber, value,
'repeating field (use get + .add())'));
}
// Validate type and range.
- _validate(tagNumber, fieldType & ~0x7, value);
+ _validate(tagNumber, fieldType, value);
_setField(tagNumber, value);
}
@@ -1128,12 +966,6 @@ abstract class GeneratedMessage {
return type;
}
- /// Returns the base field type without any of the required, repeated
- /// and packed bits.
- int _toBaseFieldType(int fieldType) {
- return fieldType & ~(_REQUIRED_BIT | _REPEATED_BIT | _PACKED_BIT);
- }
-
GeneratedMessage _getEmptyMessage(
int tagNumber, ExtensionRegistry extensionRegistry) {
CreateBuilderFunc subBuilderFunc = info_.subBuilder(tagNumber);
@@ -1173,26 +1005,26 @@ abstract class GeneratedMessage {
}
void _validate(int tagNumber, int fieldType, var value) {
- switch (fieldType) {
- case _BOOL_BIT:
+ switch (FieldType._baseType(fieldType)) {
+ case FieldType._BOOL_BIT:
if (value is !bool) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type bool'));
}
break;
- case _BYTES_BIT:
+ case FieldType._BYTES_BIT:
if (value is !List) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not List'));
}
break;
- case _STRING_BIT:
+ case FieldType._STRING_BIT:
if (value is !String) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type String'));
}
break;
- case _FLOAT_BIT:
+ case FieldType._FLOAT_BIT:
if (value is !double) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type double'));
@@ -1202,19 +1034,19 @@ abstract class GeneratedMessage {
'out of range for float'));
}
break;
- case _DOUBLE_BIT:
+ case FieldType._DOUBLE_BIT:
if (value is !double) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type double'));
}
break;
- case _ENUM_BIT:
+ case FieldType._ENUM_BIT:
if (value is !ProtobufEnum) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type ProtobufEnum'));
}
break;
- case _INT32_BIT:
+ case FieldType._INT32_BIT:
if (value is !int) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type int'));
@@ -1224,7 +1056,7 @@ abstract class GeneratedMessage {
'out of range for int32'));
}
break;
- case _INT64_BIT:
+ case FieldType._INT64_BIT:
if (value is !Int64) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not Int64'));
@@ -1234,7 +1066,7 @@ abstract class GeneratedMessage {
'out of range for int64'));
}
break;
- case _SINT32_BIT:
+ case FieldType._SINT32_BIT:
if (value is !int) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type int'));
@@ -1244,7 +1076,7 @@ abstract class GeneratedMessage {
'out of range for sint32'));
}
break;
- case _SINT64_BIT:
+ case FieldType._SINT64_BIT:
if (value is !Int64) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not Int64'));
@@ -1254,7 +1086,7 @@ abstract class GeneratedMessage {
'out of range for sint64'));
}
break;
- case _UINT32_BIT:
+ case FieldType._UINT32_BIT:
if (value is !int) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type int'));
@@ -1264,7 +1096,7 @@ abstract class GeneratedMessage {
'out of range for uint32'));
}
break;
- case _UINT64_BIT:
+ case FieldType._UINT64_BIT:
if (value is !Int64) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not Int64'));
@@ -1274,7 +1106,7 @@ abstract class GeneratedMessage {
'out of range for uint64'));
}
break;
- case _FIXED32_BIT:
+ case FieldType._FIXED32_BIT:
if (value is !int) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type int'));
@@ -1284,7 +1116,7 @@ abstract class GeneratedMessage {
'out of range for fixed32'));
}
break;
- case _FIXED64_BIT:
+ case FieldType._FIXED64_BIT:
if (value is !Int64) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not Int64'));
@@ -1294,7 +1126,7 @@ abstract class GeneratedMessage {
'out of range for fixed64'));
}
break;
- case _SFIXED32_BIT:
+ case FieldType._SFIXED32_BIT:
if (value is !int) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not type int'));
@@ -1304,7 +1136,7 @@ abstract class GeneratedMessage {
'out of range for sfixed32'));
}
break;
- case _SFIXED64_BIT:
+ case FieldType._SFIXED64_BIT:
if (value is !Int64) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not Int64'));
@@ -1314,8 +1146,8 @@ abstract class GeneratedMessage {
'out of range for sfixed64'));
}
break;
- case _GROUP_BIT:
- case _MESSAGE_BIT:
+ case FieldType._GROUP_BIT:
+ case FieldType._MESSAGE_BIT:
if (value is !GeneratedMessage) {
throw new ArgumentError(
_generateMessage(tagNumber, value, 'not a GeneratedMessage'));
« no previous file with comments | « lib/src/protobuf/field_type.dart ('k') | lib/src/protobuf/unknown_field_set.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698