Skip to content

Commit 5ef1e96

Browse files
Added CustomDebugStringConvertible conformance to TransactionOptions
1 parent f85dc7d commit 5ef1e96

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

Sources/CodableDatastore/Persistence/TransactionOptions.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
/// A set of options that the caller of a transaction can specify.
12-
public struct TransactionOptions: OptionSet, Sendable {
12+
public struct TransactionOptions: OptionSet, Sendable, CustomDebugStringConvertible {
1313
public let rawValue: UInt64
1414

1515
public init(rawValue: UInt64) {
@@ -24,12 +24,20 @@ public struct TransactionOptions: OptionSet, Sendable {
2424

2525
/// The transaction is idempotent and does not modify any other kind of state, and can be retried when it encounters an inconsistency. This allows a transaction to concurrently operate with other writes, which may be necessary in a disptributed environment.
2626
public static let idempotent = Self(rawValue: 1 << 2)
27+
28+
public var debugDescription: String {
29+
var tokens: [String] = []
30+
if contains(.readOnly) { tokens.append(".readOnly") }
31+
if contains(.collateWrites) { tokens.append(".collateWrites") }
32+
if contains(.idempotent) { tokens.append(".idempotent") }
33+
return "TransactionOptions([\(tokens.joined(separator: ", "))])"
34+
}
2735
}
2836

2937
/// A set of options that the caller of a transaction can specify.
3038
///
3139
/// These options are generally unsafe to use improperly, and should generally not be used.
32-
public struct UnsafeTransactionOptions: OptionSet, Sendable {
40+
public struct UnsafeTransactionOptions: OptionSet, Sendable, CustomDebugStringConvertible {
3341
public let rawValue: UInt64
3442

3543
public init(rawValue: UInt64) {
@@ -54,4 +62,14 @@ public struct UnsafeTransactionOptions: OptionSet, Sendable {
5462

5563
/// The transaction should persist to storage even if it is a child transaction. Note that this must be the _first_ non-readonly child transaction of a parent transaction to succeed.
5664
public static let enforceDurability = Self(rawValue: 1 << 17)
65+
66+
public var debugDescription: String {
67+
var tokens: [String] = []
68+
if contains(.readOnly) { tokens.append(".readOnly") }
69+
if contains(.collateWrites) { tokens.append(".collateWrites") }
70+
if contains(.idempotent) { tokens.append(".idempotent") }
71+
if contains(.skipObservations) { tokens.append(".skipObservations") }
72+
if contains(.enforceDurability) { tokens.append(".enforceDurability") }
73+
return "UnsafeTransactionOptions([\(tokens.joined(separator: ", "))])"
74+
}
5775
}

Tests/CodableDatastoreTests/TransactionOptionsTests.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ final class TransactionOptionsTests: XCTestCase {
1919
XCTAssertEqual(options.rawValue, expectedRawValue, file: file, line: line)
2020
}
2121

22+
func assertTransactionOptions(
23+
_ options: TransactionOptions,
24+
haveDebugString expectedString: String,
25+
file: StaticString = #filePath,
26+
line: UInt = #line
27+
) {
28+
XCTAssertEqual(options.debugDescription, expectedString, file: file, line: line)
29+
}
30+
2231
func testTransactionOptions() {
2332
assertTransactionOptions(options: [], expectedRawValue: 0)
2433

@@ -39,6 +48,21 @@ final class TransactionOptionsTests: XCTestCase {
3948
assertTransactionOptions(options: TransactionOptions(rawValue: 10), expectedRawValue: 2)
4049
assertTransactionOptions(options: TransactionOptions(rawValue: 11), expectedRawValue: 3)
4150
}
51+
52+
func testDebugStrings() {
53+
assertTransactionOptions([], haveDebugString: "TransactionOptions([])")
54+
55+
assertTransactionOptions(.readOnly, haveDebugString: "TransactionOptions([.readOnly])")
56+
assertTransactionOptions(.idempotent, haveDebugString: "TransactionOptions([.idempotent])")
57+
assertTransactionOptions(.collateWrites, haveDebugString: "TransactionOptions([.collateWrites])")
58+
59+
assertTransactionOptions([.idempotent, .readOnly], haveDebugString: "TransactionOptions([.readOnly, .idempotent])")
60+
assertTransactionOptions([.readOnly, .idempotent], haveDebugString: "TransactionOptions([.readOnly, .idempotent])")
61+
assertTransactionOptions([.readOnly, .collateWrites], haveDebugString: "TransactionOptions([.readOnly, .collateWrites])")
62+
assertTransactionOptions([.idempotent, .collateWrites], haveDebugString: "TransactionOptions([.collateWrites, .idempotent])")
63+
64+
assertTransactionOptions([.readOnly, .idempotent, .collateWrites], haveDebugString: "TransactionOptions([.readOnly, .collateWrites, .idempotent])")
65+
}
4266
}
4367

4468
final class UnsafeTransactionOptionsTests: XCTestCase {
@@ -51,6 +75,15 @@ final class UnsafeTransactionOptionsTests: XCTestCase {
5175
XCTAssertEqual(options.rawValue, expectedRawValue, file: file, line: line)
5276
}
5377

78+
func assertUnsafeTransactionOptions(
79+
_ options: UnsafeTransactionOptions,
80+
haveDebugString expectedString: String,
81+
file: StaticString = #filePath,
82+
line: UInt = #line
83+
) {
84+
XCTAssertEqual(options.debugDescription, expectedString, file: file, line: line)
85+
}
86+
5487
func testUnsafeTransactionOptions() {
5588
assertUnsafeTransactionOptions(options: [], expectedRawValue: 0)
5689

@@ -87,4 +120,46 @@ final class UnsafeTransactionOptionsTests: XCTestCase {
87120

88121
assertUnsafeTransactionOptions(options: UnsafeTransactionOptions(TransactionOptions([.readOnly, .collateWrites, .idempotent])), expectedRawValue: 7)
89122
}
123+
124+
func testDebugStrings() {
125+
assertUnsafeTransactionOptions([], haveDebugString: "UnsafeTransactionOptions([])")
126+
127+
assertUnsafeTransactionOptions(.readOnly, haveDebugString: "UnsafeTransactionOptions([.readOnly])")
128+
assertUnsafeTransactionOptions(.idempotent, haveDebugString: "UnsafeTransactionOptions([.idempotent])")
129+
assertUnsafeTransactionOptions(.collateWrites, haveDebugString: "UnsafeTransactionOptions([.collateWrites])")
130+
assertUnsafeTransactionOptions(.skipObservations, haveDebugString: "UnsafeTransactionOptions([.skipObservations])")
131+
assertUnsafeTransactionOptions(.enforceDurability, haveDebugString: "UnsafeTransactionOptions([.enforceDurability])")
132+
133+
assertUnsafeTransactionOptions([.idempotent, .readOnly], haveDebugString: "UnsafeTransactionOptions([.readOnly, .idempotent])")
134+
assertUnsafeTransactionOptions([.readOnly, .idempotent], haveDebugString: "UnsafeTransactionOptions([.readOnly, .idempotent])")
135+
136+
assertUnsafeTransactionOptions([.readOnly, .collateWrites], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites])")
137+
assertUnsafeTransactionOptions([.readOnly, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.readOnly, .skipObservations])")
138+
assertUnsafeTransactionOptions([.readOnly, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .enforceDurability])")
139+
assertUnsafeTransactionOptions([.collateWrites, .idempotent], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .idempotent])")
140+
assertUnsafeTransactionOptions([.collateWrites, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .skipObservations])")
141+
assertUnsafeTransactionOptions([.collateWrites, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .enforceDurability])")
142+
assertUnsafeTransactionOptions([.idempotent, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.idempotent, .skipObservations])")
143+
assertUnsafeTransactionOptions([.idempotent, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.idempotent, .enforceDurability])")
144+
assertUnsafeTransactionOptions([.skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.skipObservations, .enforceDurability])")
145+
146+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent])")
147+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .skipObservations])")
148+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .enforceDurability])")
149+
assertUnsafeTransactionOptions([.readOnly, .idempotent, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.readOnly, .idempotent, .skipObservations])")
150+
assertUnsafeTransactionOptions([.readOnly, .idempotent, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .idempotent, .enforceDurability])")
151+
assertUnsafeTransactionOptions([.readOnly, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .skipObservations, .enforceDurability])")
152+
assertUnsafeTransactionOptions([.collateWrites, .idempotent, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .idempotent, .skipObservations])")
153+
assertUnsafeTransactionOptions([.collateWrites, .idempotent, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .idempotent, .enforceDurability])")
154+
assertUnsafeTransactionOptions([.collateWrites, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .skipObservations, .enforceDurability])")
155+
assertUnsafeTransactionOptions([.idempotent, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.idempotent, .skipObservations, .enforceDurability])")
156+
157+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .skipObservations], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .skipObservations])")
158+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .enforceDurability])")
159+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .skipObservations, .enforceDurability])")
160+
assertUnsafeTransactionOptions([.readOnly, .idempotent, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .idempotent, .skipObservations, .enforceDurability])")
161+
assertUnsafeTransactionOptions([.collateWrites, .idempotent, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.collateWrites, .idempotent, .skipObservations, .enforceDurability])")
162+
163+
assertUnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .skipObservations, .enforceDurability], haveDebugString: "UnsafeTransactionOptions([.readOnly, .collateWrites, .idempotent, .skipObservations, .enforceDurability])")
164+
}
90165
}

0 commit comments

Comments
 (0)