Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from isoos/dart2_compat
Browse files Browse the repository at this point in the history
Use dart2_constant for Dart2 compatibility.
  • Loading branch information
jakobr-google authored May 25, 2018
2 parents dbdd15c + 6c17348 commit 36d1ccd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

* Update minimum Dart SDK to `1.23.0`.

* Dart 2 support with `dart2_constant`.

## 0.1.2

* Add support for generic exceptions raised e.g. due to `pubspec.yaml`
Expand Down
6 changes: 3 additions & 3 deletions example/src/examples/file_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
library appengine_pub.file_repository;

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:archive/archive.dart';
import 'package:dart2_constant/convert.dart' as convert;
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:pub_server/repository.dart';
Expand Down Expand Up @@ -80,7 +80,7 @@ class FileRepository extends PackageRepository {
}

// TODO: Error handling.
var pubspec = loadYaml(UTF8.decode(_getBytes(pubspecArchiveFile)));
var pubspec = loadYaml(convert.utf8.decode(_getBytes(pubspecArchiveFile)));

var package = pubspec['name'] as String;
var version = pubspec['version'] as String;
Expand All @@ -96,7 +96,7 @@ class FileRepository extends PackageRepository {
throw new StateError('`$package` already exists at version `$version`.');
}

var pubspecContent = UTF8.decode(_getBytes(pubspecArchiveFile));
var pubspecContent = convert.utf8.decode(_getBytes(pubspecArchiveFile));
pubspecFile.writeAsStringSync(pubspecContent);
new File(packageTarballPath(package, version))
.writeAsBytesSync(tarballBytes);
Expand Down
6 changes: 3 additions & 3 deletions example/src/examples/http_proxy_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
library appengine_pub.http_proxy_repository;

import 'dart:async';
import 'dart:convert';

