Skip to content

Commit

Permalink
feat: Add BodyEncoder and BodyDecoder (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Lees11 authored and ianpartridge committed Aug 24, 2018
1 parent a8212e7 commit 8e41f3f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Sources/KituraContracts/BodyDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import Foundation

/**
A class that conforms to `BodyDecoder` must be able to decode from `Data` into a `Codable` type.
This class can then be used to produce input objects for a Codable route.
*/
public protocol BodyDecoder: AnyObject {
func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T
}

extension JSONDecoder: BodyDecoder {}
27 changes: 27 additions & 0 deletions Sources/KituraContracts/BodyEncoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import Foundation

/**
A class that conforms to `BodyEncoder` must be able to encode a `Codable` type into `Data`.
This class can then be used to produce output objects for a Codable route.
*/
public protocol BodyEncoder: AnyObject {
func encode<T : Encodable>(_ value: T) throws -> Data
}
extension JSONEncoder: BodyEncoder {}

2 changes: 1 addition & 1 deletion Sources/KituraContracts/CodableQuery/QueryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import LoggerAPI
- Non-optional `Bool` decodes to `false`
- All other non-optional types throw a decoding error
*/
public class QueryDecoder: Coder, Decoder {
public class QueryDecoder: Coder, Decoder, BodyDecoder {

/**
The coding key path.
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraContracts/CodableQuery/QueryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension CharacterSet {
}
````
*/
public class QueryEncoder: Coder, Encoder {
public class QueryEncoder: Coder, Encoder, BodyEncoder {

/**
A `[String: String]` dictionary.
Expand Down

0 comments on commit 8e41f3f

Please sign in to comment.