Skip to content

Instantly share code, notes, and snippets.

@Rashidium
Created April 22, 2019 20:00
Show Gist options
  • Save Rashidium/aaa96db714bf3cd906b5e5f189daed19 to your computer and use it in GitHub Desktop.
Save Rashidium/aaa96db714bf3cd906b5e5f189daed19 to your computer and use it in GitHub Desktop.
import Foundation
protocol PanpaDecodable: Decodable & Keyable { }
protocol Keyable {
static var codingKey: String { get }
}
struct PanpaResponse<T>: Decodable where T: PanpaDecodable {
var status: Status {
if statusInt == 1 || statusInt == 0 {
return .success
}
return .failed
}
private(set) var statusInt: Int?
private var messageString: String?
private var messageAlternate: String?
var message: String {
if let messageString = messageString {
return messageString
} else {
return "error_generic".localized
}
}
private(set) var data: T?
enum CodingKeys: String, CodingKey {
case statusInt = "status"
case messageString = "message"
case messageAlternate = "Message"
case data
var stringValue: String {
switch self {
case .data: return T.codingKey
default: return rawValue
}
}
}
init() {
self.statusInt = -1
self.messageString = "error_generic".localized
self.data = nil
}
enum Status {
case success
case failed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment