-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVariable.swift
257 lines (230 loc) · 8.91 KB
/
Variable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// Copyright 2020 PLAID, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import KarteCore
import KarteUtilities
/// 設定値とそれに付随する情報を保持するためのクラスです。<br>
/// 設定値の他に、接客サービスIDやアクションIDを保持しています。
@objc(KRTVariable)
public class Variable: NSObject, Codable {
enum CodingKeys: String, CodingKey {
case campaignId
case shortenId
case name
case value
case timestamp
case eventHash
}
/// キャンペーンIDを返します。<br>
/// 設定値が未定義の場合は `nil` を返します。
@objc public var campaignId: String?
/// アクションIDを返します。<br>
/// 設定値が未定義の場合は `nil` を返します。
@objc public var shortenId: String?
/// 設定値名を返します。
@objc public var name: String
/// 設定値を返します。<br>
/// 設定値が未定義の場合は `nil` を返します。
var value: String?
var timestamp: String?
var eventHash: String?
/// 設定値が定義済みであるかどうか返します。<br>
/// 定義済みの場合は `true` を、未定義の場合は `false` を返します。
@objc public var isDefined: Bool {
value != nil
}
/// 設定値(文字列)を返します。<br>
/// 設定値が未定義の場合は `nil` を返します。
@objc public var string: String? {
value
}
/// 設定値(配列)を返します。<br>
/// 以下の場合において nil を返します。
/// - 設定値が未定義の場合
/// - 設定値(JSON文字列)のパースができない場合
@objc public var array: [Any]? {
// swiftlint:disable:previous discouraged_optional_collection
guard let data = string?.data(using: .utf8) else {
return nil
}
do {
let object = try JSONSerialization.jsonObject(with: data, options: [])
return object as? [Any]
} catch {
Logger.error(tag: .variables, message: "Failed to parse JSON: \(error)")
return nil
}
}
/// 設定値(辞書)を返します。<br>
/// 以下の場合において nil を返します。
/// - 設定値が未定義の場合
/// - 設定値(JSON文字列)のパースができない場合
@objc public var dictionary: [String: Any]? {
// swiftlint:disable:previous discouraged_optional_collection
guard let data = string?.data(using: .utf8) else {
return nil
}
do {
let object = try JSONSerialization.jsonObject(with: data, options: [])
return object as? [String: Any]
} catch {
Logger.error(tag: .variables, message: "Failed to parse JSON: \(error)")
return nil
}
}
/// 設定値インスタンスを初期化します。
/// - Parameter name: 設定値名
@objc
public init(name: String) {
self.name = name
let key = UserDefaultsKey(name, forNamespace: .variables)
if let data = UserDefaults.standard.object(forKey: key) as? Data {
do {
let variable = try JSONDecoder().decode(Variable.self, from: data)
self.campaignId = variable.campaignId
self.shortenId = variable.shortenId
self.value = variable.value
self.timestamp = variable.timestamp
self.eventHash = variable.eventHash
} catch {
Logger.verbose(tag: .variables, message: "Failed to decode JSON. \(error)")
}
} else {
Logger.warn(tag: .variables, message: "Variable is not found. name=\(name)")
}
super.init()
}
init(name: String, campaignId: String, shortenId: String, value: String?, timestamp: String?, eventHash: String?) {
self.name = name
self.campaignId = campaignId
self.shortenId = shortenId
self.value = value
self.timestamp = timestamp
self.eventHash = eventHash
super.init()
}
/// 設定値(文字列)を返します。<br>
/// なお設定値が未定義の場合は、デフォルト値を返します。
///
/// - Parameter value: デフォルト値
/// - Returns: 設定値(文字列)
@objc(stringWithDefaultValue:)
public func string(default value: String) -> String {
if let string = self.string {
return string
} else {
return value
}
}
/// 設定値(整数)を返します。<br>
/// なお設定値が数値でない場合は、デフォルト値を返します。
///
/// - Parameter value: デフォルト値
/// - Returns: 設定値(整数)
@objc(integerWithDefaultValue:)
public func integer(default value: Int) -> Int {
guard let string = string else {
return value
}
if isNumeric(string) {
return (string as NSString).integerValue
} else {
return value
}
}
/// 設定値(浮動小数点数)を返します。<br>
/// なお設定値が数値でない場合は、デフォルト値を返します。
///
/// - Parameter value: デフォルト値
/// - Returns: 設定値(浮動小数点数)
@objc(doubleWithDefaultValue:)
public func double(default value: Double) -> Double {
guard let string = string else {
return value
}
if isNumeric(string) {
return (string as NSString).doubleValue
} else {
return value
}
}
/// 設定値(ブール値)を返します。<br>
/// なおブール値への変換ルールについては [こちら](https://developer.apple.com/documentation/foundation/nsstring/1409420-boolvalue) を参照してください。
///
/// 設定値が未定義の場合は、デフォルト値を返します。
/// - Parameter value: デフォルト値
/// - Returns: 設定値(ブール値)
@objc(boolWithDefaultValue:)
public func bool(default value: Bool) -> Bool {
guard let string = string else {
return value
}
return (string as NSString).boolValue
}
/// 設定値(配列)を返します。<br>
/// 以下の場合においてデフォルト値を返します。
/// - 設定値が未定義の場合
/// - 設定値(JSON文字列)のパースができない場合
///
/// - Parameter value: デフォルト値
/// - Returns: 設定値(配列)
@objc(arrayWithDefaultValue:)
public func array(default value: [Any]) -> [Any] {
array ?? value
}
/// 設定値(辞書)を返します。<br>
/// 以下の場合においてデフォルト値を返します。
/// - 設定値が未定義の場合
/// - 設定値(JSON文字列)のパースができない場合
///
/// - Parameter value: デフォルト値
/// - Returns: 設定値(辞書)
@objc(dictionaryWithDefaultValue:)
public func dictionary(default value: [String: Any]) -> [String: Any] {
dictionary ?? value
}
func save() {
guard let campaignId = campaignId, let shortenId = shortenId else {
Logger.warn(tag: .variables, message: "Failed to save because value is nil.")
return
}
Logger.debug(tag: .variables, message: "Write variable: \(name). campaignId=\(campaignId), shortenId=\(shortenId)")
do {
let data = try JSONEncoder().encode(self)
let key = UserDefaultsKey(name, forNamespace: .variables)
UserDefaults.standard.set(data, forKey: key)
} catch {
Logger.verbose(tag: .variables, message: "Failed to encode JSON. \(error)")
}
}
func clear() {
let key = UserDefaultsKey(name, forNamespace: .variables)
UserDefaults.standard.removeObject(forKey: key)
}
private func isNumeric(_ value: String) -> Bool {
let intScanner = Scanner(string: value)
if intScanner.scanInt(nil) && intScanner.isAtEnd {
return true
}
let doubleScanner = Scanner(string: value)
if doubleScanner.scanDouble(nil) && doubleScanner.isAtEnd {
return true
}
return false
}
deinit {
}
}