Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Dart 2 fixes. (#106)
Browse files Browse the repository at this point in the history
* Breaking change: Generated RpcClient stubs use the generic invoke method. Requires package:protobuf version 0.8.0 or newer.
* Dart 2 fixes.
  • Loading branch information
jakobr-google authored May 17, 2018
1 parent 8289fd1 commit a957cb2
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ sudo: false
# them against each Dart version.
matrix:
include:
- dart: stable
- dart: dev
dart_task: dartfmt
- dart: stable
- dart: dev
env: PROTOC_VERSION=3.0.0
install: ./tool/travis/setup.sh
script: ./tool/travis/test.sh
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.8.0 - 2018-05-17

* Breaking change: Generated RpcClient stubs use the generic invoke method.
Requires package:protobuf version 0.8.0 or newer.
* Dart 2 fixes.

## 0.7.11 - 2018-04-09

* Dart 2 fix.
Expand Down
8 changes: 4 additions & 4 deletions benchmark/lib/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

library protoc.benchmark.report;

import 'dart:convert' show JSON;
import 'dart:convert' show jsonEncode;

import 'generated/benchmark.pb.dart' as pb;

Expand Down Expand Up @@ -159,15 +159,15 @@ String _stringifyMap(StringBuffer out, Map json, String indent) {

var value = json[key];
out.write(childIndent);
out.write(JSON.encode(key));
out.write(jsonEncode(key));
out.write(": ");

if (value is List) {
_stringifyList(out, value, childIndent);
} else if (value is Map && indent.length < 4) {
_stringifyMap(out, value, childIndent);
} else {
out.write(JSON.encode(value));
out.write(jsonEncode(value));
}
}
out.write("\n$indent}");
Expand All @@ -182,7 +182,7 @@ void _stringifyList(StringBuffer out, List json, String indent) {
if (!first) out.write(",\n");
first = false;
out.write(childIndent);
out.write(JSON.encode(item));
out.write(jsonEncode(item));
}
out.write("\n$indent]");
}
2 changes: 1 addition & 1 deletion lib/base_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class BaseType {
"double", r"$_setDouble", null);
case FieldDescriptorProto_Type.TYPE_INT32:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_INT32, "3",
"int", r"$_setUnsignedInt32", null);
"int", r"$_setSignedInt32", null);
case FieldDescriptorProto_Type.TYPE_UINT32:
return const BaseType._raw(FieldDescriptorProto_Type.TYPE_UINT32, "U3",
"int", r"$_setUnsignedInt32", null);
Expand Down
3 changes: 2 additions & 1 deletion lib/client_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ClientApiGenerator {
'ClientContext ctx, $inputType request) {',
'}', () {
out.println('var emptyResponse = new $outputType();');
out.println('return _client.invoke(ctx, \'${service._descriptor.name}\', '
out.println(
'return _client.invoke<$outputType>(ctx, \'${service._descriptor.name}\', '
'\'${m.name}\', request, emptyResponse);');
});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ class ProtobufField {
'0' == descriptor.defaultValue) {
return null;
} else if (descriptor.defaultValue == 'inf') {
return 'double.INFINITY';
return 'double.infinity';
} else if (descriptor.defaultValue == '-inf') {
return 'double.NEGATIVE_INFINITY';
return 'double.negativeInfinity';
} else if (descriptor.defaultValue == 'nan') {
return 'double.NAN';
return 'double.nan';
} else if (HEX_LITERAL_REGEX.hasMatch(descriptor.defaultValue)) {
return '(${descriptor.defaultValue}).toDouble()';
} else if (INTEGER_LITERAL_REGEX.hasMatch(descriptor.defaultValue)) {
Expand Down
18 changes: 9 additions & 9 deletions lib/src/descriptor.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ class DescriptorProto_ExtensionRange extends GeneratedMessage {

int get start => $_get(0, 0);
set start(int v) {
$_setUnsignedInt32(0, v);
$_setSignedInt32(0, v);
}

bool hasStart() => $_has(0);
void clearStart() => clearField(1);

int get end => $_get(1, 0);
set end(int v) {
$_setUnsignedInt32(1, v);
$_setSignedInt32(1, v);
}

bool hasEnd() => $_has(1);
Expand Down Expand Up @@ -238,15 +238,15 @@ class DescriptorProto_ReservedRange extends GeneratedMessage {

int get start => $_get(0, 0);
set start(int v) {
$_setUnsignedInt32(0, v);
$_setSignedInt32(0, v);
}

bool hasStart() => $_has(0);
void clearStart() => clearField(1);

int get end => $_get(1, 0);
set end(int v) {
$_setUnsignedInt32(1, v);
$_setSignedInt32(1, v);
}

bool hasEnd() => $_has(1);
Expand Down Expand Up @@ -412,7 +412,7 @@ class FieldDescriptorProto extends GeneratedMessage {

int get number => $_get(2, 0);
set number(int v) {
$_setUnsignedInt32(2, v);
$_setSignedInt32(2, v);
}

bool hasNumber() => $_has(2);
Expand Down Expand Up @@ -460,7 +460,7 @@ class FieldDescriptorProto extends GeneratedMessage {

int get oneofIndex => $_get(8, 0);
set oneofIndex(int v) {
$_setUnsignedInt32(8, v);
$_setSignedInt32(8, v);
}

bool hasOneofIndex() => $_has(8);
Expand Down Expand Up @@ -624,7 +624,7 @@ class EnumValueDescriptorProto extends GeneratedMessage {

int get number => $_get(1, 0);
set number(int v) {
$_setUnsignedInt32(1, v);
$_setSignedInt32(1, v);
}

bool hasNumber() => $_has(1);
Expand Down Expand Up @@ -1658,15 +1658,15 @@ class GeneratedCodeInfo_Annotation extends GeneratedMessage {

int get begin => $_get(2, 0);
set begin(int v) {
$_setUnsignedInt32(2, v);
$_setSignedInt32(2, v);
}

bool hasBegin() => $_has(2);
void clearBegin() => clearField(3);

int get end => $_get(3, 0);
set end(int v) {
$_setUnsignedInt32(3, v);
$_setSignedInt32(3, v);
}

bool hasEnd() => $_has(3);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/plugin.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ class Version extends GeneratedMessage {

int get major => $_get(0, 0);
set major(int v) {
$_setUnsignedInt32(0, v);
$_setSignedInt32(0, v);
}

bool hasMajor() => $_has(0);
void clearMajor() => clearField(1);

int get minor => $_get(1, 0);
set minor(int v) {
$_setUnsignedInt32(1, v);
$_setSignedInt32(1, v);
}

bool hasMinor() => $_has(1);
void clearMinor() => clearField(2);

int get patch => $_get(2, 0);
set patch(int v) {
$_setUnsignedInt32(2, v);
$_setSignedInt32(2, v);
}

bool hasPatch() => $_has(2);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: protoc_plugin
version: 0.7.11
version: 0.8.0
author: Dart Team <misc@dartlang.org>
description: Protoc compiler plugin to generate Dart code
homepage: https://github.com/dart-lang/dart-protoc-plugin
Expand All @@ -8,7 +8,7 @@ environment:
dependencies:
fixnum: ^0.10.5
path: ^1.0.0
protobuf: ^0.7.1
protobuf: ^0.8.0
dart_style: ^1.0.6
dev_dependencies:
browser: any
Expand Down
4 changes: 2 additions & 2 deletions test/client_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class TestApi {
Future<SomeReply> aMethod(ClientContext ctx, SomeRequest request) {
var emptyResponse = new SomeReply();
return _client.invoke(ctx, 'Test', 'AMethod', request, emptyResponse);
return _client.invoke<SomeReply>(ctx, 'Test', 'AMethod', request, emptyResponse);
}
Future<$foo$bar.AnotherReply> anotherMethod(ClientContext ctx, $foo$bar.EmptyMessage request) {
var emptyResponse = new $foo$bar.AnotherReply();
return _client.invoke(ctx, 'Test', 'AnotherMethod', request, emptyResponse);
return _client.invoke<$foo$bar.AnotherReply>(ctx, 'Test', 'AnotherMethod', request, emptyResponse);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/file_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class PhoneNumber extends GeneratedMessage {
void clearNumber() => clearField(1);
int get type => $_get(1, 0);
set type(int v) { $_setUnsignedInt32(1, v); }
set type(int v) { $_setSignedInt32(1, v); }
bool hasType() => $_has(1);
void clearType() => clearField(2);
Expand Down Expand Up @@ -362,7 +362,7 @@ class TestApi {
Future<Empty> ping(ClientContext ctx, Empty request) {
var emptyResponse = new Empty();
return _client.invoke(ctx, 'Test', 'Ping', request, emptyResponse);
return _client.invoke<Empty>(ctx, 'Test', 'Ping', request, emptyResponse);
}
}
Expand Down
16 changes: 13 additions & 3 deletions test/repeated_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

library repeated_field_test;

import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';

import '../out/protos/google/protobuf/unittest.pb.dart';
Expand Down Expand Up @@ -47,13 +48,22 @@ void main() {

test("check properties are initialized for repeated fields", () {
var msg = new TestAllTypes();
expect(msg.info_.byName["repeatedNestedMessage"].check,
expect(
(msg.info_.byName["repeatedNestedMessage"]
as FieldInfo<TestAllTypes_NestedMessage>)
.check,
same(TestAllTypes_NestedMessage.$checkItem));

expect(msg.info_.byName["repeatedGroup"].check,
expect(
(msg.info_.byName["repeatedGroup"]
as FieldInfo<TestAllTypes_RepeatedGroup>)
.check,
same(TestAllTypes_RepeatedGroup.$checkItem));

expect(msg.info_.byName["repeatedNestedEnum"].check,
expect(
(msg.info_.byName["repeatedNestedEnum"]
as FieldInfo<TestAllTypes_NestedEnum>)
.check,
same(TestAllTypes_NestedEnum.$checkItem));
});
}
4 changes: 2 additions & 2 deletions test/service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class FakeJsonClient implements RpcClient {

FakeJsonClient(this.server);

Future<GeneratedMessage> invoke(
Future<T> invoke<T extends GeneratedMessage>(
ClientContext ctx,
String serviceName,
String methodName,
GeneratedMessage request,
GeneratedMessage response) async {
T response) async {
String requestJson = request.writeToJson();
String replyJson =
await server.messageHandler(serviceName, methodName, requestJson);
Expand Down

0 comments on commit a957cb2

Please sign in to comment.