Skip to content

Commit

Permalink
Added isDistributed to Actor and Method (#1318)
Browse files Browse the repository at this point in the history
* Added isDistributed to Actor and Method

* update internal boilerplate code.
  • Loading branch information
art-divin committed Mar 28, 2024
1 parent 7bc8e19 commit 9fb3d03
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 68 deletions.
8 changes: 7 additions & 1 deletion SourceryRuntime/Sources/Common/AST/Actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public final class Actor: Type {
modifiers.contains { $0.name == "final" }
}

/// Whether method is distributed method
public var isDistributed: Bool {
modifiers.contains(where: { $0.name == "distributed" })
}

/// :nodoc:
public override init(name: String = "",
parent: Type? = nil,
Expand Down Expand Up @@ -59,7 +64,8 @@ public final class Actor: Type {
var string = super.description
string.append(", ")
string.append("kind = \(String(describing: self.kind)), ")
string.append("isFinal = \(String(describing: self.isFinal))")
string.append("isFinal = \(String(describing: self.isFinal)), ")
string.append("isDistributed = \(String(describing: self.isDistributed))")
return string
}

Expand Down
2 changes: 2 additions & 0 deletions SourceryRuntime/Sources/Generated/JSExport.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import JavaScriptCore
@objc protocol ActorAutoJSExport: JSExport {
var kind: String { get }
var isFinal: Bool { get }
var isDistributed: Bool { get }
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
Expand Down Expand Up @@ -322,6 +323,7 @@ extension Import: ImportAutoJSExport {}
var isImplicitlyUnwrappedOptionalReturnType: Bool { get }
var unwrappedReturnTypeName: String { get }
var isAsync: Bool { get }
var isDistributed: Bool { get }
var `throws`: Bool { get }
var `rethrows`: Bool { get }
var accessLevel: String { get }
Expand Down
8 changes: 7 additions & 1 deletion SourceryRuntime/Sources/Linux/AST/Method_Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
/// Whether method is async method
public let isAsync: Bool

/// Whether method is distributed
public var isDistributed: Bool {
modifiers.contains(where: { $0.name == "distributed" })
}

/// Whether method throws
public let `throws`: Bool

Expand Down Expand Up @@ -270,7 +275,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
string.append("documentation = \(String(describing: self.documentation)), ")
string.append("definedInTypeName = \(String(describing: self.definedInTypeName)), ")
string.append("attributes = \(String(describing: self.attributes)), ")
string.append("modifiers = \(String(describing: self.modifiers))")
string.append("modifiers = \(String(describing: self.modifiers)), ")
string.append("genericRequirements = \(String(describing: self.genericRequirements))")
return string
}
Expand Down Expand Up @@ -333,6 +338,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
if self.parameters != rhs.parameters { return false }
if self.returnTypeName != rhs.returnTypeName { return false }
if self.isAsync != rhs.isAsync { return false }
if self.isDistributed != rhs.isDistributed { return false }
if self.`throws` != rhs.`throws` { return false }
if self.`rethrows` != rhs.`rethrows` { return false }
if self.accessLevel != rhs.accessLevel { return false }
Expand Down
6 changes: 6 additions & 0 deletions SourceryRuntime/Sources/macOS/AST/Method.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
/// Whether method is async method
public let isAsync: Bool

/// Whether method is distributed
public var isDistributed: Bool {
modifiers.contains(where: { $0.name == "distributed" })
}

/// Whether method throws
public let `throws`: Bool

Expand Down Expand Up @@ -294,6 +299,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
if self.parameters != rhs.parameters { return false }
if self.returnTypeName != rhs.returnTypeName { return false }
if self.isAsync != rhs.isAsync { return false }
if self.isDistributed != rhs.isDistributed { return false }
if self.`throws` != rhs.`throws` { return false }
if self.`rethrows` != rhs.`rethrows` { return false }
if self.accessLevel != rhs.accessLevel { return false }
Expand Down
117 changes: 85 additions & 32 deletions SourcerySwift/Sources/SourceryRuntime.content.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public final class Actor: Type {
modifiers.contains { $0.name == "final" }
}
/// Whether method is distributed method
public var isDistributed: Bool {
modifiers.contains(where: { $0.name == "distributed" })
}
/// :nodoc:
public override init(name: String = "",
parent: Type? = nil,
Expand Down Expand Up @@ -84,7 +89,8 @@ public final class Actor: Type {
var string = super.description
string.append(", ")
string.append("kind = \\(String(describing: self.kind)), ")
string.append("isFinal = \\(String(describing: self.isFinal))")
string.append("isFinal = \\(String(describing: self.isFinal)), ")
string.append("isDistributed = \\(String(describing: self.isDistributed))")
return string
}
Expand Down Expand Up @@ -924,7 +930,7 @@ public enum Composer {
}
}
private static func findBaseType(for type: Type, name: String, typesByName: [String: Type]) -> Type? {
internal static func findBaseType(for type: Type, name: String, typesByName: [String: Type]) -> Type? {
// special case to check if the type is based on one of the recognized types
// and the superclass has a generic constraint in `name` part of the `TypeName`
var name = name
Expand All @@ -943,7 +949,16 @@ public enum Composer {
return baseType
}
}
return nil
guard name.contains("&") else { return nil }
// this can happen for a type which consists of mutliple types composed together (i.e. (A & B))
let nameComponents = name.components(separatedBy: "&").map { $0.trimmingCharacters(in: .whitespaces) }
let types: [Type] = nameComponents.compactMap {
typesByName[$0]
}
let typeNames = types.map {
TypeName(name: $0.name)
}
return ProtocolComposition(name: name, inheritedTypes: types.map { $0.globalName }, composedTypeNames: typeNames, composedTypes: types)
}
private static func updateTypeRelationship(for type: Type, typesByName: [String: Type], processed: inout [String: Bool]) {
Expand All @@ -954,31 +969,44 @@ public enum Composer {
processed[globalName] = true
updateTypeRelationship(for: baseType, typesByName: typesByName, processed: &processed)
}
copyTypeRelationships(from: baseType, to: type)
if let composedType = baseType as? ProtocolComposition {
let implements = composedType.composedTypes?.filter({ $0 is SourceryProtocol })
implements?.forEach { updateInheritsAndImplements(from: $0, to: type) }
if implements?.count == composedType.composedTypes?.count
|| composedType.composedTypes == nil
|| composedType.composedTypes?.isEmpty == true
{
type.implements[globalName] = baseType
}
} else {
updateInheritsAndImplements(from: baseType, to: type)
}
type.basedTypes[globalName] = baseType
}
}
baseType.based.keys.forEach { type.based[$0] = $0 }
baseType.basedTypes.forEach { type.basedTypes[$0.key] = $0.value }
baseType.inherits.forEach { type.inherits[$0.key] = $0.value }
baseType.implements.forEach { type.implements[$0.key] = $0.value }
if baseType is Class {
type.inherits[globalName] = baseType
} else if let baseProtocol = baseType as? SourceryProtocol {
type.implements[globalName] = baseProtocol
if let extendingProtocol = type as? SourceryProtocol {
baseProtocol.associatedTypes.forEach {
if extendingProtocol.associatedTypes[$0.key] == nil {
extendingProtocol.associatedTypes[$0.key] = $0.value
}
private static func updateInheritsAndImplements(from baseType: Type, to type: Type) {
if baseType is Class {
type.inherits[baseType.name] = baseType
} else if let `protocol` = baseType as? SourceryProtocol {
type.implements[baseType.name] = baseType
if let extendingProtocol = type as? SourceryProtocol {
`protocol`.associatedTypes.forEach {
if extendingProtocol.associatedTypes[$0.key] == nil {
extendingProtocol.associatedTypes[$0.key] = $0.value
}
}
} else if baseType is ProtocolComposition {
// TODO: associated types?
type.implements[globalName] = baseType
}
type.basedTypes[globalName] = baseType
}
}
private static func copyTypeRelationships(from baseType: Type, to type: Type) {
baseType.based.keys.forEach { type.based[$0] = $0 }
baseType.basedTypes.forEach { type.basedTypes[$0.key] = $0.value }
baseType.inherits.forEach { type.inherits[$0.key] = $0.value }
baseType.implements.forEach { type.implements[$0.key] = $0.value }
}
}
"""),
Expand Down Expand Up @@ -2264,14 +2292,31 @@ internal struct ParserResultsComposed {
// extend all types with their extensions
parsedTypes.forEach { type in
type.inheritedTypes = type.inheritedTypes.map { inheritedName in
resolveGlobalName(for: inheritedName, containingType: type.parent, unique: typeMap, modules: modules, typealiases: resolvedTypealiases)?.name ?? inheritedName
let inheritedTypes: [[String]] = type.inheritedTypes.compactMap { inheritedName in
if let resolvedGlobalName = resolveGlobalName(for: inheritedName, containingType: type.parent, unique: typeMap, modules: modules, typealiases: resolvedTypealiases)?.name {
return [resolvedGlobalName]
}
if let baseType = Composer.findBaseType(for: type, name: inheritedName, typesByName: typeMap) {
if let composed = baseType as? ProtocolComposition, let composedTypes = composed.composedTypes {
// ignore inheritedTypes when it is a `ProtocolComposition` and composedType is a protocol
var combinedTypes = composedTypes.map { $0.globalName }
combinedTypes.append(baseType.globalName)
return combinedTypes
}
}
return [inheritedName]
}
type.inheritedTypes = inheritedTypes.flatMap { $0 }
let uniqueType: Type?
if let mappedType = typeMap[type.globalName] {
// this check will only fail on an extension?
uniqueType = mappedType
} else if let composedNameType = typeFromComposedName(type.name, modules: modules) {
// this can happen for an extension on unknown type, this case should probably be handled by the inferTypeNameFromModules
uniqueType = composedNameType
} else {
uniqueType = inferTypeNameFromModules(from: type.localName, containedInType: type.parent, uniqueTypes: typeMap, modules: modules).flatMap { typeMap[$0] }
}
let uniqueType = typeMap[type.globalName] ?? // this check will only fail on an extension?
typeFromComposedName(type.name, modules: modules) ?? // this can happen for an extension on unknown type, this case should probably be handled by the inferTypeNameFromModules
(inferTypeNameFromModules(from: type.localName, containedInType: type.parent, uniqueTypes: typeMap, modules: modules).flatMap { typeMap[$0] })
guard let current = uniqueType else {
assert(type.isExtension, "Type \\(type.globalName) should be extension")
Expand Down Expand Up @@ -4762,13 +4807,13 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
public var selectorName: String
// sourcery: skipEquality, skipDescription
/// Method name without arguments names and parenthesis, i.e. `foo<T>`
/// Method name without arguments names and parentheses, i.e. `foo<T>`
public var shortName: String {
return name.range(of: "(").map({ String(name[..<$0.lowerBound]) }) ?? name
}
// sourcery: skipEquality, skipDescription
/// Method name without arguments names, parenthesis and generic types, i.e. `foo` (can be used to generate code for method call)
/// Method name without arguments names, parentheses and generic types, i.e. `foo` (can be used to generate code for method call)
public var callName: String {
return shortName.range(of: "<").map({ String(shortName[..<$0.lowerBound]) }) ?? shortName
}
Expand Down Expand Up @@ -4810,6 +4855,11 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
/// Whether method is async method
public let isAsync: Bool
/// Whether method is distributed
public var isDistributed: Bool {
modifiers.contains(where: { $0.name == "distributed" })
}
/// Whether method throws
public let `throws`: Bool
Expand Down Expand Up @@ -5041,6 +5091,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
if self.parameters != rhs.parameters { return false }
if self.returnTypeName != rhs.returnTypeName { return false }
if self.isAsync != rhs.isAsync { return false }
if self.isDistributed != rhs.isDistributed { return false }
if self.`throws` != rhs.`throws` { return false }
if self.`rethrows` != rhs.`rethrows` { return false }
if self.accessLevel != rhs.accessLevel { return false }
Expand Down Expand Up @@ -6150,7 +6201,7 @@ public class Type: NSObject, SourceryModel, Annotated, Documented, Diffable {
public var inherits = [String: Type]()
// sourcery: skipEquality, skipDescription
/// Protocols this type implements
/// Protocols this type implements. Does not contain classes in case where composition (`&`) is used in the declaration
public var implements: [String: Type] = [:]
/// Contained types
Expand Down Expand Up @@ -7640,6 +7691,7 @@ import JavaScriptCore
@objc protocol ActorAutoJSExport: JSExport {
var kind: String { get }
var isFinal: Bool { get }
var isDistributed: Bool { get }
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
Expand Down Expand Up @@ -7954,6 +8006,7 @@ extension Import: ImportAutoJSExport {}
var isImplicitlyUnwrappedOptionalReturnType: Bool { get }
var unwrappedReturnTypeName: String { get }
var isAsync: Bool { get }
var isDistributed: Bool { get }
var `throws`: Bool { get }
var `rethrows`: Bool { get }
var accessLevel: String { get }
Expand Down

0 comments on commit 9fb3d03

Please sign in to comment.