From 691b5de1a4746920c9dabd7aec0acd0f5defa08c Mon Sep 17 00:00:00 2001 From: Leif Date: Wed, 18 Sep 2024 23:16:26 -0600 Subject: [PATCH] Add Loggable protocol for state and dependency --- Sources/AppState/Application/Application+internal.swift | 6 +++++- .../Types/Dependency/Application+Dependency.swift | 4 ++-- Sources/AppState/Application/Types/Helper/Loggable.swift | 4 ++++ .../Application/Types/State/Application+State.swift | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 Sources/AppState/Application/Types/Helper/Loggable.swift diff --git a/Sources/AppState/Application/Application+internal.swift b/Sources/AppState/Application/Application+internal.swift index 9428afc..302cde4 100644 --- a/Sources/AppState/Application/Application+internal.swift +++ b/Sources/AppState/Application/Application+internal.swift @@ -3,7 +3,11 @@ extension Application { static var cacheDescription: String { shared.cache.allValues .map { key, value in - "\t- \(value)" + if let value = value as? Loggable { + "\t- \(value.logValue)" + } else { + "\t- \(value)" + } } .sorted(by: <) .joined(separator: "\n") diff --git a/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift b/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift index ced09ba..d7d7fee 100644 --- a/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift +++ b/Sources/AppState/Application/Types/Dependency/Application+Dependency.swift @@ -4,7 +4,7 @@ extension Application { } /// `Dependency` struct encapsulates dependencies used throughout the app. - public struct Dependency: CustomStringConvertible, Sendable { + public struct Dependency: Sendable, Loggable { /// The dependency value. var value: Value @@ -28,7 +28,7 @@ extension Application { self.scope = scope } - public var description: String { + public var logValue: String { "Dependency<\(Value.self)>(\(value)) (\(scope.key))" } } diff --git a/Sources/AppState/Application/Types/Helper/Loggable.swift b/Sources/AppState/Application/Types/Helper/Loggable.swift new file mode 100644 index 0000000..d9ac88b --- /dev/null +++ b/Sources/AppState/Application/Types/Helper/Loggable.swift @@ -0,0 +1,4 @@ +protocol Loggable { + @MainActor + var logValue: String { get } +} diff --git a/Sources/AppState/Application/Types/State/Application+State.swift b/Sources/AppState/Application/Types/State/Application+State.swift index bc17b6a..102c990 100644 --- a/Sources/AppState/Application/Types/State/Application+State.swift +++ b/Sources/AppState/Application/Types/State/Application+State.swift @@ -2,7 +2,7 @@ import Foundation extension Application { /// `State` encapsulates the value within the application's scope and allows any changes to be propagated throughout the scoped area. - public struct State: MutableApplicationState, Sendable { + public struct State: Sendable, MutableApplicationState, Loggable { /// Values that are available in the cache. enum StateType { case state @@ -98,7 +98,7 @@ extension Application { } @MainActor - public var description: String { + public var logValue: String { switch type { case .state: return "State<\(Value.self)>(\(value)) (\(scope.key))" case .stored: return "StoredState<\(Value.self)>(\(value)) (\(scope.key))"