Skip to content

Commit

Permalink
Named arguments to construct messages (google#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavantix authored Nov 27, 2020
1 parent 2d80431 commit 7ac17d0
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 61 deletions.
3 changes: 3 additions & 0 deletions protoc_plugin/lib/base_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class BaseType {
String getRepeatedDartType(FileGenerator fileGen) =>
"$_coreImportPrefix.List<${getDartType(fileGen)}>";

String getRepeatedDartTypeIterable(FileGenerator fileGen) =>
"$_coreImportPrefix.Iterable<${getDartType(fileGen)}>";

factory BaseType(FieldDescriptorProto field, GenerationContext ctx) {
String constSuffix;

Expand Down
39 changes: 38 additions & 1 deletion protoc_plugin/lib/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,44 @@ class MessageGenerator extends ProtobufContainer {
out.printlnAnnotated('${classname}._() : super();', [
NamedLocation(name: classname, fieldPathSegment: fieldPath, start: 0)
]);
out.println('factory ${classname}() => create();');
out.print('factory $classname(');
if (_fieldList.isNotEmpty) {
out.println('{');
for (final field in _fieldList) {
_emitDeprecatedIf(field.isDeprecated, out);
if (field.isRepeated && !field.isMapField) {
out.println(
' ${field.baseType.getRepeatedDartTypeIterable(fileGen)} ${field.memberNames.fieldName},');
} else {
out.println(
' ${field.getDartType(fileGen)} ${field.memberNames.fieldName},');
}
}
out.print('}');
}
if (_fieldList.isNotEmpty) {
out.println(') {');
out.println(' final _result = create();');
for (final field in _fieldList) {
out.println(' if (${field.memberNames.fieldName} != null) {');
if (field.isDeprecated) {
out.println(
' // ignore: deprecated_member_use_from_same_package');
}
if (field.isRepeated || field.isMapField) {
out.println(
' _result.${field.memberNames.fieldName}.addAll(${field.memberNames.fieldName});');
} else {
out.println(
' _result.${field.memberNames.fieldName} = ${field.memberNames.fieldName};');
}
out.println(' }');
}
out.println(' return _result;');
out.println('}');
} else {
out.println(') => create();');
}
out.println(
'factory ${classname}.fromBuffer($_coreImportPrefix.List<$_coreImportPrefix.int> i,'
' [$_protobufImportPrefix.ExtensionRegistry r = $_protobufImportPrefix.ExtensionRegistry.EMPTY])'
Expand Down
18 changes: 17 additions & 1 deletion protoc_plugin/test/goldens/imports.pb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ class M extends $pb.GeneratedMessage {
;

M._() : super();
factory M() => create();
factory M({
M m,
$1.M m1,
$2.M m2,
}) {
final _result = create();
if (m != null) {
_result.m = m;
}
if (m1 != null) {
_result.m1 = m1;
}
if (m2 != null) {
_result.m2 = m2;
}
return _result;
}
factory M.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory M.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
Expand Down
10 changes: 9 additions & 1 deletion protoc_plugin/test/goldens/int64.pb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ class Int64 extends $pb.GeneratedMessage {
;

Int64._() : super();
factory Int64() => create();
factory Int64({
$fixnum.Int64 value,
}) {
final _result = create();
if (value != null) {
_result.value = value;
}
return _result;
}
factory Int64.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory Int64.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
Expand Down
24 changes: 23 additions & 1 deletion protoc_plugin/test/goldens/messageGenerator
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ class PhoneNumber extends $pb.GeneratedMessage {
;

PhoneNumber._() : super();
factory PhoneNumber() => create();
factory PhoneNumber({
$core.String number,
PhoneNumber_PhoneType type,
$core.String name,
@$core.Deprecated('This field is deprecated.')
$core.String deprecatedField,
}) {
final _result = create();
if (number != null) {
_result.number = number;
}
if (type != null) {
_result.type = type;
}
if (name != null) {
_result.name = name;
}
if (deprecatedField != null) {
// ignore: deprecated_member_use_from_same_package
_result.deprecatedField = deprecatedField;
}
return _result;
}
factory PhoneNumber.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory PhoneNumber.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
Expand Down
64 changes: 32 additions & 32 deletions protoc_plugin/test/goldens/messageGenerator.meta
Original file line number Diff line number Diff line change
Expand Up @@ -18,141 +18,141 @@ annotation: {
path: 2
path: 1
sourceFile:
begin: 2247
end: 2253
begin: 2782
end: 2788
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile:
begin: 2295
end: 2301
begin: 2830
end: 2836
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile:
begin: 2374
end: 2383
begin: 2909
end: 2918
}
annotation: {
path: 4
path: 0
path: 2
path: 1
sourceFile:
begin: 2426
end: 2437
begin: 2961
end: 2972
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile:
begin: 2507
end: 2511
begin: 3042
end: 3046
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile:
begin: 2552
end: 2556
begin: 3087
end: 3091
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile:
begin: 2635
end: 2642
begin: 3170
end: 3177
}
annotation: {
path: 4
path: 0
path: 2
path: 0
sourceFile:
begin: 2685
end: 2694
begin: 3220
end: 3229
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile:
begin: 2755
end: 2759
begin: 3290
end: 3294
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile:
begin: 2806
end: 2810
begin: 3341
end: 3345
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile:
begin: 2883
end: 2890
begin: 3418
end: 3425
}
annotation: {
path: 4
path: 0
path: 2
path: 2
sourceFile:
begin: 2933
end: 2942
begin: 3468
end: 3477
}
annotation: {
path: 4
path: 0
path: 2
path: 3
sourceFile:
begin: 3052
end: 3067
begin: 3587
end: 3602
}
annotation: {
path: 4
path: 0
path: 2
path: 3
sourceFile:
begin: 3158
end: 3173
begin: 3693
end: 3708
}
annotation: {
path: 4
path: 0
path: 2
path: 3
sourceFile:
begin: 3295
end: 3313
begin: 3830
end: 3848
}
annotation: {
path: 4
path: 0
path: 2
path: 3
sourceFile:
begin: 3405
end: 3425
begin: 3940
end: 3960
}
18 changes: 17 additions & 1 deletion protoc_plugin/test/goldens/oneMessage.pb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ class PhoneNumber extends $pb.GeneratedMessage {
;

PhoneNumber._() : super();
factory PhoneNumber() => create();
factory PhoneNumber({
$core.String number,
$core.int type,
$core.String name,
}) {
final _result = create();
if (number != null) {
_result.number = number;
}
if (type != null) {
_result.type = type;
}
if (name != null) {
_result.name = name;
}
return _result;
}
factory PhoneNumber.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory PhoneNumber.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
Expand Down
Loading

0 comments on commit 7ac17d0

Please sign in to comment.