diff --git a/Sources/Telegrammer/Bot/Models/MaskPosition.swift b/Sources/Telegrammer/Bot/Models/MaskPosition.swift index 69d2eaa..ed940c8 100644 --- a/Sources/Telegrammer/Bot/Models/MaskPosition.swift +++ b/Sources/Telegrammer/Bot/Models/MaskPosition.swift @@ -7,7 +7,7 @@ SeeAlso Telegram Bot API Reference: [MaskPosition](https://core.telegram.org/bots/api#maskposition) */ -public final class MaskPosition: Codable { +public final class MaskPosition: Codable, MultipartPartNestedConvertible { /// Custom keys for coding/decoding `MaskPosition` struct enum CodingKeys: String, CodingKey { diff --git a/Sources/Telegrammer/Helpers/MultipartPartConvertible+Helper.swift b/Sources/Telegrammer/Helpers/MultipartPartConvertible+Helper.swift new file mode 100644 index 0000000..b303a9a --- /dev/null +++ b/Sources/Telegrammer/Helpers/MultipartPartConvertible+Helper.swift @@ -0,0 +1,28 @@ +// +// MultipartPartConvertible+Helper.swift +// Telegrammer +// +// Created by Givi on 11/03/2019. +// + +import Foundation +import Multipart + +public protocol MultipartPartNestedConvertible: MultipartPartConvertible {} + +public extension MultipartPartNestedConvertible where Self: Codable { + func convertToMultipartPart() throws -> MultipartPart { + return try MultipartPart(data: JSONEncoder().encode(self)) + } + + static func convertFromMultipartPart(_ part: MultipartPart) throws -> Self { + do { + return try JSONDecoder().decode(self.self, from: part.data) + } catch { + throw MultipartError( + identifier: "\(self.self)", + reason: "Failed to setup instance from json decoder - \(error)" + ) + } + } +} diff --git a/Sources/Telegrammer/Types/ReplyMarkup.swift b/Sources/Telegrammer/Types/ReplyMarkup.swift index 2ae3745..5bd1fe6 100644 --- a/Sources/Telegrammer/Types/ReplyMarkup.swift +++ b/Sources/Telegrammer/Types/ReplyMarkup.swift @@ -10,7 +10,7 @@ SeeAlso Telegram Bot API Reference: [Reply Markups](https://core.telegram.org/bots/2-0-intro#new-inline-keyboards) */ -public enum ReplyMarkup: Codable { +public enum ReplyMarkup: Codable, MultipartPartNestedConvertible { case inlineKeyboardMarkup(InlineKeyboardMarkup) case replyKeyboardMarkup(ReplyKeyboardMarkup) case replyKeyboardRemove(ReplyKeyboardRemove)