cleanup: eliminate a lot of type errors and casts
Generic type params FTW
MODIFY
lib/src/common/activity_service.dart
lib/src/common/activity_service.dart
@@ -280,7 +280,7 @@ class ActivityService extends Service {
return _github.postJSON("/repos/${slug.fullName}/subscription",
statusCode: StatusCodes.OK,
convert: RepositorySubscription.fromJSON,
- body: map) as Future<RepositorySubscription>;
+ body: map);
}
/// Deletes a Repository Subscription

MODIFY
lib/src/common/gists_service.dart
lib/src/common/gists_service.dart
@@ -68,9 +68,7 @@ class GistsService extends Service {
map["files"] = f;
return _github.postJSON("/gists",
- statusCode: 201,
- body: JSON.encode(map),
- convert: Gist.fromJSON) as Future<Gist>;
+ statusCode: 201, body: JSON.encode(map), convert: Gist.fromJSON);
}
/// Deletes the specified Gist.
@@ -102,9 +100,7 @@ class GistsService extends Service {
}
return _github.postJSON("/gists/$id",
- statusCode: 200,
- body: JSON.encode(map),
- convert: Gist.fromJSON) as Future<Gist>;
+ statusCode: 200, body: JSON.encode(map), convert: Gist.fromJSON);
}
// TODO: Implement listGistCommits: https://developer.github.com/v3/gists/#list-gist-commits
@@ -164,8 +160,7 @@ class GistsService extends Service {
/// API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
Future<GistComment> createComment(String gistId, CreateGistComment request) {
return _github.postJSON("/gists/$gistId/comments",
- body: request.toJSON(),
- convert: GistComment.fromJSON) as Future<GistComment>;
+ body: request.toJSON(), convert: GistComment.fromJSON);
}
// TODO: Implement editComment: https://developer.github.com/v3/gists/comments/#edit-a-comment

MODIFY
lib/src/common/git_service.dart
lib/src/common/git_service.dart
@@ -21,7 +21,7 @@ class GitService extends Service {
return _github.postJSON('/repos/${slug.fullName}/git/blobs',
convert: GitBlob.fromJSON,
statusCode: StatusCodes.CREATED,
- body: blob.toJSON()) as Future<GitBlob>;
+ body: blob.toJSON());
}
/// Fetches a commit from [slug] for a given [sha].
@@ -38,7 +38,7 @@ class GitService extends Service {
return _github.postJSON('/repos/${slug.fullName}/git/commits',
convert: GitCommit.fromJSON,
statusCode: StatusCodes.CREATED,
- body: commit.toJSON()) as Future<GitCommit>;
+ body: commit.toJSON());
}
/// Fetches a reference from a repository for the given [ref].
@@ -78,7 +78,7 @@ class GitService extends Service {
return _github.postJSON('/repos/${slug.fullName}/git/refs',
convert: GitReference.fromJSON,
statusCode: StatusCodes.CREATED,
- body: JSON.encode({'ref': ref, 'sha': sha})) as Future<GitReference>;
+ body: JSON.encode({'ref': ref, 'sha': sha}));
}
/// Updates a reference in a repository.
@@ -149,6 +149,6 @@ class GitService extends Service {
return _github.postJSON('/repos/${slug.fullName}/git/trees',
convert: (j) => new GitTree.fromJson(j),
statusCode: StatusCodes.CREATED,
- body: tree.toJSON()) as Future<GitTree>;
+ body: tree.toJSON());
}
}

