From 7735fb7abcc67d2990ae883c2544a4152ca4170b Mon Sep 17 00:00:00 2001 From: givip Date: Mon, 11 Mar 2019 19:33:21 +0400 Subject: [PATCH] #28 Fix error for sending InlineKeyboardMarkup and MaskPosition in a multipart message --- .../Telegrammer/Bot/Models/MaskPosition.swift | 2 +- .../MultipartPartConvertible+Helper.swift | 28 +++++++++++++++++++ Sources/Telegrammer/Types/ReplyMarkup.swift | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 Sources/Telegrammer/Helpers/MultipartPartConvertible+Helper.swift 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)