Skip to content

Commit

Permalink
Refactor to use single funtion to parse @params value
Browse files Browse the repository at this point in the history
  • Loading branch information
kientux committed Jul 10, 2022
1 parent c9eb1be commit 1ed5cf4
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions Sources/Parameterize/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,25 @@ extension Params: ParameterPair {
}

var value: Any? {
if let mapper = mapper {
return process(mappedValue: mapper(wrappedValue))
} else {
return processWrappedValue()
}
}

/// Two functions below have completely the same body
/// but it required to split to 2 functions
/// because with `wrappedValue`, we need to directly access
/// instead of go through `Any?` parameter,
/// or else it will be wrapped into an `Optional<Any>` type
/// and produces incorrect output.

private func processWrappedValue() -> Any? {
var v: Any?

if let value = wrappedValue as? OptionalProtocol {
v = value.wrapped
} else {
v = wrappedValue
}
// go through this value to eliminate optional
let underlyingValue: Any

if let value = v as? ParamConvertible {
v = value.parameterValue
if let mapper = mapper {
underlyingValue = mapper(wrappedValue) as Any
} else {
underlyingValue = wrappedValue
}

return v
}

private func process(mappedValue: Any?) -> Any? {
var v: Any?

if let value = mappedValue as? OptionalProtocol {
if let value = underlyingValue as? OptionalProtocol {
v = value.wrapped
} else {
v = mappedValue
v = underlyingValue
}

if let value = v as? ParamConvertible {
v = value.parameterValue
v = value.parameterValue as Any
}

return v
Expand Down

0 comments on commit 1ed5cf4

Please sign in to comment.