MODIFY
lib/src/common/github.dart
lib/src/common/github.dart
@@ -204,12 +204,12 @@ class GitHub {
/// [convert] is a simple function that is passed this [GitHub] instance and a JSON object.
/// The future will pass the object returned from this function to the then method.
/// The default [convert] function returns the input object.
- Future<T> getJSON<T>(String path,
+ Future<T> getJSON<S, T>(String path,
{int statusCode,
void fail(http.Response response),
Map<String, String> headers,
Map<String, String> params,
- JSONConverter<T> convert,
+ JSONConverter<S, T> convert,
String preview}) async {
if (headers == null) headers = {};
@@ -218,7 +218,7 @@ class GitHub {
}
if (convert == null) {
- convert = (input) => input;
+ convert = (input) => input as T;
}
headers.putIfAbsent("Accept", () => "application/vnd.github.v3+json");
@@ -261,12 +261,12 @@ class GitHub {
/// The future will pass the object returned from this function to the then method.
/// The default [convert] function returns the input object.
/// [body] is the data to send to the server.
- Future<dynamic> postJSON(String path,
+ Future<T> postJSON<S, T>(String path,
{int statusCode,
void fail(http.Response response),
Map<String, String> headers,
Map<String, String> params,
- JSONConverter convert,
+ JSONConverter<S, T> convert,
String body,
String preview}) async {
if (headers == null) headers = {};
@@ -276,7 +276,7 @@ class GitHub {
}
if (convert == null) {
- convert = (input) => input;
+ convert = (input) => input as T;
}
headers.putIfAbsent("Accept", () => "application/vnd.github.v3+json");

MODIFY
lib/src/common/issues_service.dart
lib/src/common/issues_service.dart
@@ -211,7 +211,7 @@ class IssuesService extends Service {
'/repos/${slug.fullName}/issues/$issueNumber/comments',
body: it,
convert: IssueComment.fromJSON,
- statusCode: StatusCodes.CREATED) as Future<IssueComment>;
+ statusCode: StatusCodes.CREATED);
}
// TODO: Implement editComment: https://developer.github.com/v3/issues/comments/#edit-a-comment
@@ -249,7 +249,7 @@ class IssuesService extends Service {
RepositorySlug slug, String name, String color) {
return _github.postJSON("/repos/${slug.fullName}/labels",
body: JSON.encode({"name": name, "color": color}),
- convert: IssueLabel.fromJSON) as Future<IssueLabel>;
+ convert: IssueLabel.fromJSON);
}
/// Edits a label.
@@ -258,7 +258,7 @@ class IssuesService extends Service {
Future<IssueLabel> editLabel(RepositorySlug slug, String name, String color) {
return _github.postJSON("/repos/${slug.fullName}/labels/$name",
body: JSON.encode({"name": name, "color": color}),
- convert: IssueLabel.fromJSON) as Future<IssueLabel>;
+ convert: IssueLabel.fromJSON);
}
/// Deletes a label.
@@ -347,8 +347,7 @@ class IssuesService extends Service {
Future<Milestone> createMilestone(
RepositorySlug slug, CreateMilestone request) {
return _github.postJSON("/repos/${slug.fullName}/milestones",
- body: JSON.encode(request.toJSON()),
- convert: Milestone.fromJSON) as Future<Milestone>;
+ body: JSON.encode(request.toJSON()), convert: Milestone.fromJSON);
}
// TODO: Implement editMilestone: https://developer.github.com/v3/issues/milestones/#update-a-milestone

MODIFY
lib/src/common/orgs_service.dart
lib/src/common/orgs_service.dart
@@ -109,7 +109,7 @@ class OrganizationsService extends Service {
statusCode: 201,
convert: Team.fromJSON,
// TODO: This is probably wrong, map needs to be json encoded?
- body: map) as Future<Team>;
+ body: map);
}
/// Edits a Team.
@@ -124,7 +124,7 @@ class OrganizationsService extends Service {
statusCode: 200,
convert: Team.fromJSON,
// TODO: This is probably wrong, map needs to be json encoded?
- body: map) as Future<Team>;
+ body: map);
}
/// Deletes the team specified by the [teamId]
@@ -295,7 +295,7 @@ class OrganizationsService extends Service {
Future<Hook> createHook(String org, CreateHook hook) {
return _github.postJSON("/orgs/$org/hooks",
convert: (Map<String, dynamic> i) => Hook.fromJSON(org, i),
- body: hook.toJSON()) as Future<Hook>;
+ body: hook.toJSON());
}
// TODO: Implement editHook: https://developer.github.com/v3/orgs/hooks/#edit-a-hook

MODIFY
lib/src/common/pulls_service.dart
lib/src/common/pulls_service.dart
@@ -42,8 +42,7 @@ class PullRequestsService extends Service {
Future<PullRequestInformation> create(
RepositorySlug slug, CreatePullRequest request) {
return _github.postJSON("/repos/${slug.fullName}/pulls",
- convert: PullRequestInformation.fromJSON,
- body: request.toJSON()) as Future<PullRequestInformation>;
+ convert: PullRequestInformation.fromJSON, body: request.toJSON());
}
/// Edit a pull request.

