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

fix warning #28

Merged
merged 1 commit into from
May 22, 2023
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
23 changes: 23 additions & 0 deletions Demo.swiftpm/Sources/AppModule/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ extension TrackingEvent {
}
}

struct TimestampMutation: Mutation {
func transform(_ e: Parchment.Loggable, id: Parchment.LoggerComponentID) -> Parchment.AnyLoggable {
var e = AnyLoggable(e)
e.parameters["createdAt"] = Date()
return e
}
}

struct UserIDMutation: Mutation {
let userID = 1

func transform(_ e: Parchment.Loggable, id: Parchment.LoggerComponentID) -> Parchment.AnyLoggable {
var e = AnyLoggable(e)
e.parameters["userID"] = userID
return e
}
}

let logger = LoggerBundler.make(
components: [MyLogger(), DebugLogger()],
bufferFlowController: DefaultBufferFlowController(pollingInterval: 5, delayInputLimit: 5)
Expand Down Expand Up @@ -66,6 +84,11 @@ struct ExampleAppApp: App {
.background(Color.gray)
.task {
await logger.startLogging()

await logger.add(
mutations: [TimestampMutation(), UserIDMutation()]
)

await logger.send(event: .impletion("home"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Parchment/LoggerBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final actor LoggerBundler {
}

public func add(mutations: [Mutation]) {
transform = ([transform, mutations.composed()]).composed()
transform = [transform, mutations.composed()].composed()
}

/// Sends a Log to the retained LoggerComponents.
Expand Down
15 changes: 12 additions & 3 deletions Sources/Parchment/Mutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,30 @@ public struct AnyLoggable: Loggable {
}
}

typealias Transform = (Loggable, LoggerComponentID) -> AnyLoggable
typealias Transform = @Sendable (Loggable, LoggerComponentID) -> AnyLoggable

public protocol Mutation: Sendable {
func transform(_: any Loggable, id: LoggerComponentID) -> AnyLoggable
}

private extension Mutation {
var _transform: Transform {
{
self.transform($0, id: $1)
}
}
}

extension Sequence where Element == Mutation {
func composed() -> Transform {
map { $0.transform }.composed()
return map { $0._transform }.composed()
}
}

extension Sequence where Element == Transform {
func composed() -> Transform {
reduce({ log, _ in AnyLoggable(log) }) { partialResult, transform in
let base: Transform = { log, _ in AnyLoggable(log) }
return reduce(base) { partialResult, transform in
{
transform(partialResult($0, $1), $1)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/ParchmentDefault/osLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by Kohei Kawaguchi on 2023/05/20.
//

import Foundation