Skip to content

Commit

Permalink
updating value extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
nixzhu committed Dec 25, 2024
1 parent de56a9f commit db8647e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
56 changes: 50 additions & 6 deletions Sources/Ananda/AnandaValueExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 17 additions & 21 deletions Tests/AnandaTests/AnandaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db8647e

Please sign in to comment.