Skip to content

Commit

Permalink
added encodeIfPresent
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jul 7, 2023
1 parent b9e1464 commit ab1e135
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/ConfigurationCodable/Encodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,29 @@ public extension JSONEncoder {

public extension UnkeyedEncodingContainer {
@inlinable
mutating func encode<T>(
_ t: T, configuration: T.EncodingConfiguration
) throws where T: EncodableWithConfiguration {
try encode(EncodableWrapper(t, configuration: configuration))
mutating func encode<T: EncodableWithConfiguration>(
_ value: T, configuration: T.EncodingConfiguration
) throws {
try encode(EncodableWrapper(value, configuration: configuration))
}
}

public extension KeyedEncodingContainer {
@inlinable
mutating func encode<T>(
_ t: T, forKey key: Key, configuration: T.EncodingConfiguration
) throws where T: EncodableWithConfiguration {
try encode(EncodableWrapper(t, configuration: configuration),
mutating func encode<T: EncodableWithConfiguration>(
_ value: T, forKey key: Key, configuration: T.EncodingConfiguration
) throws {
try encode(EncodableWrapper(value, configuration: configuration),
forKey: key)
}

@inlinable
mutating func encodeIfPresent<T: EncodableWithConfiguration>(
_ value: T?, forKey key: Key, configuration: T.EncodingConfiguration
) throws {
try encodeIfPresent(
value.map { EncodableWrapper($0, configuration: configuration) },
forKey: key
)
}
}

0 comments on commit ab1e135

Please sign in to comment.