Skip to content
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
66 changes: 33 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,38 @@ jobs:
- name: Run tests (platforms)
run: make test-platforms

linux:
name: Ubuntu
runs-on: ubuntu-latest
strategy:
matrix:
swift:
- '6.2.3'
steps:
- uses: actions/checkout@v5
- name: Install Swift ${{ matrix.swift }}
uses: swift-actions/setup-swift@v3
with:
swift-version: ${{ matrix.swift }}
- name: Print Swift version
run: swift --version
- name: Run tests
run: make test-swift
# linux:
# name: Ubuntu
# runs-on: ubuntu-latest
# strategy:
# matrix:
# swift:
# - '6.2.3'
# steps:
# - uses: actions/checkout@v5
# - name: Install Swift ${{ matrix.swift }}
# uses: swift-actions/setup-swift@v3
# with:
# swift-version: ${{ matrix.swift }}
# - name: Print Swift version
# run: swift --version
# - name: Run tests
# run: make test-swift

wasm:
name: Wasm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Install Swift 6.2.3
uses: swift-actions/setup-swift@v3
with:
swift-version: '6.2.3'
- uses: bytecodealliance/actions/wasmtime/setup@v1
- name: Install Swift SDK for WebAssembly
run: |
set -ex
swift sdk install https://download.swift.org/swift-6.2.3-release/wasm-sdk/swift-6.2.3-RELEASE/swift-6.2.3-RELEASE_wasm.artifactbundle.tar.gz --checksum 394040ecd5260e68bb02f6c20aeede733b9b90702c2204e178f3e42413edad2a
# wasm:
# name: Wasm
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v5
# - name: Install Swift 6.2.3
# uses: swift-actions/setup-swift@v3
# with:
# swift-version: '6.2.3'
# - uses: bytecodealliance/actions/wasmtime/setup@v1
# - name: Install Swift SDK for WebAssembly
# run: |
# set -ex
# swift sdk install https://download.swift.org/swift-6.2.3-release/wasm-sdk/swift-6.2.3-RELEASE/swift-6.2.3-RELEASE_wasm.artifactbundle.tar.gz --checksum 394040ecd5260e68bb02f6c20aeede733b9b90702c2204e178f3e42413edad2a

