Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SE-0185] Utilize synthesized Equatable and Hashable conformances #101

Merged
merged 1 commit into from
Aug 1, 2018
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
11 changes: 0 additions & 11 deletions Sources/Tentacle/Author.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,3 @@ public struct Author: ResourceType, Encodable {
self.email = email
}
}

extension Author: Hashable, Equatable {
public var hashValue: Int {
return name.hashValue ^ email.hashValue
}

static public func ==(lhs: Author, rhs: Author) -> Bool {
return lhs.name == rhs.name
&& lhs.email == rhs.email
}
}
24 changes: 1 addition & 23 deletions Sources/Tentacle/Branch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Repository {

public struct Branch: ResourceType {

public struct Commit: Decodable {
public struct Commit: ResourceType {
public let sha: SHA
}

Expand All @@ -35,25 +35,3 @@ public struct Branch: ResourceType {
}

}

extension Branch: Hashable {
public static func ==(lhs: Branch, rhs: Branch) -> Bool {
return lhs.name == rhs.name && lhs.commit == rhs.commit
}

public var hashValue: Int {
return name.hashValue ^ commit.hashValue
}
}

extension Branch.Commit: Hashable {
public var hashValue: Int {
return sha.hashValue
}
}

extension Branch.Commit: Equatable {
public static func ==(lhs: Branch.Commit, rhs: Branch.Commit) -> Bool {
return lhs.sha == rhs.sha
}
}
25 changes: 1 addition & 24 deletions Sources/Tentacle/Commit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct Commit: ResourceType {
}
}

extension Commit {
extension Commit: Hashable {
public var hashValue: Int {
return sha.hashValue
}
Expand All @@ -54,26 +54,3 @@ extension Commit {
return lhs.sha == rhs.sha
}
}

extension Commit.Author {
public var hashValue: Int {
return date.hashValue ^ name.hashValue ^ email.hashValue
}

public static func ==(lhs: Commit.Author, rhs: Commit.Author) -> Bool {
return lhs.date == rhs.date
&& lhs.name == rhs.name
&& lhs.email == rhs.email
}
}

extension Commit.Parent {
public var hashValue: Int {
return sha.hashValue ^ url.hashValue
}

public static func ==(lhs: Commit.Parent, rhs: Commit.Parent) -> Bool {
return lhs.sha == rhs.sha
&& lhs.url == rhs.url
}
}
40 changes: 6 additions & 34 deletions Sources/Tentacle/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Repository {
/// - directory: a directory when queried directly in a repository (may contain multiple files)
public enum Content: ResourceType {
/// A file in a repository
public struct File: CustomStringConvertible, Decodable {
public struct File: CustomStringConvertible, ResourceType {

public enum ContentTypeName: String, Decodable {
case file
Expand All @@ -45,7 +45,7 @@ public enum Content: ResourceType {
/// - directory: a directory in a repository
/// - symlink: a symlink in a repository not targeting a file inside the same repository
/// - submodule: a submodule in a repository
public enum ContentType: Decodable {
public enum ContentType: Decodable, Equatable {
/// A file a in a repository
case file(size: Int, downloadURL: URL?)

Expand Down Expand Up @@ -139,6 +139,10 @@ public enum Content: ResourceType {
case content
}

// Hashable
public var hashValue: Int {
return name.hashValue
}
}

case file(File)
Expand Down Expand Up @@ -182,35 +186,3 @@ extension Content: Hashable {
}
}
}

extension Content.File.ContentType: Equatable {
public static func ==(lhs: Content.File.ContentType, rhs: Content.File.ContentType) -> Bool {
switch (lhs, rhs) {
case let (.file(size, url), .file(size2, url2)):
return size == size2 && url == url2
case (.directory, .directory):
return true
case let (.submodule(url), .submodule(url2)):
return url == url2
case let (.symlink(target, url), .symlink(target2, url2)):
return target == target2 && url == url2
default:
return false
}
}
}

extension Content.File: Hashable {
public static func ==(lhs: Content.File, rhs: Content.File) -> Bool {
return lhs.name == rhs.name
&& lhs.path == rhs.path
&& lhs.sha == rhs.sha
&& lhs.content == rhs.content
}

public var hashValue: Int {
return name.hashValue
}
}


11 changes: 1 addition & 10 deletions Sources/Tentacle/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,9 @@ public struct File: ResourceType, Encodable {
try container.encode(content.base64EncodedString(), forKey: .content)
try container.encode(branch, forKey: .branch)
}
}

extension File: Hashable {
// Hashable
public var hashValue: Int {
return message.hashValue
}

public static func ==(lhs: File, rhs: File) -> Bool {
return lhs.message == rhs.message
&& lhs.committer == rhs.committer
&& lhs.author == rhs.committer
&& lhs.content == rhs.content
&& lhs.branch == rhs.branch
}
}
10 changes: 0 additions & 10 deletions Sources/Tentacle/FileResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,3 @@ public struct FileResponse: ResourceType {
/// Commit associated with the file that was created
public let commit: Commit
}

extension FileResponse {
public var hashValue: Int {
return content.hashValue ^ commit.hashValue
}

public static func ==(lhs: FileResponse, rhs: FileResponse) -> Bool {
return true
}
}
12 changes: 1 addition & 11 deletions Sources/Tentacle/GitHubError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// An error from the GitHub API.
public struct GitHubError: CustomStringConvertible, Error, Decodable {
public struct GitHubError: CustomStringConvertible, Error, ResourceType {
/// The error message from the API.
public let message: String

Expand All @@ -21,13 +21,3 @@ public struct GitHubError: CustomStringConvertible, Error, Decodable {
self.message = message
}
}

extension GitHubError: Hashable {
public static func ==(lhs: GitHubError, rhs: GitHubError) -> Bool {
return lhs.message == rhs.message
}

public var hashValue: Int {
return message.hashValue
}
}
18 changes: 1 addition & 17 deletions Sources/Tentacle/Identifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Identifiable {
}
}

public struct ID<Of: Identifiable>: Decodable {
public struct ID<Of: Identifiable>: Decodable, Hashable {
var rawValue: Int

public var string: String {
Expand All @@ -37,19 +37,3 @@ extension ID: ExpressibleByIntegerLiteral {
self.rawValue = value
}
}

extension ID: Hashable {

public var hashValue: Int {
return rawValue.hashValue
}

}

extension ID: Equatable {

static public func == (lhs: ID, rhs: ID) -> Bool {
return lhs.rawValue == rhs.rawValue
}

}
10 changes: 0 additions & 10 deletions Sources/Tentacle/Organization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,3 @@ public struct Organization: CustomStringConvertible, ResourceType {
return name
}
}

extension Organization: Hashable {
public static func ==(lhs: Organization, rhs: Organization) -> Bool {
return lhs.name == rhs.name
}

public var hashValue: Int {
return name.hashValue
}
}
10 changes: 0 additions & 10 deletions Sources/Tentacle/PullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,3 @@ public struct PullRequest: CustomStringConvertible, ResourceType {
case patchURL = "patch_url"
}
}

extension PullRequest: Hashable {
public static func ==(lhs: PullRequest, rhs: PullRequest) -> Bool {
return lhs.url == rhs.url
}

public var hashValue: Int {
return url.hashValue
}
}
14 changes: 3 additions & 11 deletions Sources/Tentacle/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal enum Method: String {
}

/// An opaque value representing a request to be made.
public struct Request<Value> {
public struct Request<Value>: Hashable {
internal var method: Method
internal var path: String
internal var queryItems: [URLQueryItem]
Expand All @@ -29,20 +29,12 @@ public struct Request<Value> {
self.queryItems = queryItems
self.body = body
}
}

extension Request: Hashable {
// Hashable
public var hashValue: Int {
return method.hashValue
^ path.hashValue
^ queryItems.map { $0.hashValue }.reduce(0, ^)
^ (self.body?.hashValue ?? 0)
}

public static func == (lhs: Request, rhs: Request) -> Bool {
return lhs.method == rhs.method
&& lhs.path == rhs.path
&& lhs.queryItems == rhs.queryItems
&& lhs.body == rhs.body
^ (body?.hashValue ?? 0)
}
}
11 changes: 2 additions & 9 deletions Sources/Tentacle/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private func linksInLinkHeader(_ header: String) -> [String: URL] {
}

/// A response from the GitHub API.
public struct Response {
public struct Response: Hashable {
/// The number of requests remaining in the current rate limit window, or nil if the server
/// isn't rate-limited.
public let rateLimitRemaining: UInt?
Expand All @@ -61,15 +61,8 @@ public struct Response {
.map { Date(timeIntervalSince1970: $0) }
self.links = linksInLinkHeader(headerFields["Link"] as String? ?? "")
}
}

extension Response: Hashable {
public static func ==(lhs: Response, rhs: Response) -> Bool {
return lhs.rateLimitRemaining == rhs.rateLimitRemaining
&& lhs.rateLimitReset == rhs.rateLimitReset
&& lhs.links == rhs.links
}

// Hashable
public var hashValue: Int {
return (rateLimitRemaining?.hashValue ?? 0)
^ (rateLimitReset?.hashValue ?? 0)
Expand Down
10 changes: 0 additions & 10 deletions Sources/Tentacle/Sha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ public struct SHA: ResourceType, Encodable {
}
}

extension SHA {
public var hashValue: Int {
return hash.hashValue
}

public static func ==(lhs: SHA, rhs: SHA) -> Bool {
return lhs.hash == rhs.hash
}
}

extension SHA: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.hash = value
Expand Down
36 changes: 1 addition & 35 deletions Sources/Tentacle/Tree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,47 +173,13 @@ public struct Tree: CustomStringConvertible, ResourceType {
}
}
}
}

extension Tree: Hashable {
public static func ==(lhs: Tree, rhs: Tree) -> Bool {
return lhs.sha == rhs.sha
&& lhs.url == rhs.url
&& lhs.entries == rhs.entries
&& lhs.isTruncated == rhs.isTruncated
}

// Hashable
public var hashValue: Int {
return sha.hashValue
}
}

extension Tree.Entry.EntryType: Hashable, Equatable {
public var hashValue: Int {
switch self {
case let .blob(url: url, size: size):
return "blob".hashValue ^ url.hashValue ^ size.hashValue
case let .tree(url):
return "tree".hashValue ^ url.hashValue
case .commit:
return "commit".hashValue
}
}

static public func ==(lhs: Tree.Entry.EntryType, rhs: Tree.Entry.EntryType) -> Bool {
switch (lhs, rhs) {
case let (.blob(url1, size1), .blob(url2, size2)):
return url1 == url2 && size1 == size2
case let (.tree(url1), .tree(url2)):
return url1 == url2
case (.commit, .commit):
return true
default:
return false
}
}
}

extension Tree.Entry: Hashable {
public static func ==(lhs: Tree.Entry, rhs: Tree.Entry) -> Bool {
return lhs.sha == rhs.sha
Expand Down
Loading