-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
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
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,147 @@ | ||
// | ||
// Protocol.swift | ||
// Sourcery | ||
// | ||
// Created by Krzysztof Zablocki on 09/12/2016. | ||
// Copyright © 2016 Pixle. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if !canImport(ObjectiveC) | ||
|
||
/// :nodoc: | ||
public typealias SourceryProtocol = Protocol | ||
|
||
/// Describes Swift protocol | ||
public final class Protocol: Type { | ||
public override subscript(dynamicMember member: String) -> Any? { | ||
switch member { | ||
case "associatedTypes": | ||
return associatedTypes | ||
default: | ||
return super[dynamicMember: member] | ||
} | ||
} | ||
|
||
// sourcery: skipJSExport | ||
public class var kind: String { return "protocol" } | ||
|
||
/// Returns "protocol" | ||
public override var kind: String { Self.kind } | ||
|
||
/// list of all declared associated types with their names as keys | ||
public var associatedTypes: [String: AssociatedType] { | ||
didSet { | ||
isGeneric = !associatedTypes.isEmpty || !genericRequirements.isEmpty | ||
} | ||
} | ||
|
||
// sourcery: skipCoding | ||
/// list of generic requirements | ||
public override var genericRequirements: [GenericRequirement] { | ||
didSet { | ||
isGeneric = !associatedTypes.isEmpty || !genericRequirements.isEmpty | ||
} | ||
} | ||
|
||
/// :nodoc: | ||
public init(name: String = "", | ||
parent: Type? = nil, | ||
accessLevel: AccessLevel = .internal, | ||
isExtension: Bool = false, | ||
variables: [Variable] = [], | ||
methods: [Method] = [], | ||
subscripts: [Subscript] = [], | ||
inheritedTypes: [String] = [], | ||
containedTypes: [Type] = [], | ||
typealiases: [Typealias] = [], | ||
associatedTypes: [String: AssociatedType] = [:], | ||
genericRequirements: [GenericRequirement] = [], | ||
attributes: AttributeList = [:], | ||
modifiers: [SourceryModifier] = [], | ||
annotations: [String: NSObject] = [:], | ||
documentation: [String] = [], | ||
implements: [String: Type] = [:], | ||
kind: String = Protocol.kind) { | ||
self.associatedTypes = associatedTypes | ||
super.init( | ||
name: name, | ||
parent: parent, | ||
accessLevel: accessLevel, | ||
isExtension: isExtension, | ||
variables: variables, | ||
methods: methods, | ||
subscripts: subscripts, | ||
inheritedTypes: inheritedTypes, | ||
containedTypes: containedTypes, | ||
typealiases: typealiases, | ||
genericRequirements: genericRequirements, | ||
attributes: attributes, | ||
modifiers: modifiers, | ||
annotations: annotations, | ||
documentation: documentation, | ||
isGeneric: !associatedTypes.isEmpty || !genericRequirements.isEmpty, | ||
implements: implements, | ||
kind: kind | ||
) | ||
} | ||
|
||
/// :nodoc: | ||
// sourcery: skipJSExport | ||
override public var description: String { | ||
var string = super.description | ||
string.append(", ") | ||
string.append("kind = \(String(describing: self.kind)), ") | ||
string.append("associatedTypes = \(String(describing: self.associatedTypes)), ") | ||
return string | ||
} | ||
|
||
override public func diffAgainst(_ object: Any?) -> DiffableResult { | ||
let results = DiffableResult() | ||
guard let castObject = object as? Protocol else { | ||
results.append("Incorrect type <expected: Protocol, received: \(Swift.type(of: object))>") | ||
return results | ||
} | ||
results.append(contentsOf: DiffableResult(identifier: "associatedTypes").trackDifference(actual: self.associatedTypes, expected: castObject.associatedTypes)) | ||
results.append(contentsOf: super.diffAgainst(castObject)) | ||
return results | ||
} | ||
|
||
/// :nodoc: | ||
// sourcery: skipJSExport | ||
public override var hash: Int { | ||
var hasher = Hasher() | ||
hasher.combine(self.associatedTypes) | ||
hasher.combine(super.hash) | ||
return hasher.finalize() | ||
} | ||
|
||
/// :nodoc: | ||
public override func isEqual(_ object: Any?) -> Bool { | ||
guard let rhs = object as? Protocol else { return false } | ||
if self.associatedTypes != rhs.associatedTypes { return false } | ||
return super.isEqual(rhs) | ||
} | ||
|
||
// sourcery:inline:Protocol.AutoCoding | ||
|
||
/// :nodoc: | ||
required public init?(coder aDecoder: NSCoder) { | ||
guard let associatedTypes: [String: AssociatedType] = aDecoder.decode(forKey: "associatedTypes") else { | ||
withVaList(["associatedTypes"]) { arguments in | ||
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments) | ||
} | ||
fatalError() | ||
}; self.associatedTypes = associatedTypes | ||
super.init(coder: aDecoder) | ||
} | ||
|
||
/// :nodoc: | ||
override public func encode(with aCoder: NSCoder) { | ||
super.encode(with: aCoder) | ||
aCoder.encode(self.associatedTypes, forKey: "associatedTypes") | ||
} | ||
// sourcery:end | ||
} | ||
#endif |