- name: Build
run: swift build --swift-sdk swift-6.2.3-RELEASE_wasm --target CustomDump -Xlinker -z -Xlinker stack-size=$((1024 * 1024))
# - name: Build
# run: swift build --swift-sdk swift-6.2.3-RELEASE_wasm --target CustomDump -Xlinker -z -Xlinker stack-size=$((1024 * 1024))
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions Sources/CustomDump/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,13 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
let lhsItem = lhs._objectIdentifier
let rhsItem = rhs._objectIdentifier
if lhsItem == rhsItem {
let customType = lhs._customDiffType
let (lhs, rhs) = lhs._customDiffValues
let subjectType = typeName(type(of: lhs))
let lhsMirror = Mirror(customDumpReflecting: lhs)
let rhsMirror = Mirror(customDumpReflecting: rhs)
let subjectType =
customType.map { typeName($0) }
?? typeName(lhsMirror.subjectType)
var occurrence = tracker.occurrencePerType[subjectType, default: 1] {
didSet { tracker.occurrencePerType[subjectType] = occurrence }
}
Expand All @@ -367,22 +372,19 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
print(
"\(lhsName.map { "\($0): " } ?? "")#\(id) \(subjectType)(↩︎)\(separator)"
.indenting(by: indent)
.indenting(with: format.first + " "),
to: &out
)
print(
"\(rhsName.map { "\($0): " } ?? "")#\(id) \(subjectType)(↩︎)\(separator)"
.indenting(by: indent)
.indenting(with: format.second + " "),
.indenting(with: format.both + " "),
terminator: "",
to: &out
)
} else {
let id = id
tracker.visitedItems.insert(lhsItem)
occurrence += 1
diffChildren(
lhs: lhs,
rhs: rhs,
Mirror(customDumpReflecting: lhs),
Mirror(customDumpReflecting: rhs),
lhsMirror,
rhsMirror,
lhsName: "\(lhsName.map { "\($0): " } ?? "")#\(id)",
rhsName: "\(rhsName.map { "\($0): " } ?? "")#\(id)",
nameSuffix: "",
Expand All @@ -393,8 +395,6 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
collapseUnchanged: false,
filter: macroPropertyFilter(for: lhs)
)
tracker.visitedItems.insert(lhsItem)
occurrence += 1
}
} else {
diffEverything()
Expand Down Expand Up @@ -806,9 +806,14 @@ private struct Line: CustomDumpStringConvertible, Identifiable {

public protocol _CustomDiffObject {
var _customDiffValues: (Any, Any) { get }
var _customDiffType: Any.Type? { get }
var _objectIdentifier: ObjectIdentifier { get }
}

extension _CustomDiffObject {
public var _customDiffType: Any.Type? { nil }
}

extension _CustomDiffObject where Self: AnyObject {
public var _objectIdentifier: ObjectIdentifier {
ObjectIdentifier(self)
Expand Down
34 changes: 24 additions & 10 deletions Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,12 @@ func _customDump<T, TargetStream>(

case (let value as _CustomDiffObject, _):
let item = value._objectIdentifier
let customType = value._customDiffType
let (_, value) = value._customDiffValues
let subjectType = typeName(type(of: value))
let valueMirror = Mirror(customDumpReflecting: value)
let subjectType =
customType.map { typeName($0) }
?? typeName(valueMirror.subjectType)
var occurrence = tracker.occurrencePerType[subjectType, default: 1] {
didSet { tracker.occurrencePerType[subjectType] = occurrence }
}
Expand All @@ -208,15 +212,25 @@ func _customDump<T, TargetStream>(
} else {
tracker.visitedItems.insert(item)
occurrence += 1
customDumpHelp(
value,
to: &out,
name: nil,
nameSuffix: "",
indent: 0,
isRoot: false,
maxDepth: maxDepth
)
if customType != nil {
dumpChildren(
of: valueMirror,
prefix: "\(subjectType)(",
suffix: ")",
shouldSort: false,
filter: macroPropertyFilter(for: value)
)
} else {
customDumpHelp(
value,
to: &out,
name: nil,
nameSuffix: "",
indent: 0,
isRoot: false,
maxDepth: maxDepth
)
}
}

case (let value as CustomDumpRepresentable, _):
Expand Down
5 changes: 5 additions & 0 deletions Sources/CustomDump/Internal/Identifiable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
func isIdentityEqual(_ lhs: Any, _ rhs: Any) -> Bool {
if let lhs = lhs as? any _CustomDiffObject,
let rhs = rhs as? any _CustomDiffObject
{
return lhs._objectIdentifier == rhs._objectIdentifier
}
guard let lhs = lhs as? any Identifiable else { return false }
func open<LHS: Identifiable>(_ lhs: LHS) -> Bool {
guard let rhs = rhs as? LHS else { return false }
Expand Down
34 changes: 33 additions & 1 deletion Sources/CustomDump/Internal/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ extension Mirror {
}

func isMirrorEqual(_ lhs: Any, _ rhs: Any) -> Bool {
var visitedPairs: Set<MirrorPair> = []
return isMirrorEqual(lhs, rhs, visitedPairs: &visitedPairs)
}

private struct MirrorPair: Hashable {
let lhs: ObjectIdentifier
let rhs: ObjectIdentifier
}

private func mirrorIdentifier(_ value: Any) -> ObjectIdentifier? {
if let value = value as? any _CustomDiffObject {
return value._objectIdentifier
}
let valueType = type(of: value)
guard valueType is AnyClass else { return nil }
return ObjectIdentifier(value as AnyObject)
}

private func isMirrorEqual(
_ lhs: Any,
_ rhs: Any,
visitedPairs: inout Set<MirrorPair>
) -> Bool {
if let lhsIdentifier = mirrorIdentifier(lhs),
let rhsIdentifier = mirrorIdentifier(rhs)
{
let pair = MirrorPair(lhs: lhsIdentifier, rhs: rhsIdentifier)
if visitedPairs.contains(pair) {
return true
}
visitedPairs.insert(pair)
}
guard let lhs = lhs as? any Equatable else {
let lhsType = type(of: lhs)
if lhsType is AnyClass, lhsType == type(of: rhs), lhs as AnyObject === rhs as AnyObject {
Expand All @@ -45,7 +77,7 @@ func isMirrorEqual(_ lhs: Any, _ rhs: Any) -> Bool {
for (lhsChild, rhsChild) in zip(lhsMirror.children, rhsMirror.children) {
guard
lhsChild.label == rhsChild.label,
isMirrorEqual(lhsChild.value, rhsChild.value)
isMirrorEqual(lhsChild.value, rhsChild.value, visitedPairs: &visitedPairs)
else { return false }
}
return true
Expand Down
Loading
Loading