Skip to content

Commit

Permalink
Merge branch 'hotfix/3.9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkhatskevich committed Jul 20, 2023
2 parents 7f40597 + 7198d43 commit bccd037
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 61 deletions.
28 changes: 14 additions & 14 deletions Sources/Core/Extensions/Publisher+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,21 @@ extension Publisher
_ body: @escaping (inout Output) throws -> Void
) -> AnyPublisher<Output, Failure> {

return self
.flatMap { input in
do
{
return try input
.+ body
./ Result<Output, Failure>.success
}
catch
{
return error
./ errorMapping
./ Result<Output, Failure>.failure
}
flatMap { input in

do
{
return try input
.+ body
./ Result<Output, Failure>.success
}
catch
{
return error
./ errorMapping
./ Result<Output, Failure>.failure
}
}
}

/// Inspect the upstream value and pass it downstream.
Expand Down
125 changes: 114 additions & 11 deletions Sources/Core/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ infix operator !! : NilCoalescingPrecedence // rethrow with FORCE error typecast

// MARK: - Implementation

/// Passes `input` value into `body` as is and returns whatever
/// `body` returns to continue the pipeline.
@Sendable
public
//infix
func ./ <T, U>(
input: T,
body: (T) async throws -> U
) async rethrows -> U {

try await Pipeline.take(input, mapAsync: body)
}

/// Passes `input` value into `body` as is and returns whatever
/// `body` returns to continue the pipeline.
public
Expand All @@ -60,7 +73,22 @@ func ./ <T, U>(
body: (T) throws -> U
) rethrows -> U {

return try Pipeline.take(input, map: body)
try Pipeline.take(input, map: body)
}

/// Passes unwrapped `input` value into `body` if it's non-nil,
/// or does nothing otherwise. Returns whatever `body` supposed
/// to return (or `nil`) as optional to continue the pipeline.
/// Analogue of `flatMap(...)` function of `Optional` type.
@Sendable
public
//infix
func .? <T, U>(
input: T?,
body: (T) async throws -> U?
) async rethrows -> U? {

try await Pipeline.take(optional: input, flatMapAsync: body)
}

/// Passes unwrapped `input` value into `body` if it's non-nil,
Expand All @@ -74,7 +102,27 @@ func .? <T, U>(
body: (T) throws -> U?
) rethrows -> U? {

return try Pipeline.take(optional: input, flatMap: body)
try Pipeline.take(optional: input, flatMap: body)
}

/**
Mutates `input` even if it's a `let` instance of value type with
a throwing closure, so the whole expression throws if the closure
throws.
NOTE: for reference type it will return same input instance with
given mutations, but for value type it will return a copy of
`input` instance with given mutations.
*/
@Sendable
public
//infix
func .+ <T>(
input: T,
_ body: (inout T) async throws -> Void
) async rethrows -> T {

try await Pipeline.mutate(input, body)
}

/**
Expand All @@ -96,6 +144,24 @@ func .+ <T>(
try Pipeline.mutate(input, body)
}

/**
Mutates `input` even if it's a `let` instance of value type.
NOTE: for reference type it will return same input instance with
given mutations, but for value type it will return a copy of
`input` instance with given mutations.
*/
@Sendable
public
//infix
func .- <T>(
input: T,
_ body: (T) async throws -> Void
) async rethrows -> T {

try await Pipeline.inspect(input, body)
}

/**
Mutates `input` even if it's a `let` instance of value type.
Expand All @@ -113,22 +179,40 @@ func .- <T>(
try Pipeline.inspect(input, body)
}

@Sendable
public
//infix
func .! <T>(
input: T,
condition: (T) async throws -> Bool
) async throws -> T {

try await Pipeline.ensure(input, condition)
}

public
//infix
func .! <T>(
input: T,
condition: (T) throws -> Bool
) throws -> T {

if
try condition(input)
{
return input
}
else
{
throw Pipeline.FailedConditionCheck()
}
try Pipeline.ensure(input, condition)
}

/// Passes `input` value into `body` as is. Returns nothing.
/// Typically defines final step in pipeline. Alternatively
/// can be used to "restart" pipeline — continue chain with
/// next step taking no input (Void).
@Sendable
public
//infix
func .* <T, U>(
input: T,
body: (T) async throws -> U
) async rethrows {

try await Pipeline.take(input, endAsync: body)
}

/// Passes `input` value into `body` as is. Returns nothing.
Expand All @@ -145,6 +229,22 @@ func .* <T, U>(
try Pipeline.take(input, end: body)
}

/// Passes unwrapped `input` value into `body` if it's non-nil,
/// or does nothing otherwise. Returns nothing anyway.
/// Typically defines final step in pipeline. Alternatively
/// can be used to "restart" pipeline — continue chain with
/// next step taking no input (Void).
@Sendable
public
//infix
func .?* <T, U>(
input: T?,
body: (T) async throws -> U
) async rethrows {

try await Pipeline.take(optional: input, endAsync: body)
}

/// Passes unwrapped `input` value into `body` if it's non-nil,
/// or does nothing otherwise. Returns nothing anyway.
/// Typically defines final step in pipeline. Alternatively
Expand All @@ -160,6 +260,7 @@ func .?* <T, U>(
try Pipeline.take(optional: input, end: body)
}

@Sendable
public
//infix
func ?! <T>(
Expand All @@ -170,6 +271,7 @@ func ?! <T>(
try Pipeline.unwrapOrThrow(input, getError)
}

@Sendable
public
//infix
func ?! (
Expand All @@ -180,6 +282,7 @@ func ?! (
try Pipeline.throwIfFalse(input, getError)
}

@Sendable
public
//infix
func ?! <T>(
Expand Down
Loading

0 comments on commit bccd037

Please sign in to comment.