@@ -8,7 +8,7 @@ import SwiftSyntax
8
8
// This is a little fishy as the user might shadow those types,
9
9
// but I suppose an acceptable tradeoff.
10
10
11
- private let attributeTypes : Set < String > = [
11
+ private let swiftTypes : Set < String > = [
12
12
// Swift
13
13
" String " ,
14
14
" Int " , " Int8 " , " Int16 " , " Int32 " , " Int64 " ,
@@ -20,7 +20,8 @@ private let attributeTypes : Set<String> = [
20
20
" Swift.UInt " , " Swift.UInt8 " , " Swift.UInt16 " , " Swift.UInt32 " , " Swift.UInt64 " ,
21
21
" Swift.Float " , " Swift.Double " ,
22
22
" Swift.Bool " ,
23
-
23
+ ]
24
+ private let foundationTypes : Set < String > = [
24
25
// Foundation
25
26
" Data " , " Foundation.Data " ,
26
27
" Date " , " Foundation.Date " ,
@@ -33,6 +34,7 @@ private let attributeTypes : Set<String> = [
33
34
" NSURL " , " Foundation.NSURL " ,
34
35
" NSData " , " Foundation.NSData "
35
36
]
37
+ private let attributeTypes : Set < String > = swiftTypes. union ( foundationTypes)
36
38
37
39
private let toOneRelationshipTypes : Set < String > = [
38
40
// CoreData
@@ -42,6 +44,8 @@ private let toOneRelationshipTypes : Set<String> = [
42
44
]
43
45
private let toManyRelationshipTypes : Set < String > = [
44
46
// Foundation
47
+ " Array " , " Foundation.Array " ,
48
+ " NSArray " , " Foundation.NSArray " ,
45
49
" Set " , " Foundation.Set " ,
46
50
" NSSet " , " Foundation.NSSet " ,
47
51
" NSOrderedSet " , " Foundation.NSOrderedSet "
@@ -52,28 +56,36 @@ extension TypeSyntax {
52
56
/// Whether the type can be represented in Objective-C.
53
57
/// A *very* basic implementation.
54
58
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
59
if let opt = self . as ( OptionalTypeSyntax . self) {
60
+ if let array = opt. wrappedType. as ( ArrayTypeSyntax . self) {
61
+ return array. element. canBeRepresentedInObjectiveC
62
+ }
62
63
if let id = opt. wrappedType. as ( IdentifierTypeSyntax . self) {
63
- return id. isKnownAttributePropertyType
64
- || id. isKnownRelationshipPropertyType
64
+ if id. isKnownRelationshipPropertyType {
65
+ let element = id. genericArgumentClause? . arguments. first? . argument
66
+ return element? . isKnownAttributePropertyType ?? false
67
+ }
68
+ return id. isKnownFoundationPropertyType
65
69
}
66
70
// E.g. this is not representable: `String??`, this is `String?`.
71
+ // But Double? or Int? is not representable
67
72
// I.e. nesting of Optional's are not representable.
68
73
return false
69
74
}
75
+
70
76
if let array = self . as ( ArrayTypeSyntax . self) {
71
77
// This *is* representable: `[String]`,
72
78
// even this `[ [ 10, 20 ], [ 30, 40 ] ]`
73
79
return array. element. canBeRepresentedInObjectiveC
74
80
}
75
-
76
- return false
81
+
82
+ if let id = self . as ( IdentifierTypeSyntax . self) ,
83
+ id. isKnownFoundationGenericPropertyType {
84
+ let arg = id. genericArgumentClause? . arguments. first? . argument
85
+ return arg? . isKnownAttributePropertyType ?? false
86
+ }
87
+
88
+ return self . isKnownAttributePropertyType
77
89
}
78
90
79
91
/**
@@ -137,7 +149,23 @@ extension IdentifierTypeSyntax {
137
149
return false
138
150
}
139
151
}
152
+
153
+ var isKnownFoundationPropertyType : Bool {
154
+ let name = name. trimmed. text
155
+ return foundationTypes. contains ( name)
156
+ }
140
157
158
+ var isKnownFoundationGenericPropertyType : Bool {
159
+ let name = name. trimmed. text
160
+ guard toManyRelationshipTypes. contains ( name) else {
161
+ return false
162
+ }
163
+ if let generic = genericArgumentClause {
164
+ return generic. arguments. count == 1
165
+ }
166
+ return false
167
+ }
168
+
141
169
var isKnownRelationshipPropertyType : Bool {
142
170
isKnownRelationshipPropertyType ( checkOptional: true )
143
171
}
0 commit comments