@@ -14,12 +14,11 @@ public typealias JSONKeyConfig = (Property) -> JSONPropertyKey
1414public typealias JSONValueConfig = ( Any ? , Property ) -> Any ?
1515
1616/// Config for convertible
17+ ///
18+ /// Suggest:
19+ /// 1. Set every config once for every type
20+ /// 2. Set config after app finish lauching
1721public class ConvertibleConfig {
18- private static var lock = pthread_rwlock_t ( )
19- private static let initLock = {
20- pthread_rwlock_init ( & lock, nil )
21- } ( )
22-
2322 private class Item {
2423 var modelKey : ModelKeyConfig ?
2524 var modelValue : ModelValueConfig ?
@@ -32,15 +31,11 @@ public class ConvertibleConfig {
3231 init ( jsonValue: @escaping JSONValueConfig ) { self . jsonValue = jsonValue }
3332 }
3433
35- private static let global : Item = Item ( )
34+ private static let global = Item ( )
3635 private static var items : [ TypeKey : Item ] = [ : ]
3736
3837 /// get global config for modelKey
3938 public static func modelKey( from property: Property ) -> ModelPropertyKey {
40- let _ = initLock
41- pthread_rwlock_rdlock ( & lock)
42- defer { pthread_rwlock_unlock ( & lock) }
43-
4439 guard let fn = global. modelKey else { return property. name }
4540 return fn ( property)
4641 }
@@ -54,10 +49,6 @@ public class ConvertibleConfig {
5449 /// get type's config for modelKey
5550 public static func modelKey( from property: Property ,
5651 _ type: Convertible . Type ) -> ModelPropertyKey {
57- let _ = initLock
58- pthread_rwlock_rdlock ( & lock)
59- defer { pthread_rwlock_unlock ( & lock) }
60-
6152 if let fn = items [ typeKey ( type) ] ? . modelKey {
6253 return fn ( property)
6354 }
@@ -72,17 +63,15 @@ public class ConvertibleConfig {
7263 }
7364 }
7465
75- guard let fn = global. modelKey else { return property. name }
66+ guard let fn = global. modelKey else {
67+ return property. name
68+ }
7669 return fn ( property)
7770 }
7871
7972 /// get global config for modelValue
8073 public static func modelValue( from jsonValue: Any ? ,
8174 _ property: Property ) -> Any ? {
82- let _ = initLock
83- pthread_rwlock_rdlock ( & lock)
84- defer { pthread_rwlock_unlock ( & lock) }
85-
8675 guard let fn = global. modelValue else { return jsonValue }
8776 return fn ( jsonValue, property)
8877 }
@@ -98,34 +87,32 @@ public class ConvertibleConfig {
9887 public static func modelValue( from jsonValue: Any ? ,
9988 _ property: Property ,
10089 _ type: Convertible . Type ) -> Any ? {
101- let _ = initLock
102- pthread_rwlock_rdlock ( & lock)
103- defer { pthread_rwlock_unlock ( & lock) }
104-
105- if let fn = items [ typeKey ( type) ] ? . modelValue {
90+ let key = typeKey ( type)
91+ if let fn = items [ key] ? . modelValue {
10692 return fn ( jsonValue, property)
10793 }
10894
10995 let mt = Metadata . type ( type)
11096 if var classMt = mt as? ClassType {
11197 while let superMt = classMt. super {
11298 if let fn = items [ typeKey ( superMt. type) ] ? . modelValue {
99+ items [ key] ? . modelValue = fn
113100 return fn ( jsonValue, property)
114101 }
115102 classMt = superMt
116103 }
117104 }
118105
119- guard let fn = global. modelValue else { return jsonValue }
106+ guard let fn = global. modelValue else {
107+ items [ key] ? . modelValue = { v, _ in v }
108+ return jsonValue
109+ }
110+ items [ key] ? . modelValue = fn
120111 return fn ( jsonValue, property)
121112 }
122113
123114 /// get global config for JSONKey
124115 public static func JSONKey( from property: Property ) -> JSONPropertyKey {
125- let _ = initLock
126- pthread_rwlock_rdlock ( & lock)
127- defer { pthread_rwlock_unlock ( & lock) }
128-
129116 guard let fn = global. jsonKey else { return property. name }
130117 return fn ( property)
131118 }
@@ -139,10 +126,6 @@ public class ConvertibleConfig {
139126 /// get type's config for JSONKey
140127 public static func JSONKey( from property: Property ,
141128 _ type: Convertible . Type ) -> JSONPropertyKey {
142- let _ = initLock
143- pthread_rwlock_rdlock ( & lock)
144- defer { pthread_rwlock_unlock ( & lock) }
145-
146129 if let fn = items [ typeKey ( type) ] ? . jsonKey {
147130 return fn ( property)
148131 }
@@ -157,17 +140,15 @@ public class ConvertibleConfig {
157140 }
158141 }
159142
160- guard let fn = global. jsonKey else { return property. name }
143+ guard let fn = global. jsonKey else {
144+ return property. name
145+ }
161146 return fn ( property)
162147 }
163148
164149 /// get global config for JSONValue
165150 public static func JSONValue( from modelValue: Any ? ,
166151 _ property: Property ) -> Any ? {
167- let _ = initLock
168- pthread_rwlock_rdlock ( & lock)
169- defer { pthread_rwlock_unlock ( & lock) }
170-
171152 guard let fn = global. modelValue else { return modelValue }
172153 return fn ( modelValue, property)
173154 }
@@ -183,25 +164,27 @@ public class ConvertibleConfig {
183164 public static func JSONValue( from modelValue: Any ? ,
184165 _ property: Property ,
185166 _ type: Convertible . Type ) -> Any ? {
186- let _ = initLock
187- pthread_rwlock_rdlock ( & lock)
188- defer { pthread_rwlock_unlock ( & lock) }
189-
190- if let fn = items [ typeKey ( type) ] ? . jsonValue {
167+ let key = typeKey ( type)
168+ if let fn = items [ key] ? . jsonValue {
191169 return fn ( modelValue, property)
192170 }
193171
194172 let mt = Metadata . type ( type)
195173 if var classMt = mt as? ClassType {
196174 while let superMt = classMt. super {
197175 if let fn = items [ typeKey ( superMt. type) ] ? . jsonValue {
176+ items [ key] ? . jsonValue = fn
198177 return fn ( modelValue, property)
199178 }
200179 classMt = superMt
201180 }
202181 }
203182
204- guard let fn = global. modelValue else { return modelValue }
183+ guard let fn = global. modelValue else {
184+ items [ key] ? . jsonValue = { v, _ in v}
185+ return modelValue
186+ }
187+ items [ key] ? . jsonValue = fn
205188 return fn ( modelValue, property)
206189 }
207190
@@ -214,13 +197,6 @@ public class ConvertibleConfig {
214197 /// set types's config for modelKey
215198 public static func setModelKey( for types: [ Convertible . Type ] = [ ] ,
216199 _ modelKey: @escaping ModelKeyConfig ) {
217- let _ = initLock
218- pthread_rwlock_wrlock ( & lock)
219- defer { pthread_rwlock_unlock ( & lock) }
220-
221- // clear model key cache
222- Metadata . modelTypes. forEach { $0. clearModelKeys ( ) }
223-
224200 if types. count == 0 {
225201 global. modelKey = modelKey
226202 return
@@ -245,10 +221,6 @@ public class ConvertibleConfig {
245221 /// set types's config for modelValue
246222 public static func setModelValue( for types: [ Convertible . Type ] = [ ] ,
247223 modelValue: @escaping ModelValueConfig ) {
248- let _ = initLock
249- pthread_rwlock_wrlock ( & lock)
250- defer { pthread_rwlock_unlock ( & lock) }
251-
252224 if types. count == 0 {
253225 global. modelValue = modelValue
254226 return
@@ -273,13 +245,6 @@ public class ConvertibleConfig {
273245 /// set types's config for jsonKey
274246 public static func setJSONKey( for types: [ Convertible . Type ] = [ ] ,
275247 jsonKey: @escaping JSONKeyConfig ) {
276- let _ = initLock
277- pthread_rwlock_wrlock ( & lock)
278- defer { pthread_rwlock_unlock ( & lock) }
279-
280- // clear JSON key cache
281- Metadata . modelTypes. forEach { $0. clearJSONKeys ( ) }
282-
283248 if types. count == 0 {
284249 global. jsonKey = jsonKey
285250 return
@@ -304,10 +269,6 @@ public class ConvertibleConfig {
304269 /// set types's config for jsonValue
305270 public static func setJSONValue( for types: [ Convertible . Type ] = [ ] ,
306271 jsonValue: @escaping JSONValueConfig ) {
307- let _ = initLock
308- pthread_rwlock_wrlock ( & lock)
309- defer { pthread_rwlock_unlock ( & lock) }
310-
311272 if types. count == 0 {
312273 global. jsonValue = jsonValue
313274 return
0 commit comments