Skip to content

Commit 38886e0

Browse files
committed
Add a naive canBeRepresentedInObjectiveC to TypeSyntax
This checks for a set of known type names, hence isn't super reliable. It cannot distinguish between struct types an target relationship types. E.g. `var addresses = [ Address ]`, could be a toMany relship or an array of Address structs.
1 parent c183a5e commit 38886e0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Sources/ManagedModelMacros/Utilities/AttributeTypes.swift

+31
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,38 @@ private let toManyRelationshipTypes : Set<String> = [
4848
]
4949

5050
extension TypeSyntax {
51+
52+
/// Whether the type can be represented in Objective-C.
53+
/// A *very* basic implementation.
54+
var canBeRepresentedInObjectiveC : Bool {
55+
// TODO: Naive shortcut
56+
if let id = self.as(IdentifierTypeSyntax.self) {
57+
return id.isKnownAttributePropertyType
58+
|| id.isKnownRelationshipPropertyType
59+
}
60+
61+
if let opt = self.as(OptionalTypeSyntax.self) {
62+
if let id = opt.wrappedType.as(IdentifierTypeSyntax.self) {
63+
return id.isKnownAttributePropertyType
64+
|| id.isKnownRelationshipPropertyType
65+
}
66+
// E.g. this is not representable: `String??`, this is `String?`.
67+
// I.e. nesting of Optional's are not representable.
68+
return false
69+
}
70+
if let array = self.as(ArrayTypeSyntax.self) {
71+
// This *is* representable: `[String]`,
72+
// even this `[ [ 10, 20 ], [ 30, 40 ] ]`
73+
return array.element.canBeRepresentedInObjectiveC
74+
}
75+
76+
return false
77+
}
5178

79+
/**
80+
* This checks for some known basetypes, like `Int`, `String` or `Data`.
81+
* It also unwraps optionals, arrays and such.
82+
*/
5283
var isKnownAttributePropertyType : Bool {
5384
if let id = self.as(IdentifierTypeSyntax.self) {
5485
return id.isKnownAttributePropertyType

0 commit comments

Comments
 (0)