2 // MasterShipMapper.swift
5 // Created by Hori,Masaki on 2017/02/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class MasterShipMapper: JSONMapper {
14 let apiResponse: APIResponse
15 let configuration = MappingConfiguration(entity: MasterShip.entity,
16 dataKeys: ["api_data", "api_mst_ship"],
17 editorStore: ServerDataStore.oneTimeEditor())
19 required init(_ apiResponse: APIResponse) {
21 self.apiResponse = apiResponse
24 private lazy var masterSTypes: [MasterSType] = {
26 guard let store = configuration.editorStore as? ServerDataStore else {
31 return store.sortedMasterSTypesById()
34 func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool {
36 if key != "api_stype" {
41 guard let sType = value.int else {
43 Logger.shared.log("MasterShipMapper: value is not Int")
48 setStype(sType, to: masterShip)
53 private func setStype(_ stypeID: Int, to masterShip: MasterShip) {
55 if masterShip.stype.id == stypeID { return }
57 guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID }) else {
59 Logger.shared.log("MasterShipMapper: Can not find MasterSType")
64 // FUCK: 型推論がバカなのでダウンキャストしてるんだ!!!
65 guard let masterSType = configuration.editorStore.exchange(stype) as? MasterSType else {
67 Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType")
72 masterShip.stype = masterSType