MODIFY
lib/src/common/repos_service.dart
lib/src/common/repos_service.dart
@@ -82,12 +82,10 @@ class RepositoriesService extends Service {
{String org}) {
if (org != null) {
return _github.postJSON('/orgs/$org/repos',
- body: repository.toJSON(),
- convert: TeamRepository.fromJSON) as Future<Repository>;
+ body: repository.toJSON(), convert: TeamRepository.fromJSON);
} else {
return _github.postJSON('/user/repos',
- body: repository.toJSON(),
- convert: Repository.fromJSON) as Future<Repository>;
+ body: repository.toJSON(), convert: Repository.fromJSON);
}
}
@@ -183,7 +181,7 @@ class RepositoriesService extends Service {
Future<LanguageBreakdown> listLanguages(RepositorySlug slug) =>
_github.getJSON("/repos/${slug.fullName}/languages",
statusCode: StatusCodes.OK,
- convert: (Map<String, int> input) => new LanguageBreakdown(input));
+ convert: (input) => new LanguageBreakdown(input));
/// Lists the tags of the specified repository.
///
@@ -367,7 +365,7 @@ class RepositoriesService extends Service {
// TODO: map probably needs to be json encoded
body: map,
statusCode: 200,
- convert: ContentCreation.fromJSON) as Future<ContentCreation>;
+ convert: ContentCreation.fromJSON);
}
/// Deletes the specified file.
@@ -413,8 +411,7 @@ class RepositoriesService extends Service {
Future<Repository> createFork(RepositorySlug slug, [CreateFork fork]) {
if (fork == null) fork = new CreateFork();
return _github.postJSON("/repos/${slug.fullName}/forks",
- body: fork.toJSON(),
- convert: Repository.fromJSON) as Future<Repository>;
+ body: fork.toJSON(), convert: Repository.fromJSON);
}
/// Lists the hooks of the specified repository.
@@ -440,7 +437,7 @@ class RepositoriesService extends Service {
Future<Hook> createHook(RepositorySlug slug, CreateHook hook) {
return _github.postJSON("/repos/${slug.fullName}/hooks",
convert: (Map<String, dynamic> i) => Hook.fromJSON(slug.fullName, i),
- body: hook.toJSON()) as Future<Hook>;
+ body: hook.toJSON());
}
// TODO: Implement editHook: https://developer.github.com/v3/repos/hooks/#edit-a-hook
@@ -501,7 +498,7 @@ class RepositoriesService extends Service {
return _github.postJSON("/repos/${slug.fullName}/merges",
body: merge.toJSON(),
convert: RepositoryCommit.fromJSON,
- statusCode: 201) as Future<RepositoryCommit>;
+ statusCode: 201);
}
/// Fetches the GitHub pages information for the specified repository.
@@ -625,8 +622,7 @@ class RepositoriesService extends Service {
Future<RepositoryStatus> createStatus(
RepositorySlug slug, String ref, CreateStatus request) {
return _github.postJSON("/repos/${slug.fullName}/statuses/$ref",
- body: request.toJSON(),
- convert: RepositoryStatus.fromJSON) as Future<RepositoryStatus>;
+ body: request.toJSON(), convert: RepositoryStatus.fromJSON);
}
/// Gets a Combined Status for the specified repository and ref.

MODIFY
lib/src/common/search_service.dart
lib/src/common/search_service.dart
@@ -39,11 +39,9 @@ class SearchService extends Service {
return;
}
- List<dynamic> items = input['items'];
+ var items = input['items'] as List;
- items
- .map((Map<String, dynamic> item) => Repository.fromJSON(item))
- .forEach(controller.add);
+ items.map((item) => Repository.fromJSON(item)).forEach(controller.add);
}).onDone(controller.close);
return controller.stream;
@@ -87,11 +85,9 @@ class SearchService extends Service {
return;
}
- List<dynamic> items = input['items'];
+ var items = input['items'] as List;
- items
- .map((Map<String, dynamic> item) => User.fromJSON(item))
- .forEach(controller.add);
+ items.map((item) => User.fromJSON(item)).forEach(controller.add);
}).onDone(controller.close);
return controller.stream;

MODIFY
lib/src/common/users_service.dart
lib/src/common/users_service.dart
@@ -38,7 +38,7 @@ class UsersService extends Service {
// TODO: map probably needs to be JSON encoded.
body: map,
statusCode: 200,
- convert: CurrentUser.fromJSON) as Future<CurrentUser>;
+ convert: CurrentUser.fromJSON);
}
/// Fetches a list of users specified by [names].

MODIFY
lib/src/common/util/json.dart
lib/src/common/util/json.dart
part of github.common;
/// Creates a Model Object from the JSON [input]
-typedef T JSONConverter<T>(dynamic input);
+typedef T JSONConverter<S, T>(S input);

MODIFY
lib/src/common/util/pagination.dart
lib/src/common/util/pagination.dart
@@ -84,7 +84,8 @@ class PaginationHelper {
}
}
- Stream<T> objects<T>(String method, String path, JSONConverter<T> converter,
+ Stream<T> objects<S, T>(
+ String method, String path, JSONConverter<S, T> converter,
{int pages,
Map<String, String> headers,
Map<String, dynamic> params,

cleanup: eliminate a lot of type errors and casts

Generic type params FTW