Last active
September 2, 2018 08:30
-
-
Save marty-suzuki/8a6ab234835fd9fe0216c6ab971ec75f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@testable import 'Your Target Name' | |
import UIKit | |
import Foundation | |
/// Define this protocol in your target | |
// protocol AutoPrivatePropertyAccessible {} | |
// MARK: - PrivatePropertyAccessible | |
protocol PrivatePropertyAccessible { | |
associatedtype PrivatePropertiesCompatible | |
var privateProperties: PrivateProperties<PrivatePropertiesCompatible> { get } | |
} | |
struct PrivateProperties<Base> { | |
fileprivate let base: Base | |
init(_ base: Base) { | |
self.base = base | |
} | |
} | |
extension PrivatePropertyAccessible where PrivatePropertiesCompatible == Self { | |
var privateProperties: PrivateProperties<Self> { | |
return PrivateProperties(self) | |
} | |
} | |
{% for type in types.implementing.AutoPrivatePropertyAccessible %} | |
// MARK: - {{ type.name }} | |
extension {{type.name}}: PrivatePropertyAccessible {} | |
extension PrivateProperties where Base == {{type.name}} { | |
{% for variable in type.storedVariables where variable.readAccess == "private" or variable.readAccess == "fileprivate" %} | |
var {{variable.name}}: {{variable.typeName}} { | |
return base.value(forKey: "{{variable.name}}") {% if variable.isOptional %}as? {{variable.unwrappedTypeName}}{% else %}as! {{variable.unwrappedTypeName}}{% endif %} | |
} | |
{% endfor %} | |
} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment