mirror of
https://github.com/krzysztofzablocki/Sourcery.git
synced 2026-04-07 19:17:40 +00:00
cec7895f0a
* 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
161 lines
6.2 KiB
Swift
161 lines
6.2 KiB
Swift
//
|
|
// Created by Krzysztof Zablocki on 13/09/2016.
|
|
// Copyright (c) 2016 Pixle. All rights reserved.
|
|
//
|
|
|
|
#if canImport(ObjectiveC)
|
|
import Foundation
|
|
|
|
/// Defines Swift enum
|
|
@objcMembers
|
|
public final class Enum: Type {
|
|
// sourcery: skipDescription
|
|
/// Returns "enum"
|
|
public override var kind: String { return "enum" }
|
|
|
|
/// Enum cases
|
|
public var cases: [EnumCase]
|
|
|
|
/**
|
|
Enum raw value type name, if any. This type is removed from enum's `based` and `inherited` types collections.
|
|
|
|
- important: Unless raw type is specified explicitly via type alias RawValue it will be set to the first type in the inheritance chain.
|
|
So if your enum does not have raw value but implements protocols you'll have to specify conformance to these protocols via extension to get enum with nil raw value type and all based and inherited types.
|
|
*/
|
|
public var rawTypeName: TypeName? {
|
|
didSet {
|
|
if let rawTypeName = rawTypeName {
|
|
hasRawType = true
|
|
if let index = inheritedTypes.firstIndex(of: rawTypeName.name) {
|
|
inheritedTypes.remove(at: index)
|
|
}
|
|
if based[rawTypeName.name] != nil {
|
|
based[rawTypeName.name] = nil
|
|
}
|
|
} else {
|
|
hasRawType = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// sourcery: skipDescription, skipEquality
|
|
/// :nodoc:
|
|
public private(set) var hasRawType: Bool
|
|
|
|
// sourcery: skipDescription, skipEquality
|
|
/// Enum raw value type, if known
|
|
public var rawType: Type?
|
|
|
|
// sourcery: skipEquality, skipDescription, skipCoding
|
|
/// Names of types or protocols this type inherits from, including unknown (not scanned) types
|
|
public override var based: [String: String] {
|
|
didSet {
|
|
if let rawTypeName = rawTypeName, based[rawTypeName.name] != nil {
|
|
based[rawTypeName.name] = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether enum contains any associated values
|
|
public var hasAssociatedValues: Bool {
|
|
return cases.contains(where: { $0.hasAssociatedValue })
|
|
}
|
|
|
|
/// :nodoc:
|
|
public init(name: String = "",
|
|
parent: Type? = nil,
|
|
accessLevel: AccessLevel = .internal,
|
|
isExtension: Bool = false,
|
|
inheritedTypes: [String] = [],
|
|
rawTypeName: TypeName? = nil,
|
|
cases: [EnumCase] = [],
|
|
variables: [Variable] = [],
|
|
methods: [Method] = [],
|
|
containedTypes: [Type] = [],
|
|
typealiases: [Typealias] = [],
|
|
attributes: AttributeList = [:],
|
|
modifiers: [SourceryModifier] = [],
|
|
annotations: [String: NSObject] = [:],
|
|
documentation: [String] = [],
|
|
isGeneric: Bool = false) {
|
|
|
|
self.cases = cases
|
|
self.rawTypeName = rawTypeName
|
|
self.hasRawType = rawTypeName != nil || !inheritedTypes.isEmpty
|
|
|
|
super.init(name: name, parent: parent, accessLevel: accessLevel, isExtension: isExtension, variables: variables, methods: methods, inheritedTypes: inheritedTypes, containedTypes: containedTypes, typealiases: typealiases, attributes: attributes, modifiers: modifiers, annotations: annotations, documentation: documentation, isGeneric: isGeneric)
|
|
|
|
if let rawTypeName = rawTypeName?.name, let index = self.inheritedTypes.firstIndex(of: rawTypeName) {
|
|
self.inheritedTypes.remove(at: index)
|
|
}
|
|
}
|
|
|
|
/// :nodoc:
|
|
// sourcery: skipJSExport
|
|
override public var description: String {
|
|
var string = super.description
|
|
string.append(", ")
|
|
string.append("cases = \(String(describing: self.cases)), ")
|
|
string.append("rawTypeName = \(String(describing: self.rawTypeName)), ")
|
|
string.append("hasAssociatedValues = \(String(describing: self.hasAssociatedValues))")
|
|
return string
|
|
}
|
|
|
|
override public func diffAgainst(_ object: Any?) -> DiffableResult {
|
|
let results = DiffableResult()
|
|
guard let castObject = object as? Enum else {
|
|
results.append("Incorrect type <expected: Enum, received: \(Swift.type(of: object))>")
|
|
return results
|
|
}
|
|
results.append(contentsOf: DiffableResult(identifier: "cases").trackDifference(actual: self.cases, expected: castObject.cases))
|
|
results.append(contentsOf: DiffableResult(identifier: "rawTypeName").trackDifference(actual: self.rawTypeName, expected: castObject.rawTypeName))
|
|
results.append(contentsOf: super.diffAgainst(castObject))
|
|
return results
|
|
}
|
|
|
|
/// :nodoc:
|
|
// sourcery: skipJSExport
|
|
public override var hash: Int {
|
|
var hasher = Hasher()
|
|
hasher.combine(self.cases)
|
|
hasher.combine(self.rawTypeName)
|
|
hasher.combine(super.hash)
|
|
return hasher.finalize()
|
|
}
|
|
|
|
/// :nodoc:
|
|
public override func isEqual(_ object: Any?) -> Bool {
|
|
guard let rhs = object as? Enum else { return false }
|
|
if self.cases != rhs.cases { return false }
|
|
if self.rawTypeName != rhs.rawTypeName { return false }
|
|
return super.isEqual(rhs)
|
|
}
|
|
|
|
// sourcery:inline:Enum.AutoCoding
|
|
|
|
/// :nodoc:
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
guard let cases: [EnumCase] = aDecoder.decode(forKey: "cases") else {
|
|
withVaList(["cases"]) { arguments in
|
|
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
|
|
}
|
|
fatalError()
|
|
}; self.cases = cases
|
|
self.rawTypeName = aDecoder.decode(forKey: "rawTypeName")
|
|
self.hasRawType = aDecoder.decode(forKey: "hasRawType")
|
|
self.rawType = aDecoder.decode(forKey: "rawType")
|
|
super.init(coder: aDecoder)
|
|
}
|
|
|
|
/// :nodoc:
|
|
override public func encode(with aCoder: NSCoder) {
|
|
super.encode(with: aCoder)
|
|
aCoder.encode(self.cases, forKey: "cases")
|
|
aCoder.encode(self.rawTypeName, forKey: "rawTypeName")
|
|
aCoder.encode(self.hasRawType, forKey: "hasRawType")
|
|
aCoder.encode(self.rawType, forKey: "rawType")
|
|
}
|
|
// sourcery:end
|
|
}
|
|
#endif
|