diff --git a/Sources/Ananda/AnandaValueExtractor.swift b/Sources/Ananda/AnandaValueExtractor.swift index 4c2876a..ef3b5ef 100644 --- a/Sources/Ananda/AnandaValueExtractor.swift +++ b/Sources/Ananda/AnandaValueExtractor.swift @@ -11,12 +11,12 @@ public struct AnandaValueExtractor: Sendable { /// Standard shared instance public static let standard = Self() - let bool: @Sendable (AnandaJSON) -> Bool? - let int: @Sendable (AnandaJSON) -> Int? - let double: @Sendable (AnandaJSON) -> Double? - let string: @Sendable (AnandaJSON) -> String? - let date: @Sendable (AnandaJSON) -> Date? - let url: @Sendable (AnandaJSON) -> URL? + var bool: @Sendable (AnandaJSON) -> Bool? + var int: @Sendable (AnandaJSON) -> Int? + var double: @Sendable (AnandaJSON) -> Double? + var string: @Sendable (AnandaJSON) -> String? + var date: @Sendable (AnandaJSON) -> Date? + var url: @Sendable (AnandaJSON) -> URL? /// Initializer /// - Parameters: @@ -119,6 +119,50 @@ public struct AnandaValueExtractor: Sendable { } } +extension AnandaValueExtractor { + /// Returns updated `Self` with new `bool` extractor. + public func updatingBool(_ bool: @Sendable @escaping (AnandaJSON) -> Bool?) -> Self { + var result = self + result.bool = bool + return result + } + + /// Returns updated `Self` with new `int` extractor. + public func updatingInt(_ int: @Sendable @escaping (AnandaJSON) -> Int?) -> Self { + var result = self + result.int = int + return result + } + + /// Returns updated `Self` with new `double` extractor. + public func updatingDouble(_ double: @Sendable @escaping (AnandaJSON) -> Double?) -> Self { + var result = self + result.double = double + return result + } + + /// Returns updated `Self` with new `string` extractor. + public func updatingString(_ string: @Sendable @escaping (AnandaJSON) -> String?) -> Self { + var result = self + result.string = string + return result + } + + /// Returns updated `Self` with new `date` extractor. + public func updatingDate(_ date: @Sendable @escaping (AnandaJSON) -> Date?) -> Self { + var result = self + result.date = date + return result + } + + /// Returns updated `Self` with new `url` extractor. + public func updatingURL(_ url: @Sendable @escaping (AnandaJSON) -> URL?) -> Self { + var result = self + result.url = url + return result + } +} + #if os(Linux) #if $RetroactiveAttribute extension ISO8601DateFormatter: @retroactive @unchecked Sendable { diff --git a/Tests/AnandaTests/AnandaTests.swift b/Tests/AnandaTests/AnandaTests.swift index 9846b91..2c855c4 100644 --- a/Tests/AnandaTests/AnandaTests.swift +++ b/Tests/AnandaTests/AnandaTests.swift @@ -387,29 +387,25 @@ final class AnandaTests { } struct Toot: AnandaModel { - static var valueExtractor: AnandaValueExtractor { - .init( - bool: { - if let bool = $0.originalBool { - return bool - } else { - if let int = $0.originalInt { - return int != 0 - } - - if let string = $0.originalString { - switch string.lowercased() { - case "true": - return true - default: - break - } - } - - return nil + static let valueExtractor: AnandaValueExtractor = .standard.updatingBool { + if let bool = $0.originalBool { + return bool + } else { + if let int = $0.originalInt { + return int != 0 + } + + if let string = $0.originalString { + switch string.lowercased() { + case "true": + return true + default: + break } } - ) + + return nil + } } let id: Int