Skip to content

Instantly share code, notes, and snippets.

View s4cha's full-sized avatar
🎯
Focusing

Sacha DSO s4cha

🎯
Focusing
View GitHub Profile
func restURL<T:RestResource>(r:T) -> String {
return "/\(T.restName())/\(r.restId())"
}
protocol RestResource {
static func restName() -> String
func restId() -> String
}
struct Customer {
var identifier:Int = 0
}
struct Product {
var identifier:Int = 0
}
struct Order {
var identifier:Int = 0
protocol Identifiable {
var identifier:Int {get}
}
struct Customer:Identifiable {
var identifier:Int = 0
}
struct Product:Identifiable {
var identifier:Int = 0
extension RestResource where Self:Identifiable {
func restId() -> String { return "\(identifier)" }
}
extension Customer:RestResource { static func restName() -> String { return "customers" } }
extension Product:RestResource { static func restName() -> String { return "products" } }
extension Order:RestResource { static func restName() -> String { return "orders" } }
let customer = Customer(identifier: 1234)
restURL(customer) // -> "/customers/1234"
@s4cha
s4cha / rest1.swift
Last active October 25, 2015 14:41
let customer = Customer(identifier: 1234)
// We want to generate "/customers/1234"
extension Customer:RestResource {
static func restName() -> String {
return "customers"
}
func restId() -> String {
return "\(identifier)"
}
}
typealias JSON = AnyObject
protocol JSONParsable {
init(json: JSON)
}