import 'package:dart2_constant/convert.dart' as convert;
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:pub_server/repository.dart';
Expand All @@ -29,12 +29,12 @@ class HttpProxyRepository extends PackageRepository {
baseUrl.resolve('/api/packages/${Uri.encodeComponent(package)}');

http.Response response = await client.get(versionUrl);
var json = JSON.decode(response.body);
var json = convert.json.decode(response.body);
var versions = json['versions'] as List<Map>;
if (versions != null) {
for (var item in versions) {
var pubspec = item['pubspec'];
var pubspecString = JSON.encode(pubspec);
var pubspecString = convert.json.encode(pubspec);
yield new PackageVersion(pubspec['name'] as String,
pubspec['version'] as String, pubspecString);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/shelf_pubserver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
library pub_server.shelf_pubserver;

import 'dart:async';
import 'dart:convert';

import 'package:dart2_constant/convert.dart' as convert;
import 'package:http_parser/http_parser.dart';
import 'package:logging/logging.dart';
import 'package:mime/mime.dart';
Expand Down Expand Up @@ -268,7 +268,7 @@ class ShelfPubServer {

// TODO: The 'latest' is something we should get rid of, since it's
// duplicated in 'versions'.
var binaryJson = JSON.encoder.fuse(UTF8.encoder).convert({
var binaryJson = convert.json.encoder.fuse(convert.utf8.encoder).convert({
'name': package,
'latest': packageVersion2Json(latestVersion),
'versions': packageVersions.map(packageVersion2Json).toList(),
Expand Down Expand Up @@ -450,20 +450,20 @@ class ShelfPubServer {

Future<shelf.Response> _successfullRequest(String message) async {
return new shelf.Response(200,
body: JSON.encode({
body: convert.json.encode({
'success': {'message': message}
}),
headers: {'content-type': 'application/json'});
}

shelf.Response _unauthorizedRequest() => new shelf.Response(403,
body: JSON.encode({
body: convert.json.encode({
'error': {'message': 'Unauthorized request.'}
}),
headers: {'content-type': 'application/json'});

shelf.Response _badRequest(String message) => new shelf.Response(400,
body: JSON.encode({
body: convert.json.encode({
'error': {'message': message}
}),
headers: {'content-type': 'application/json'});
Expand All @@ -475,7 +475,7 @@ class ShelfPubServer {

shelf.Response _jsonResponse(Map json, {int status: 200}) =>
new shelf.Response(status,
body: JSON.encode(json),
body: convert.json.encode(json),
headers: {'content-type': 'application/json'});

// Download urls.
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:
sdk: '>=1.23.0 <2.0.0'

dependencies:
dart2_constant: ^1.0.1
http_parser: '^3.0.0'
logging: ">=0.9.3 <1.0.0"
mime: ">=0.9.3 <0.10.0"
Expand Down
42 changes: 22 additions & 20 deletions test/shelf_pubserver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
library pub_server.shelf_pubserver_test;

import 'dart:async';
import 'dart:convert';

import 'package:dart2_constant/convert.dart' as convert;
import 'package:shelf/shelf.dart' as shelf;
import 'package:pub_server/repository.dart';
import 'package:pub_server/shelf_pubserver.dart';
Expand Down Expand Up @@ -136,15 +136,16 @@ shelf.Request multipartRequest(Uri uri, List<int> bytes) {
var requestBytes = [];
String boundary = 'testboundary';

requestBytes.addAll(ASCII.encode('--$boundary\r\n'));
requestBytes.addAll(convert.ascii.encode('--$boundary\r\n'));
requestBytes.addAll(
convert.ascii.encode('Content-Type: application/octet-stream\r\n'));
requestBytes
.addAll(ASCII.encode('Content-Type: application/octet-stream\r\n'));
requestBytes.addAll(ASCII.encode('Content-Length: ${bytes.length}\r\n'));
requestBytes.addAll(ASCII.encode('Content-Disposition: '
.addAll(convert.ascii.encode('Content-Length: ${bytes.length}\r\n'));
requestBytes.addAll(convert.ascii.encode('Content-Disposition: '
'form-data; name="file"; '
'filename="package.tar.gz"\r\n\r\n'));
requestBytes.addAll(bytes);
requestBytes.addAll(ASCII.encode('\r\n--$boundary--\r\n'));
requestBytes.addAll(convert.ascii.encode('\r\n--$boundary--\r\n'));

var headers = {
'Content-Type': 'multipart/form-data; boundary="$boundary"',
Expand Down Expand Up @@ -200,7 +201,7 @@ main() {
test('successful retrieval of version', () async {
var mock = new RepositoryMock(versionsFun: (String package) {
// The pubspec is invalid, but that is irrelevant for this test.
var pubspec = JSON.encode({'foo': 1});
var pubspec = convert.json.encode({'foo': 1});
var analyzer = new PackageVersion('analyzer', '0.1.0', pubspec);
return new Stream.fromIterable([analyzer]);
});
Expand All @@ -211,14 +212,14 @@ main() {

expect(response.mimeType, equals('application/json'));
expect(response.statusCode, equals(200));
expect(JSON.decode(body), equals(expectedJson));
expect(convert.json.decode(body), equals(expectedJson));
});

test('successful retrieval of version - from cache', () async {
var mock = new RepositoryMock();
var cacheMock = new PackageCacheMock(getFun: expectAsync1((String pkg) {
expect(pkg, equals('analyzer'));
return UTF8.encode('json response');
return convert.utf8.encode('json response');
}));
var server = new ShelfPubServer(mock, cache: cacheMock);
var request = getRequest('/api/packages/analyzer');
Expand All @@ -233,7 +234,7 @@ main() {
test('successful retrieval of version - populate cache', () async {
var mock = new RepositoryMock(versionsFun: (String package) {
// The pubspec is invalid, but that is irrelevant for this test.
var pubspec = JSON.encode({'foo': 1});
var pubspec = convert.json.encode({'foo': 1});
var analyzer = new PackageVersion('analyzer', '0.1.0', pubspec);
return new Stream.fromIterable([analyzer]);
});
Expand All @@ -242,7 +243,8 @@ main() {
return null;
}), setFun: expectAsync2((String package, List<int> data) {
expect(package, equals('analyzer'));
expect(JSON.decode(UTF8.decode(data)), equals(expectedJson));
expect(convert.json.decode(convert.utf8.decode(data)),
equals(expectedJson));
}));
var server = new ShelfPubServer(mock, cache: cacheMock);
var request = getRequest('/api/packages/analyzer');
Expand All @@ -251,7 +253,7 @@ main() {

expect(response.mimeType, equals('application/json'));
expect(response.statusCode, equals(200));
expect(JSON.decode(body), equals(expectedJson));
expect(convert.json.decode(body), equals(expectedJson));
});
});

Expand All @@ -274,15 +276,15 @@ main() {
var body = await response.readAsString();

expect(response.statusCode, equals(400));
expect(JSON.decode(body)['error']['message'],
expect(convert.json.decode(body)['error']['message'],
'Version string "0.1.0+@" is not a valid semantic version.');
});

test('successful retrieval of version', () async {
var mock = new RepositoryMock(
lookupVersionFun: (String package, String version) {
// The pubspec is invalid, but that is irrelevant for this test.
var pubspec = JSON.encode({'foo': 1});
var pubspec = convert.json.encode({'foo': 1});
return new PackageVersion(package, version, pubspec);
});
var server = new ShelfPubServer(mock);
Expand All @@ -299,7 +301,7 @@ main() {

expect(response.mimeType, equals('application/json'));
expect(response.statusCode, equals(200));
expect(JSON.decode(body), equals(expectedJson));
expect(convert.json.decode(body), equals(expectedJson));
});
});

Expand Down Expand Up @@ -379,7 +381,7 @@ main() {
expect(response.statusCode, equals(200));
expect(response.headers['content-type'], equals('application/json'));

var jsonBody = JSON.decode(await response.readAsString());
var jsonBody = convert.json.decode(await response.readAsString());
expect(
jsonBody,
equals({
Expand All @@ -395,7 +397,7 @@ main() {
// Call the `finishUrl`.
request = new shelf.Request('GET', finishUrl);
response = await server.requestHandler(request);
jsonBody = JSON.decode(await response.readAsString());
jsonBody = convert.json.decode(await response.readAsString());
expect(
jsonBody,
equals({
Expand Down Expand Up @@ -432,7 +434,7 @@ main() {
var response = await server.requestHandler(request);
expect(response.statusCode, equals(200));
expect(response.headers['content-type'], equals('application/json'));
var jsonBody = JSON.decode(await response.readAsString());
var jsonBody = convert.json.decode(await response.readAsString());
expect(
jsonBody,
equals({
Expand All @@ -450,7 +452,7 @@ main() {
// Call the `finishUrl`.
request = new shelf.Request('GET', finishUrl);
response = await server.requestHandler(request);
jsonBody = JSON.decode(await response.readAsString());
jsonBody = convert.json.decode(await response.readAsString());
expect(
jsonBody,
equals({
Expand Down Expand Up @@ -483,7 +485,7 @@ main() {
// Call the `finishUrl`.
request = new shelf.Request('GET', finishUrl);
response = await server.requestHandler(request);
var jsonBody = JSON.decode(await response.readAsString());
var jsonBody = convert.json.decode(await response.readAsString());
expect(
jsonBody,
equals({
Expand Down

0 comments on commit 36d1ccd

Please sign in to comment.