Files
Ruslan A 7153768c65 Fix associatedtype generics (#1345)
* Preliminary associatedtype support

* Implemented associatedtype support with generic requirements

* Fixed failing test

* Squashed commit of the following:

commit 9d01e6f99a
Author: Ruslan A <r.alikhamov@gmail.com>
Date:   Fri Jun 14 20:06:41 2024 +0400

    Improved concurrency support in SwiftTemplate caching (#1344)

* Removed test code

* Removed comment

* Updated Linux classes

* update internal boilerplate code.

* Updated generated code

* Removed warnings

* Updated expected file

* Updated expected file

* Adjusted protocol type for Linux

* Removed protocol composition due to Swift compiler crash under Linux
2024-06-19 22:50:18 +04:00

174 lines
6.6 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
public final class Enum: Type {
public override subscript(dynamicMember member: String) -> Any? {
switch member {
case "cases":
return cases
case "hasAssociatedValues":
return hasAssociatedValues
default:
return super[dynamicMember: member]
}
}
// sourcery: skipJSExport
public class var kind: String { return "enum" }
// sourcery: skipDescription
/// Returns "enum"
public override var kind: String { Self.kind }
/// 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, kind: Self.kind)
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