forked from realm/realm-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRLMProperty.mm
306 lines (270 loc) · 10.8 KB
/
RLMProperty.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#import "RLMProperty_Private.h"
#import "RLMArray.h"
#import "RLMObject.h"
#import "RLMSchema_Private.h"
#import "RLMSwiftSupport.h"
#import "RLMUtil.hpp"
@implementation RLMProperty {
NSString *_objcRawType;
}
- (instancetype)initWithName:(NSString *)name
type:(RLMPropertyType)type
objectClassName:(NSString *)objectClassName
attributes:(RLMPropertyAttributes)attributes {
self = [super init];
if (self) {
_name = name;
_type = type;
_objectClassName = objectClassName;
_attributes = attributes;
[self setObjcCodeFromType];
[self updateAccessors];
}
return self;
}
-(void)updateAccessors {
// populate getter/setter names if generic
if (!_getterName) {
_getterName = _name;
}
if (!_setterName) {
// Objective-C setters only capitalize the first letter of the property name if it falls between 'a' and 'z'
int asciiCode = [_name characterAtIndex:0];
BOOL shouldUppercase = asciiCode >= 'a' && asciiCode <= 'z';
NSString *firstChar = [_name substringToIndex:1];
firstChar = shouldUppercase ? firstChar.uppercaseString : firstChar;
_setterName = [NSString stringWithFormat:@"set%@%@:", firstChar, [_name substringFromIndex:1]];
}
_getterSel = NSSelectorFromString(_getterName);
_setterSel = NSSelectorFromString(_setterName);
}
-(void)setObjcCodeFromType {
switch (_type) {
case RLMPropertyTypeInt:
_objcType = 'q';
break;
case RLMPropertyTypeBool:
_objcType = 'c';
break;
case RLMPropertyTypeDouble:
_objcType = 'd';
break;
case RLMPropertyTypeFloat:
_objcType = 'f';
break;
case RLMPropertyTypeAny:
case RLMPropertyTypeArray:
case RLMPropertyTypeData:
case RLMPropertyTypeDate:
case RLMPropertyTypeObject:
case RLMPropertyTypeString:
_objcType = '@';
break;
}
}
// determine RLMPropertyType from objc code - returns true if valid type was found/set
-(BOOL)setTypeFromRawType {
const char *code = _objcRawType.UTF8String;
_objcType = *code; // first char of type attr
// map to RLMPropertyType
switch (self.objcType) {
case 's': // short
case 'i': // int
case 'l': // long
case 'q': // long long
_type = RLMPropertyTypeInt;
return YES;
case 'f':
_type = RLMPropertyTypeFloat;
return YES;
case 'd':
_type = RLMPropertyTypeDouble;
return YES;
case 'c': // BOOL is stored as char - since rlm has no char type this is ok
case 'B':
_type = RLMPropertyTypeBool;
return YES;
case '@': {
static const char arrayPrefix[] = "@\"RLMArray<";
static const int arrayPrefixLen = sizeof(arrayPrefix) - 1;
if (code[1] == '\0') {
// string is "@"
_type = RLMPropertyTypeAny;
}
else if (strcmp(code, "@\"NSString\"") == 0) {
_type = RLMPropertyTypeString;
}
else if (strcmp(code, "@\"NSDate\"") == 0) {
_type = RLMPropertyTypeDate;
}
else if (strcmp(code, "@\"NSData\"") == 0) {
_type = RLMPropertyTypeData;
}
else if (strncmp(code, arrayPrefix, arrayPrefixLen) == 0) {
// get object class from type string - @"RLMArray<objectClassName>"
_type = RLMPropertyTypeArray;
_objectClassName = [[NSString alloc] initWithBytes:code + arrayPrefixLen
length:strlen(code + arrayPrefixLen) - 2 // drop trailing >"
encoding:NSUTF8StringEncoding];
Class cls = [RLMSchema classForString:_objectClassName];
if (!RLMIsSubclass(cls, RLMObject.class)) {
@throw [NSException exceptionWithName:@"RLMException"
reason:[NSString stringWithFormat:@"'%@' is not supported as an RLMArray object type. RLMArrays can only contain instances of RLMObject subclasses. See http://realm.io/docs/cocoa/#to-many for more information.", self.objectClassName]
userInfo:nil];
}
}
else if (strcmp(code, "@\"NSNumber\"") == 0) {
@throw [NSException exceptionWithName:@"RLMException"
reason:[NSString stringWithFormat:@"'NSNumber' is not supported as an RLMObject property. Supported number types include int, long, float, double, and other primitive number types. See http://realm.io/docs/cocoa/api/Constants/RLMPropertyType.html for all supported types."]
userInfo:nil];
}
else if (strcmp(code, "@\"RLMArray\"") == 0) {
@throw [NSException exceptionWithName:@"RLMException"
reason:@"RLMArray properties require a protocol defining the contained type - example: RLMArray<Person>"
userInfo:nil];
}
else {
// for objects strip the quotes and @
NSString *className = [_objcRawType substringWithRange:NSMakeRange(2, _objcRawType.length-3)];
// verify type
Class cls = [RLMSchema classForString:className];
if (!RLMIsSubclass(cls, RLMObject.class)) {
@throw [NSException exceptionWithName:@"RLMException"
reason:[NSString stringWithFormat:@"'%@' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See http://realm.io/docs/cocoa/api/Classes/RLMObject.html for more information.", self.objectClassName]
userInfo:nil];
}
_type = RLMPropertyTypeObject;
_objectClassName = className;
}
return YES;
}
default:
return NO;
}
}
- (bool)parseObjcProperty:(objc_property_t)property {
unsigned int count;
objc_property_attribute_t *attrs = property_copyAttributeList(property, &count);
bool ignore = false;
for (size_t i = 0; i < count; ++i) {
switch (*attrs[i].name) {
case 'T':
_objcRawType = @(attrs[i].value);
break;
case 'R':
ignore = true;
break;
case 'N':
// nonatomic
break;
case 'D':
// dynamic
break;
case 'G':
_getterName = @(attrs[i].value);
break;
case 'S':
_setterName = @(attrs[i].value);
break;
default:
break;
}
}
free(attrs);
return ignore;
}
- (instancetype)initSwiftPropertyWithName:(NSString *)name
attributes:(RLMPropertyAttributes)attributes
property:(objc_property_t)property
instance:(RLMObject *)obj {
self = [super init];
if (!self) {
return nil;
}
_name = name;
_attributes = attributes;
if ([self parseObjcProperty:property]) {
return nil;
}
// convert array types to objc variant
if ([_objcRawType isEqualToString:@"@\"RLMArray\""]) {
_objcRawType = [NSString stringWithFormat:@"@\"RLMArray<%@>\"", [[obj valueForKey:_name] objectClassName]];
}
if (![self setTypeFromRawType]) {
NSString *reason = [NSString stringWithFormat:@"Can't persist property '%@' with incompatible type. "
"Add to ignoredPropertyNames: method to ignore.", self.name];
@throw [NSException exceptionWithName:@"RLMException" reason:reason userInfo:nil];
}
// convert type for any swift property types (which are parsed as Any)
if (_type == RLMPropertyTypeAny) {
if ([[obj valueForKey:_name] isKindOfClass:[NSString class]]) {
_type = RLMPropertyTypeString;
}
}
// update getter/setter names
[self updateAccessors];
return self;
}
- (instancetype)initWithName:(NSString *)name
attributes:(RLMPropertyAttributes)attributes
property:(objc_property_t)property
{
self = [super init];
if (!self) {
return nil;
}
_name = name;
_attributes = attributes;
if ([self parseObjcProperty:property]) {
return nil;
}
if (![self setTypeFromRawType]) {
NSString *reason = [NSString stringWithFormat:@"Can't persist property '%@' with incompatible type. "
"Add to ignoredPropertyNames: method to ignore.", self.name];
@throw [NSException exceptionWithName:@"RLMException" reason:reason userInfo:nil];
}
// update getter/setter names
[self updateAccessors];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
RLMProperty *prop = [[RLMProperty allocWithZone:zone] init];
prop->_name = _name;
prop->_type = _type;
prop->_objcType = _objcType;
prop->_objectClassName = _objectClassName;
prop->_attributes = _attributes;
prop->_getterName = _getterName;
prop->_setterName = _setterName;
prop->_isPrimary = _isPrimary;
[prop updateAccessors];
return prop;
}
-(BOOL)isEqualToProperty:(RLMProperty *)prop {
return [_name isEqualToString:prop.name] && _type == prop.type && prop.isPrimary == _isPrimary &&
(_objectClassName == nil || [_objectClassName isEqualToString:prop.objectClassName]);
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:RLMProperty.class]) {
return NO;
}
return [self isEqualToProperty:object];
}
@end