Skip to content

Commit

Permalink
Merge pull request #46 from Kuniwak/swift-v5-2nd
Browse files Browse the repository at this point in the history
Use synthesized Equatable and Hashables
  • Loading branch information
Kuniwak authored Mar 30, 2019
2 parents 1002043 + b1e7844 commit 044a67f
Show file tree
Hide file tree
Showing 34 changed files with 441 additions and 809 deletions.
2 changes: 1 addition & 1 deletion MirrorDiffKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/Kuniwak/MirrorDiffKit.git", :tag => s.version.to_s }
s.source_files = "Sources/**/*"
s.swift_versions = ['4.2', '5.0']
s.swift_versions = ['4.2', '5.0']
s.frameworks = "Foundation"
end
110 changes: 8 additions & 102 deletions MirrorDiffKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Sources/Diffable+CustomStringConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Diffable /*: CustomStringConvertible */ {
return "nil"

case let .string(type: type, content: content):
if type == String.self {
if type.actualType == String.self {
return "\"\(content)\""
}
return "\(type)(\"\(content)\")"
Expand All @@ -29,7 +29,7 @@ extension Diffable /*: CustomStringConvertible */ {
case let .url(url):
return url.absoluteString

case let .type(type):
case let .anyType(type):
return "\(type).self"

case let .tuple(type: _, entries: entries):
Expand Down
80 changes: 0 additions & 80 deletions Sources/Diffable+Equatable.swift

This file was deleted.

66 changes: 0 additions & 66 deletions Sources/Diffable+Hashable.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Diffable+PrettyPrintable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension Diffable: PrettyPrintable {
return [.line("nil")]

case let .string(type: type, content: content):
if type == String.self {
if type.actualType == String.self {
return [.line("\"\(content)\"")]
}
return [.line("\(type)(\"\(content)\")")]
Expand All @@ -25,7 +25,7 @@ extension Diffable: PrettyPrintable {
case let .url(url):
return [.line("\(url.absoluteString)")]

case let .type(type):
case let .anyType(type):
return [.line("\(type).self")]

case let .tuple(type: _, entries: entries):
Expand Down
14 changes: 0 additions & 14 deletions Sources/Diffable.CustomReflectableContent+Equatable.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Sources/Diffable.CustomReflectableContent+Hashable.swift

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/Diffable.DictionaryEntry+Equatable.swift

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/Diffable.DictionaryEntry+Hashable.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Sources/Diffable.TupleEntry+CustomStringConvertible.swift

This file was deleted.

16 changes: 0 additions & 16 deletions Sources/Diffable.TupleEntry+Equatable.swift

This file was deleted.

13 changes: 0 additions & 13 deletions Sources/Diffable.TupleEntry+Hashable.swift

This file was deleted.

52 changes: 31 additions & 21 deletions Sources/Diffable.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import Foundation


public indirect enum Diffable {
public indirect enum Diffable: Equatable, Hashable {
case null
case none
case string(type: Any.Type, content: String)
case number(type: Any.Type, value: String)
case string(type: HashableType, content: String)
case number(type: HashableType, value: String)
case bool(Bool)
case date(Date)
case url(URL)
case type(Any.Type)
case tuple(type: Any.Type, entries: [TupleEntry])
case collection(type: Any.Type, elements: [Diffable])

// XXX: We can collect only Hashable values into Sets, but we cannot know
// whether Diffable is a Hashable or not. And also we cannot cast to
// Hashable because it is a generic protocol. Therefore we cannot handle
// types that have a type restrictions.
case set(type: Any.Type, elements: [Diffable])

case dictionary(type: Any.Type, entries: [DictionaryEntry])
case anyEnum(type: Any.Type, caseName: EnumCaseName, associated: [TupleEntry])
case anyStruct(type: Any.Type, entries: [String: Diffable])
case anyClass(type: Any.Type, entries: [String: Diffable])
case minorCustomReflectable(type: Any.Type, content: CustomReflectableContent)
case anyType(HashableType)
case tuple(type: HashableType, entries: [TupleEntry])
case collection(type: HashableType, elements: [Diffable])
case set(type: HashableType, elements: Set<Diffable>)
case dictionary(type: HashableType, entries: Set<DictionaryEntry>)
case anyEnum(type: HashableType, caseName: EnumCaseName, associated: [TupleEntry])
case anyStruct(type: HashableType, entries: [String: Diffable])
case anyClass(type: HashableType, entries: [String: Diffable])
case minorCustomReflectable(type: HashableType, content: CustomReflectableContent)
case unrecognizable(debugInfo: String)


public enum TupleEntry {
public enum TupleEntry: Equatable, Hashable {
case labeled(label: String, value: Diffable)
case notLabeled(index: Int, value: Diffable)

Expand All @@ -41,14 +35,30 @@ public indirect enum Diffable {
return value
}
}


var description: String {
switch self {
case let .labeled(label: label, value: value):
return "\(label): \(value.description)"

case let .notLabeled(index: _, value: value):
return "\(value.description)"
}
}
}


public struct DictionaryEntry {
public struct DictionaryEntry: Equatable, Hashable {
public let key: Diffable
public let value: Diffable


public var description: String {
return "\(self.key.description): \(self.value.description)"
}


public init(entry: (key: Diffable, value: Diffable)) {
self.init(key: entry.key, value: entry.value)
}
Expand All @@ -61,7 +71,7 @@ public indirect enum Diffable {
}


public enum CustomReflectableContent {
public enum CustomReflectableContent: Equatable, Hashable {
// NOTE: Some builtin types such as UnicodeScalar and Character have only empty children, but have identifiable description.
// https://github.com/apple/swift/blob/ec5b51ec7c6f31e8d16bae762368032463bbac83/stdlib/public/core/Mirrors.swift.gyb#L21-L26
case empty(description: String)
Expand Down
Loading

0 comments on commit 044a67f

Please sign in to comment.