Dynamic member lookup (via marker protocol)
This commit is contained in:
committed by
David Jennes
parent
203510175f
commit
7247d0a83d
15
Sources/DynamicMemberLookup.swift
Normal file
15
Sources/DynamicMemberLookup.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
/// Marker protocol so we can know which types support `@dynamicMemberLookup`. Add this to your own types that support
|
||||
/// lookup by String.
|
||||
public protocol DynamicMemberLookup {
|
||||
/// Get a value for a given `String` key
|
||||
subscript(dynamicMember member: String) -> Any? { get }
|
||||
}
|
||||
|
||||
public extension DynamicMemberLookup where Self: RawRepresentable {
|
||||
subscript(dynamicMember member: String) -> Any? {
|
||||
switch member {
|
||||
case "rawValue": return rawValue
|
||||
default: return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,6 +110,8 @@ public struct Variable: Equatable, Resolvable {
|
||||
return object.value(forKey: bit)
|
||||
}
|
||||
#endif
|
||||
} else if let value = context as? DynamicMemberLookup {
|
||||
return value[dynamicMember: bit]
|
||||
} else if let value = context {
|
||||
return Mirror(reflecting: value).getValue(for: bit)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user