Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #126 from DJBen/sihao/example-project-use-result
Browse files Browse the repository at this point in the history
Use Swift Foundation Result type to represent a value and error in ex…
  • Loading branch information
sebastianv1 authored Jan 31, 2020
2 parents 41bb59e + c1a76bd commit 030011f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 68 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@
* Retain subcomponent graphs
[Sebastian Shanus](https://github.com/sebastianv1)
[#42](https://github.com/square/Cleanse/issues/42)

#### Improvements

* Replace demo app's ErrorOptional type with Swift Foundation Result type
[Sihao Lu](https://github.com/DJBen)
[#126](https://github.com/square/Cleanse/pull/126)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
F69184B31D0DF28A00125872 /* GithubMemberService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184B21D0DF28A00125872 /* GithubMemberService.swift */; };
F69184B61D0DF2CA00125872 /* GithubRepositoriesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184B51D0DF2CA00125872 /* GithubRepositoriesService.swift */; };
F69184BB1D0DF3DD00125872 /* NetworkModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184BA1D0DF3DD00125872 /* NetworkModule.swift */; };
F69184BD1D0DF5DC00125872 /* ErrorOptional.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184BC1D0DF5DC00125872 /* ErrorOptional.swift */; };
F69184BF1D0DF70F00125872 /* HTTPError.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184BE1D0DF70F00125872 /* HTTPError.swift */; };
F69184C31D0DFB4A00125872 /* GithubServicesModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184C21D0DFB4A00125872 /* GithubServicesModule.swift */; };
F69184C61D0E015A00125872 /* NSURLSessionAdditions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69184C51D0E015A00125872 /* NSURLSessionAdditions.swift */; };
Expand Down Expand Up @@ -107,7 +106,6 @@
F69184B21D0DF28A00125872 /* GithubMemberService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GithubMemberService.swift; sourceTree = "<group>"; };
F69184B51D0DF2CA00125872 /* GithubRepositoriesService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GithubRepositoriesService.swift; sourceTree = "<group>"; };
F69184BA1D0DF3DD00125872 /* NetworkModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkModule.swift; sourceTree = "<group>"; };
F69184BC1D0DF5DC00125872 /* ErrorOptional.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorOptional.swift; sourceTree = "<group>"; };
F69184BE1D0DF70F00125872 /* HTTPError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPError.swift; sourceTree = "<group>"; };
F69184C21D0DFB4A00125872 /* GithubServicesModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GithubServicesModule.swift; sourceTree = "<group>"; };
F69184C51D0E015A00125872 /* NSURLSessionAdditions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURLSessionAdditions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,7 +162,6 @@
F69184B71D0DF32800125872 /* Services */ = {
isa = PBXGroup;
children = (
F69184BC1D0DF5DC00125872 /* ErrorOptional.swift */,
F69184CF1D0E33F600125872 /* FakeModeSettings.swift */,
F69184CD1D0E31F200125872 /* FakeServices.swift */,
F69184B21D0DF28A00125872 /* GithubMemberService.swift */,
Expand Down Expand Up @@ -464,7 +461,6 @@
F6E878301D0BD911006F3FAB /* SettingsPage.swift in Sources */,
884618AF1D9324AD0001ADEB /* AppComponent.swift in Sources */,
F6E878321D0D1120006F3FAB /* RepositoriesPage.swift in Sources */,
F69184BD1D0DF5DC00125872 /* ErrorOptional.swift in Sources */,
F6E878381D0DD11A006F3FAB /* SplitViewController.swift in Sources */,
F69184D21D0E357600125872 /* FoundationCommonModule.swift in Sources */,
);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ struct FakeGithubServicesModule : GithubServicesModule {


struct FakeGithubMembersService : GithubMembersService {
func list(handler: @escaping (ErrorOptional<[GithubMember]>) -> Void) {
func list(handler: @escaping (Result<[GithubMember], Error>) -> Void) {
DispatchQueue.main.async() {
handler(.init([
handler(.success([
GithubMember(login: "abrons"),
GithubMember(login: "mikelikespie"),
GithubMember(login: "holmes"),
Expand All @@ -48,9 +48,9 @@ struct FakeGithubMembersService : GithubMembersService {
}

struct FakeGithubRepositoriesService : GithubRepositoriesService {
func list(_ handler: @escaping (ErrorOptional<[GithubRepository]>) -> Void) {
func list(_ handler: @escaping (Result<[GithubRepository], Error>) -> Void) {
DispatchQueue.main.async() {
handler(.init([
handler(.success([
GithubRepository(name: "okhttp", watchersCount: 11_917),
GithubRepository(name: "cleanse", watchersCount: 42)
]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct GithubMember {

/// Service that lists "Member" for the current organization
protocol GithubMembersService {
func list(handler: @escaping (ErrorOptional<[GithubMember]>) -> Void)
func list(handler: @escaping (Result<[GithubMember], Error>) -> Void)
}

struct GithubMembersServiceImpl : GithubMembersService {
Expand All @@ -30,7 +30,7 @@ struct GithubMembersServiceImpl : GithubMembersService {
let urlSession: URLSession

/// Lists members of an organization
func list(handler: @escaping (ErrorOptional<[GithubMember]>) -> Void) {
func list(handler: @escaping (Result<[GithubMember], Error>) -> Void) {
urlSession.jsonListTask(
baseURL: githubURL.get(),
pathComponents: "orgs", githubOrganizationName.get(), "public_members") { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct GithubRepository {

/// Service that lists repositories for the current user
protocol GithubRepositoriesService {
func list(_ handler: @escaping (ErrorOptional<[GithubRepository]>) -> Void)
func list(_ handler: @escaping (Result<[GithubRepository], Error>) -> Void)
}

struct GithubRepositoriesServiceImpl : GithubRepositoriesService {
Expand All @@ -32,7 +32,7 @@ struct GithubRepositoriesServiceImpl : GithubRepositoriesService {

let urlSession: URLSession

func list(_ handler: @escaping (ErrorOptional<[GithubRepository]>) -> Void) {
func list(_ handler: @escaping (Result<[GithubRepository], Error>) -> Void) {
urlSession.jsonListTask(
baseURL: githubURL.get(),
pathComponents: "users", githubOrganizationName.get(), "repos",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import Foundation


extension URLSession {
func jsonTask(baseURL: URL, pathComponents: String..., resultHandler: @escaping (ErrorOptional<Any>) -> Void) -> URLSessionDataTask {
func jsonTask(baseURL: URL, pathComponents: String..., resultHandler: @escaping (Result<Any, Error>) -> Void) -> URLSessionDataTask {
let url = baseURL.appendingPathComponent(pathComponents.joined(separator: "/"))
return jsonTask(url: url as URL, resultHandler: resultHandler)
}

private func jsonTask(url: URL, resultHandler: @escaping (ErrorOptional<Any>) -> Void) -> URLSessionDataTask {
private func jsonTask(url: URL, resultHandler: @escaping (Result<Any, Error>) -> Void) -> URLSessionDataTask {
let task = self.dataTask(with: url as URL) { (data, response, error) in
if let error: Error = error ?? HTTPError(statusCode: (response as! HTTPURLResponse).statusCode) {
resultHandler(ErrorOptional(error))
resultHandler(.failure(error))
return
}

do {
try resultHandler(ErrorOptional(JSONSerialization.jsonObject(with: data!, options: [])))
try resultHandler(.success(JSONSerialization.jsonObject(with: data!, options: [])))
} catch let e {
resultHandler(ErrorOptional(e))
resultHandler(.failure(e))
return
}
}
Expand All @@ -39,7 +39,7 @@ extension URLSession {
baseURL: URL,
pathComponents: String...,
query: String? = nil,
resultHandler: @escaping (ErrorOptional<[[String: AnyObject]]>) -> Void
resultHandler: @escaping (Result<[[String: AnyObject]], Error>) -> Void
) -> URLSessionDataTask {
var url = baseURL.appendingPathComponent(pathComponents.joined(separator: "/"))

Expand Down

0 comments on commit 030011f

Please sign in to comment.