Skip to content

Instantly share code, notes, and snippets.

@hisui
Created June 21, 2017 05:17
Show Gist options
  • Save hisui/6954c16aab6a2c7d4d05b26ecf1fb735 to your computer and use it in GitHub Desktop.
Save hisui/6954c16aab6a2c7d4d05b26ecf1fb735 to your computer and use it in GitHub Desktop.
import ObjectMapper
enum Event {
case ringing
case incomingSound(instant: Int64, samples: Data)
case outgoingSound(instant: Int64, samples: Data)
case hangUp
}
extension Event : ImmutableMappable {
init(map: Map) throws {
switch try map.value("kind") as String {
case "ringing":
self = .ringing
case "hangUp":
self = .hangUp
case "incomingSound":
self = .incomingSound(
instant: try map.value("instant"),
samples: try map.value("samples")
)
case "outgoingSound":
self = .outgoingSound(
instant: try map.value("instant"),
samples: try map.value("samples")
)
default:
throw NSError()
}
}
mutating func mapping(map: Map) {
switch self {
case .ringing:
"ringing" >>> map["kind"]
case .hangUp:
"hangUp" >>> map["kind"]
case let .incomingSound(instant: instant, samples: samples):
"incomingSound" >>> map["kind"]
instant >>> map["instant"]
samples >>> map["samples"]
case let .outgoingSound(instant: instant, samples: samples):
"outgoingSound" >>> map["kind"]
instant >>> map["instant"]
samples >>> map["samples"]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment