-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛠 Refactor: Split
RNIDictionarySynthesizable
Summary: Extract extension in `RNIDictionarySynthesizable` into its own file in `os/src_library/React Native/RNIDictionarySynthesizable/RNIDictionarySynthesizable+Default`. empty
- Loading branch information
1 parent
7002f39
commit 2573bb0
Showing
2 changed files
with
105 additions
and
83 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
..._library/React Native/RNIDictionarySynthesizable/RNIDictionarySynthesizable+Default.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// RNIDictionarySynthesizable+Default.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 4/27/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension RNIDictionarySynthesizable { | ||
|
||
// MARK: - Static Properties | ||
// ------------------------- | ||
|
||
public static var synthesizedDictionaryIgnore: [String] { | ||
[]; | ||
}; | ||
|
||
public static var synthesizedDictionaryInlinedProperties: [PartialKeyPath<Self>] { | ||
[]; | ||
}; | ||
|
||
// MARK: - Static Functions | ||
// ------------------------ | ||
|
||
fileprivate static func recursivelyParseValue( | ||
_ value: Any, | ||
isJSDict: Bool | ||
) -> Any { | ||
|
||
if let synthesizableDict = value as? (any RNIDictionarySynthesizable) { | ||
return synthesizableDict.synthesizedDictionary(isJSDict: isJSDict); | ||
|
||
} else if isJSDict, let rawValue = value as? any RawRepresentable { | ||
return rawValue.rawValue; | ||
|
||
} else if isJSDict, let array = value as? Array<Any> { | ||
return array.map { | ||
return Self.recursivelyParseValue($0, isJSDict: isJSDict); | ||
}; | ||
}; | ||
|
||
return value; | ||
}; | ||
|
||
// MARK: - Public Functions | ||
// ------------------------ | ||
|
||
public func synthesizedDictionary( | ||
isJSDict: Bool | ||
) -> Dictionary<String, Any> { | ||
|
||
let mirror = Mirror(reflecting: self); | ||
let properties = mirror.children; | ||
|
||
#if DEBUG | ||
/// Runtime Check - Verify if `synthesizedDictionaryIgnore` is valid | ||
for propertyKeyToIgnore in Self.synthesizedDictionaryIgnore { | ||
if !properties.contains(where: { $0.label == propertyKeyToIgnore }) { | ||
fatalError( | ||
"Invalid value of '\(propertyKeyToIgnore)' in " | ||
+ "'synthesizedDictionaryIgnore' for '\(Self.self)' - " | ||
+ "No property named '\(propertyKeyToIgnore)' in '\(Self.self)'" | ||
); | ||
}; | ||
}; | ||
#endif | ||
|
||
let propertyValueMap = properties.lazy.map { ( | ||
propertyKey: String?, value: Any) -> (String, Any)? in | ||
|
||
guard let propertyKey = propertyKey, | ||
!Self.synthesizedDictionaryIgnore.contains(propertyKey) | ||
else { return nil }; | ||
|
||
let parsedValue = Self.recursivelyParseValue(value, isJSDict: isJSDict); | ||
return (propertyKey, parsedValue); | ||
}; | ||
|
||
var baseDict = Dictionary( | ||
uniqueKeysWithValues: propertyValueMap.compactMap { $0 } | ||
); | ||
|
||
Self.synthesizedDictionaryInlinedProperties.forEach { | ||
guard let value = self[keyPath: $0] as? (any RNIDictionarySynthesizable) | ||
else { return }; | ||
|
||
let inlinedDict = value.synthesizedDictionary(isJSDict: isJSDict); | ||
baseDict = baseDict.merging(inlinedDict){ old, _ in old }; | ||
}; | ||
|
||
return baseDict; | ||
}; | ||
|
||
public var synthesizedJSDictionary: Dictionary<String, Any> { | ||
self.synthesizedDictionary(isJSDict: true); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters