OSDN Git Service

swiftlint 'line_length'の警告を修正
[kcd/KCD.git] / KCD / BasicMapper.swift
1 //
2 //  BasicMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 fileprivate enum BasicAPI: String {
12     case getMemberBasic = "/kcsapi/api_get_member/basic"
13     case port = "/kcsapi/api_port/port"
14 }
15
16 fileprivate func dataKey(_ apiResponse: APIResponse) -> String {
17     guard let basicApi = BasicAPI(rawValue: apiResponse.api)
18         else { return "api_data" }
19     switch basicApi {
20     case .port: return "api_data.api_basic"
21     default: return "api_data"
22     }
23 }
24
25 class BasicMapper: JSONMapper {
26     typealias ObjectType = Basic
27     
28     let apiResponse: APIResponse
29     let configuration: MappingConfiguration<Basic>
30     
31     required init(_ apiResponse: APIResponse) {
32         self.apiResponse = apiResponse
33         self.configuration = MappingConfiguration(entity: Basic.entity,
34                                                   dataKey: dataKey(apiResponse),
35                                                   editorStore: ServerDataStore.oneTimeEditor())
36     }
37     
38     func commit() {
39         let j = apiResponse.json as NSDictionary
40         guard let data = j.value(forKeyPath: configuration.dataKey) as? [String: Any]
41             else { return print("json is wrong") }
42         
43         let store = ServerDataStore.oneTimeEditor()
44         guard let basic = store.basic() ?? store.createBasic()
45             else { return print("Can not Get Basic") }
46         registerElement(data, to: basic)
47     }
48 }