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

Use Swift Foundation Result type to represent a value and error in ex… #126

Merged
merged 1 commit into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two spaces at the end of this line (creates a newline)

[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 Expand Up @@ -651,7 +647,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.squareup.CleanseGithubBrowserTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CleanseGithubBrowser.app/CleanseGithubBrowser";
};
name = Debug;
Expand All @@ -664,7 +660,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.squareup.CleanseGithubBrowserTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CleanseGithubBrowser.app/CleanseGithubBrowser";
};
name = Release;
Expand All @@ -676,7 +672,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.squareup.CleanseGithubBrowserUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TEST_TARGET_NAME = CleanseGithubBrowser;
};
name = Debug;
Expand All @@ -688,7 +684,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.squareup.CleanseGithubBrowserUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
TEST_TARGET_NAME = CleanseGithubBrowser;
};
name = Release;
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