Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Last active September 4, 2018 05:44
Show Gist options
  • Save marty-suzuki/cce0d967a8bbf57b58bf951c6b97f999 to your computer and use it in GitHub Desktop.
Save marty-suzuki/cce0d967a8bbf57b58bf951c6b97f999 to your computer and use it in GitHub Desktop.
@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)
}
}
extension PrivateProperties {
func property<T>(forKey key: String) -> T {
return Mirror(reflecting: base).children.first { $0.label == key }?.value as! T
}
}
{% 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 property(forKey: "{{variable.name}}")
}
{% endfor %}
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment