Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a class for field types #605

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 27 additions & 22 deletions protobuf/lib/src/protobuf/builder_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BuilderInfo {
void add<T>(
int tagNumber,
String name,
int? fieldType,
FieldType? fieldType,
dynamic defaultOrMaker,
CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
Expand All @@ -79,22 +79,22 @@ class BuilderInfo {
void addMapField<K, V>(
int tagNumber,
String name,
int keyFieldType,
int valueFieldType,
FieldType keyFieldType,
FieldType valueFieldType,
BuilderInfo mapEntryBuilderInfo,
CreateBuilderFunc? valueCreator,
{ProtobufEnum? defaultEnumValue,
String? protoName}) {
var index = byIndex.length;
_addField(MapFieldInfo<K, V>(name, tagNumber, index, PbFieldType.M,
_addField(MapFieldInfo<K, V>(name, tagNumber, index, const FieldType.MAP(),
keyFieldType, valueFieldType, mapEntryBuilderInfo, valueCreator,
defaultEnumValue: defaultEnumValue, protoName: protoName));
}

void addRepeated<T>(
int tagNumber,
String name,
int fieldType,
FieldType fieldType,
CheckFunc<T> check,
CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
Expand Down Expand Up @@ -123,7 +123,7 @@ class BuilderInfo {
}
}

void a<T>(int tagNumber, String name, int fieldType,
void a<T>(int tagNumber, String name, FieldType fieldType,
{dynamic defaultOrMaker,
CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
Expand All @@ -137,37 +137,41 @@ class BuilderInfo {
/// Adds PbFieldType.OS String with no default value to reduce generated
/// code size.
void aOS(int tagNumber, String name, {String? protoName}) {
add<String>(tagNumber, name, PbFieldType.OS, null, null, null, null,
add<String>(tagNumber, name, const FieldType.OPTIONAL_STRING(), null, null,
null, null,
protoName: protoName);
}

/// Adds PbFieldType.PS String with no default value.
void pPS(int tagNumber, String name, {String? protoName}) {
addRepeated<String>(tagNumber, name, PbFieldType.PS,
getCheckFunction(PbFieldType.PS), null, null, null,
addRepeated<String>(tagNumber, name, const FieldType.REPEATED_STRING(),
getCheckFunction(const FieldType.REPEATED_STRING()), null, null, null,
protoName: protoName);
}

/// Adds PbFieldType.QS String with no default value.
void aQS(int tagNumber, String name, {String? protoName}) {
add<String>(tagNumber, name, PbFieldType.QS, null, null, null, null,
add<String>(tagNumber, name, const FieldType.REQUIRED_STRING(), null, null,
null, null,
protoName: protoName);
}

/// Adds Int64 field with Int64.ZERO default.
void aInt64(int tagNumber, String name, {String? protoName}) {
add<Int64>(tagNumber, name, PbFieldType.O6, Int64.ZERO, null, null, null,
add<Int64>(tagNumber, name, const FieldType.OPTIONAL_I64(), Int64.ZERO,
null, null, null,
protoName: protoName);
}

/// Adds a boolean with no default value.
void aOB(int tagNumber, String name, {String? protoName}) {
add<bool>(tagNumber, name, PbFieldType.OB, null, null, null, null,
add<bool>(tagNumber, name, const FieldType.OPTIONAL_BOOL(), null, null,
null, null,
protoName: protoName);
}

// Enum.
void e<T>(int tagNumber, String name, int fieldType,
void e<T>(int tagNumber, String name, FieldType fieldType,
{dynamic defaultOrMaker,
ValueOfFunc? valueOf,
List<ProtobufEnum>? enumValues,
Expand All @@ -178,21 +182,22 @@ class BuilderInfo {
}

// Repeated, not a message, group, or enum.
void p<T>(int tagNumber, String name, int fieldType, {String? protoName}) {
assert(!_isGroupOrMessage(fieldType) && !_isEnum(fieldType));
void p<T>(int tagNumber, String name, FieldType fieldType,
{String? protoName}) {
assert(!fieldType.isGroupOrMessage && !fieldType.isEnum);
addRepeated<T>(tagNumber, name, fieldType, getCheckFunction(fieldType),
null, null, null,
protoName: protoName);
}

// Repeated message, group, or enum.
void pc<T>(int tagNumber, String name, int fieldType,
void pc<T>(int tagNumber, String name, FieldType fieldType,
{CreateBuilderFunc? subBuilder,
ValueOfFunc? valueOf,
List<ProtobufEnum>? enumValues,
ProtobufEnum? defaultEnumValue,
String? protoName}) {
assert(_isGroupOrMessage(fieldType) || _isEnum(fieldType));
assert(fieldType.isGroupOrMessage || fieldType.isEnum);
addRepeated<T>(tagNumber, name, fieldType, _checkNotNull, subBuilder,
valueOf, enumValues,
defaultEnumValue: defaultEnumValue, protoName: protoName);
Expand All @@ -203,7 +208,7 @@ class BuilderInfo {
add<T>(
tagNumber,
name,
PbFieldType.OM,
const FieldType.OPTIONAL_MESSAGE(),
GeneratedMessage._defaultMakerFor<T>(subBuilder),
subBuilder,
null,
Expand All @@ -216,7 +221,7 @@ class BuilderInfo {
add<T>(
tagNumber,
name,
PbFieldType.QM,
const FieldType.REQUIRED_MESSAGE(),
GeneratedMessage._defaultMakerFor<T>(subBuilder),
subBuilder,
null,
Expand All @@ -234,8 +239,8 @@ class BuilderInfo {
// Map field.
void m<K, V>(int tagNumber, String name,
{String? entryClassName,
required int keyFieldType,
required int valueFieldType,
required FieldType keyFieldType,
required FieldType valueFieldType,
CreateBuilderFunc? valueCreator,
ValueOfFunc? valueOf,
List<ProtobufEnum>? enumValues,
Expand Down Expand Up @@ -265,7 +270,7 @@ class BuilderInfo {
return i?.name;
}

int? fieldType(int tagNumber) {
FieldType? fieldType(int tagNumber) {
var i = fieldInfo[tagNumber];
return i?.type;
}
Expand Down
Loading