Skip to content

Commit 1b99789

Browse files
authored
Merge pull request #31 from k-kohey/refactor/mutation
Refactor mutation
2 parents 7c0782c + dc161bd commit 1b99789

File tree

5 files changed

+91
-80
lines changed

5 files changed

+91
-80
lines changed

Sources/Parchment/LoggerBundler.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final actor LoggerBundler {
1111
private let buffer: LogBuffer
1212
private let bufferFlowController: BufferFlowController
1313

14-
private(set) var transform: Transform
14+
private var mutations: [any Mutation] = []
1515

1616
public init(
1717
components: [any LoggerComponent],
@@ -22,17 +22,15 @@ public final actor LoggerBundler {
2222
self.components = components
2323
self.buffer = buffer
2424
self.bufferFlowController = bufferFlowController
25-
transform = mutations.composed()
25+
self.mutations = mutations
2626
}
2727

2828
public func add(component: LoggerComponent) {
2929
components.append(component)
3030
}
3131

3232
public func add(mutations: [Mutation]) {
33-
transform = [
34-
transform, mutations.composed()
35-
].composed()
33+
self.mutations += mutations
3634
}
3735

3836
/// Sends a Log to the retained LoggerComponents.
@@ -53,7 +51,7 @@ public final actor LoggerBundler {
5351
for logger in await loggers() {
5452
let payload = await Payload(
5553
destination: logger.id.value,
56-
event: transform(event, logger.id),
54+
event: mutations.transform(event, id: logger.id),
5755
timestamp: .init()
5856
)
5957

Sources/Parchment/Mutation.swift

+6-20
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,13 @@ public protocol Mutation: Sendable {
3131
func transform(_: any Loggable, id: LoggerComponentID) -> AnyLoggable
3232
}
3333

34-
private extension Mutation {
35-
var _transform: Transform {
36-
{
37-
self.transform($0, id: $1)
34+
extension Array: Mutation where Element == Mutation {
35+
public func transform(_ base: Loggable, id: LoggerComponentID) -> AnyLoggable {
36+
var result = AnyLoggable(base)
37+
for mutation in self {
38+
result = mutation.transform(result, id: id)
3839
}
40+
return result
3941
}
4042
}
4143

42-
extension Sequence where Element == Mutation {
43-
func composed() -> Transform {
44-
return map { $0._transform }.composed()
45-
}
46-
}
47-
48-
extension Sequence where Element == Transform {
49-
func composed() -> Transform {
50-
let base: Transform = { log, _ in AnyLoggable(log) }
51-
return reduce(base) { partialResult, transform in
52-
{
53-
transform(partialResult($0, $1), $1)
54-
}
55-
}
56-
}
57-
}

Tests/ParchmentDefaultTests/MutationTest.swift Tests/ParchmentDefaultTests/DeviceDataMutationTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// RegularlyPollingSchedulerTests.swift
2+
// DeviceDataMutationTests.swift
33
//
44
//
55
// Created by k-kohey on 2021/12/06.
@@ -27,16 +27,16 @@ private enum Event: Loggable {
2727
}
2828
}
2929

30-
class MutationTests: XCTestCase {
3130
#if canImport(UIKit)
31+
class DeviceDataMutationTests: XCTestCase {
3232
// 的確なテストに直す
3333
@MainActor func testTransform() async throws {
3434
let event = Event.hoge
3535
let mutation: [Mutation] = [DeviceDataMutation(device: .current)]
3636

37-
let newEvent = await mutation.composed()(event, .init("hoge"))
37+
let newEvent = await mutation.transform(event, id: .init("hoge"))
3838

3939
XCTAssertTrue(newEvent.parameters.count > 1)
4040
}
41-
#endif
4241
}
42+
#endif
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// MutationTests.swift
3+
//
4+
//
5+
// Created by Kohei Kawaguchi on 2023/05/18.
6+
//
7+
8+
@testable import Parchment
9+
import XCTest
10+
11+
private struct MutationMock: Mutation, @unchecked Sendable {
12+
let _transform: ((Loggable, LoggerComponentID) -> AnyLoggable)?
13+
14+
func transform(_ l: Loggable, id: LoggerComponentID) -> AnyLoggable {
15+
_transform!(l, id)
16+
}
17+
}
18+
19+
final class MutationTests: XCTestCase {
20+
func testTransform() async {
21+
let mutationA = MutationMock { l, id in
22+
AnyLoggable(
23+
eventName: l.eventName,
24+
parameters: l.parameters.merging(["hoge": 0], uniquingKeysWith: { _, r in
25+
r
26+
})
27+
)
28+
}
29+
let mutationB = MutationMock { l, id in
30+
AnyLoggable(
31+
eventName: l.eventName,
32+
parameters: l.parameters.merging(["fuga": 1], uniquingKeysWith: { _, r in
33+
r
34+
})
35+
)
36+
}
37+
38+
39+
let mutations: [any Mutation] = [mutationA, mutationB]
40+
41+
let r = await mutations.transform(AnyLoggable(eventName: "", parameters: ["foo": 2]), id: .init(""))
42+
43+
XCTAssertEqual(
44+
r.parameters as NSDictionary,
45+
["hoge": 0, "fuga": 1, "foo": 2]
46+
)
47+
}
48+
49+
func testTransform_later_is_higher_priority() async {
50+
let mutationA = MutationMock { l, id in
51+
AnyLoggable(
52+
eventName: l.eventName,
53+
parameters: l.parameters.merging(["hoge": 0], uniquingKeysWith: { _, r in
54+
r
55+
})
56+
)
57+
}
58+
let mutationB = MutationMock { l, id in
59+
AnyLoggable(
60+
eventName: l.eventName,
61+
parameters: l.parameters.merging(["hoge": 1], uniquingKeysWith: { _, r in
62+
r
63+
})
64+
)
65+
}
66+
67+
68+
let mutations: [any Mutation] = [mutationA, mutationB]
69+
70+
let r = await mutations.transform(AnyLoggable(eventName: "", parameters: ["hoge": ""]), id: .init(""))
71+
72+
XCTAssertEqual(
73+
r.parameters as NSDictionary,
74+
["hoge": 1]
75+
)
76+
}
77+
}

Tests/ParchmentTests/TransformTests.swift

-50
This file was deleted.

0 commit comments

Comments
 (0)