mirror of
https://github.com/krzysztofzablocki/Sourcery.git
synced 2026-04-07 19:17:40 +00:00
* Adjusted file structure to accommodate two generated files * Adjusted scripting * Removed Description, Diffable, Equality for now * Removed Stencil import * Updated templates * Updated scripting * Updated generated coding * Disable build deletion (temp) * Updated template * Updated generated code * updated generated code * Updated generated code * Adjusted scripting * Updated scripting * Updated generated code * Reverted template deletion * Removed Stencil imports * Updated generated code * trigger CI * Removed description, diffable and equality stencil templates * Reverted temporary changes * Commented failing tests * Skipping JSExport for description & hash * Updated generated code * Enabled failing tests * Adding stencil templates back to test tests * Reset generated file for linux for test * Reverted Extensions for testing * Reverted ParserResultsComposed * Attempt to fix unit tests * Attempt to resolve unit test * Reverted TypeName asSource * Reverted TypeName revertion * Reverted revert of ParserResultComposed * Reverted revert of Extensions * Reverted revert of Linux.content.generated * Reverted attempts to fix unit tests * Fix for the failing codegen tests * Added clarifying comment * Removed description, diffable and equality stencil templates * Updated generated code * Tinkering with optimization level for speed boost * Excluded stencil templates for codegen * Fixed wrong compiler flag * Removed speed optimization to a separate PR * Reverted test code
163 lines
7.1 KiB
Swift
163 lines
7.1 KiB
Swift
//
|
|
// Created by Krzysztof Zablocki on 13/09/2016.
|
|
// Copyright (c) 2016 Pixle. All rights reserved.
|
|
//
|
|
#if !canImport(ObjectiveC)
|
|
import Foundation
|
|
|
|
/// Defines enum case
|
|
public final class EnumCase: NSObject, SourceryModel, AutoDescription, Annotated, Documented, Diffable, SourceryDynamicMemberLookup {
|
|
public subscript(dynamicMember member: String) -> Any? {
|
|
switch member {
|
|
case "name":
|
|
return name
|
|
case "hasAssociatedValue":
|
|
return hasAssociatedValue
|
|
case "associatedValues":
|
|
return associatedValues
|
|
case "indirect":
|
|
return indirect
|
|
case "annotations":
|
|
return annotations
|
|
case "rawValue":
|
|
return rawValue
|
|
default:
|
|
fatalError("unable to lookup: \(member) in \(self)")
|
|
}
|
|
}
|
|
/// Enum case name
|
|
public let name: String
|
|
|
|
/// Enum case raw value, if any
|
|
public let rawValue: String?
|
|
|
|
/// Enum case associated values
|
|
public let associatedValues: [AssociatedValue]
|
|
|
|
/// Enum case annotations
|
|
public var annotations: Annotations = [:]
|
|
|
|
public var documentation: Documentation = []
|
|
|
|
/// Whether enum case is indirect
|
|
public let indirect: Bool
|
|
|
|
/// Whether enum case has associated value
|
|
public var hasAssociatedValue: Bool {
|
|
return !associatedValues.isEmpty
|
|
}
|
|
|
|
// Underlying parser data, never to be used by anything else
|
|
// sourcery: skipEquality, skipDescription, skipCoding, skipJSExport
|
|
/// :nodoc:
|
|
public var __parserData: Any?
|
|
|
|
/// :nodoc:
|
|
public init(name: String, rawValue: String? = nil, associatedValues: [AssociatedValue] = [], annotations: [String: NSObject] = [:], documentation: [String] = [], indirect: Bool = false) {
|
|
self.name = name
|
|
self.rawValue = rawValue
|
|
self.associatedValues = associatedValues
|
|
self.annotations = annotations
|
|
self.documentation = documentation
|
|
self.indirect = indirect
|
|
}
|
|
|
|
/// :nodoc:
|
|
// sourcery: skipJSExport
|
|
override public var description: String {
|
|
var string = "\(Swift.type(of: self)): "
|
|
string.append("name = \(String(describing: self.name)), ")
|
|
string.append("rawValue = \(String(describing: self.rawValue)), ")
|
|
string.append("associatedValues = \(String(describing: self.associatedValues)), ")
|
|
string.append("annotations = \(String(describing: self.annotations)), ")
|
|
string.append("documentation = \(String(describing: self.documentation)), ")
|
|
string.append("indirect = \(String(describing: self.indirect)), ")
|
|
string.append("hasAssociatedValue = \(String(describing: self.hasAssociatedValue))")
|
|
return string
|
|
}
|
|
|
|
public func diffAgainst(_ object: Any?) -> DiffableResult {
|
|
let results = DiffableResult()
|
|
guard let castObject = object as? EnumCase else {
|
|
results.append("Incorrect type <expected: EnumCase, received: \(Swift.type(of: object))>")
|
|
return results
|
|
}
|
|
results.append(contentsOf: DiffableResult(identifier: "name").trackDifference(actual: self.name, expected: castObject.name))
|
|
results.append(contentsOf: DiffableResult(identifier: "rawValue").trackDifference(actual: self.rawValue, expected: castObject.rawValue))
|
|
results.append(contentsOf: DiffableResult(identifier: "associatedValues").trackDifference(actual: self.associatedValues, expected: castObject.associatedValues))
|
|
results.append(contentsOf: DiffableResult(identifier: "annotations").trackDifference(actual: self.annotations, expected: castObject.annotations))
|
|
results.append(contentsOf: DiffableResult(identifier: "documentation").trackDifference(actual: self.documentation, expected: castObject.documentation))
|
|
results.append(contentsOf: DiffableResult(identifier: "indirect").trackDifference(actual: self.indirect, expected: castObject.indirect))
|
|
return results
|
|
}
|
|
|
|
/// :nodoc:
|
|
// sourcery: skipJSExport
|
|
public override var hash: Int {
|
|
var hasher = Hasher()
|
|
hasher.combine(self.name)
|
|
hasher.combine(self.rawValue)
|
|
hasher.combine(self.associatedValues)
|
|
hasher.combine(self.annotations)
|
|
hasher.combine(self.documentation)
|
|
hasher.combine(self.indirect)
|
|
return hasher.finalize()
|
|
}
|
|
|
|
/// :nodoc:
|
|
public override func isEqual(_ object: Any?) -> Bool {
|
|
guard let rhs = object as? EnumCase else { return false }
|
|
if self.name != rhs.name { return false }
|
|
if self.rawValue != rhs.rawValue { return false }
|
|
if self.associatedValues != rhs.associatedValues { return false }
|
|
if self.annotations != rhs.annotations { return false }
|
|
if self.documentation != rhs.documentation { return false }
|
|
if self.indirect != rhs.indirect { return false }
|
|
return true
|
|
}
|
|
|
|
// sourcery:inline:EnumCase.AutoCoding
|
|
|
|
/// :nodoc:
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
guard let name: String = aDecoder.decode(forKey: "name") else {
|
|
withVaList(["name"]) { arguments in
|
|
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
|
|
}
|
|
fatalError()
|
|
}; self.name = name
|
|
self.rawValue = aDecoder.decode(forKey: "rawValue")
|
|
guard let associatedValues: [AssociatedValue] = aDecoder.decode(forKey: "associatedValues") else {
|
|
withVaList(["associatedValues"]) { arguments in
|
|
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
|
|
}
|
|
fatalError()
|
|
}; self.associatedValues = associatedValues
|
|
guard let annotations: Annotations = aDecoder.decode(forKey: "annotations") else {
|
|
withVaList(["annotations"]) { arguments in
|
|
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
|
|
}
|
|
fatalError()
|
|
}; self.annotations = annotations
|
|
guard let documentation: Documentation = aDecoder.decode(forKey: "documentation") else {
|
|
withVaList(["documentation"]) { arguments in
|
|
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
|
|
}
|
|
fatalError()
|
|
}; self.documentation = documentation
|
|
self.indirect = aDecoder.decode(forKey: "indirect")
|
|
}
|
|
|
|
/// :nodoc:
|
|
public func encode(with aCoder: NSCoder) {
|
|
aCoder.encode(self.name, forKey: "name")
|
|
aCoder.encode(self.rawValue, forKey: "rawValue")
|
|
aCoder.encode(self.associatedValues, forKey: "associatedValues")
|
|
aCoder.encode(self.annotations, forKey: "annotations")
|
|
aCoder.encode(self.documentation, forKey: "documentation")
|
|
aCoder.encode(self.indirect, forKey: "indirect")
|
|
}
|
|
// sourcery:end
|
|
}
|
|
#endif
|