Skip to content

Commit

Permalink
Add conformance for Combine’s TopLevelDecoder & TopLevelEncoder when …
Browse files Browse the repository at this point in the history
…it’s available
  • Loading branch information
kdubb committed May 16, 2021
1 parent 0a012e4 commit e3a1600
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func decode<T: Decodable>(_ type: T.Type, from data: String) throws -> T
func decodeIfPresent<T: Decodable>(_ type: T.Type, from data: String) throws -> T?
```

##### Combine

When used on Apple platforms where the Combine framework is available, all the encoders conform to Combine's
`TopLevelEncoder` and all decoders conform to its `TopLevelDecoder`.

### Polymorphic Encoding/Decoding

`Codable` encoders and decoders are a great tool and very convenient. Unfortunately using them with polymorphic types is cumbersome,
Expand Down
11 changes: 11 additions & 0 deletions Sources/PotentASN1/ASN1Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,14 @@ extension SchemaState {
}

}


#if canImport(Combine)

import Combine

extension ASN1Decoder : TopLevelDecoder {
public typealias Input = Data
}

#endif
11 changes: 11 additions & 0 deletions Sources/PotentASN1/ASN1Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,14 @@ extension SchemaState {
}

}


#if canImport(Combine)

import Combine

extension ASN1Encoder : TopLevelEncoder {
public typealias Output = Data
}

#endif
11 changes: 11 additions & 0 deletions Sources/PotentCBOR/CBORDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,14 @@ private let _iso8601Formatter: DateFormatter = {
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return formatter
}()


#if canImport(Combine)

import Combine

extension CBORDecoder : TopLevelDecoder {
public typealias Input = Data
}

#endif
11 changes: 11 additions & 0 deletions Sources/PotentCBOR/CBOREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ private let _iso8601Formatter: DateFormatter = {
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return formatter
}()


#if canImport(Combine)

import Combine

extension CBOREncoder : TopLevelEncoder {
public typealias Output = Data
}

#endif
15 changes: 15 additions & 0 deletions Sources/PotentCodables/AnyValue/AnyValueDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,18 @@ public struct AnyValueDecoderTransform: InternalDecoderTransform {
}

}


#if canImport(Combine)

import Combine

extension AnyValueDecoder : TopLevelDecoder {
public typealias Input = AnyValue

public func decode<T>(_ type: T.Type, from tree: AnyValue) throws -> T where T : Decodable {
return try decodeTree(type, from: tree)
}
}

#endif
15 changes: 15 additions & 0 deletions Sources/PotentCodables/AnyValue/AnyValueEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,18 @@ public struct AnyValueEncoderTransform: InternalEncoderTransform {
}

}


#if canImport(Combine)

import Combine

extension AnyValueEncoder : TopLevelEncoder {
public typealias Output = AnyValue

public func encode<T>(_ value: T) throws -> AnyValue where T : Encodable {
return try encodeTree(value)
}
}

#endif
11 changes: 11 additions & 0 deletions Sources/PotentJSON/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,14 @@ private let _iso8601Formatter: DateFormatter = {
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return formatter
}()


#if canImport(Combine)

import Combine

extension JSONDecoder : TopLevelDecoder {
public typealias Input = Data
}

#endif
11 changes: 11 additions & 0 deletions Sources/PotentJSON/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,14 @@ private let _iso8601Formatter: DateFormatter = {
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return formatter
}()


#if canImport(Combine)

import Combine

extension JSONEncoder : TopLevelEncoder {
public typealias Output = Data
}

#endif

0 comments on commit e3a1600

Please sign in to comment.