RLMProperty.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMProperty_Private.hpp"
  19. #import "RLMArray_Private.hpp"
  20. #import "RLMListBase.h"
  21. #import "RLMObject.h"
  22. #import "RLMObjectSchema_Private.hpp"
  23. #import "RLMObject_Private.h"
  24. #import "RLMSchema_Private.h"
  25. #import "RLMSwiftSupport.h"
  26. #import "RLMUtil.hpp"
  27. #import "property.hpp"
  28. static_assert((int)RLMPropertyTypeInt == (int)realm::PropertyType::Int, "");
  29. static_assert((int)RLMPropertyTypeBool == (int)realm::PropertyType::Bool, "");
  30. static_assert((int)RLMPropertyTypeFloat == (int)realm::PropertyType::Float, "");
  31. static_assert((int)RLMPropertyTypeDouble == (int)realm::PropertyType::Double, "");
  32. static_assert((int)RLMPropertyTypeString == (int)realm::PropertyType::String, "");
  33. static_assert((int)RLMPropertyTypeData == (int)realm::PropertyType::Data, "");
  34. static_assert((int)RLMPropertyTypeDate == (int)realm::PropertyType::Date, "");
  35. static_assert((int)RLMPropertyTypeObject == (int)realm::PropertyType::Object, "");
  36. BOOL RLMPropertyTypeIsComputed(RLMPropertyType propertyType) {
  37. return propertyType == RLMPropertyTypeLinkingObjects;
  38. }
  39. // Swift obeys the ARC naming conventions for method families (except for init)
  40. // but the end result doesn't really work (using KVC on a method returning a
  41. // retained value results in a leak, but not returning a retained value results
  42. // in crashes). Objective-C makes properties with naming fitting the method
  43. // families a compile error, so we just disallow them in Swift as well.
  44. // http://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-method-families
  45. void RLMValidateSwiftPropertyName(NSString *name) {
  46. // To belong to a method family, the property name must begin with the family
  47. // name followed by a non-lowercase letter (or nothing), with an optional
  48. // leading underscore
  49. const char *str = name.UTF8String;
  50. if (str[0] == '_')
  51. ++str;
  52. auto nameSize = strlen(str);
  53. // Note that "init" is deliberately not in this list because Swift does not
  54. // infer family membership for it.
  55. for (auto family : {"alloc", "new", "copy", "mutableCopy"}) {
  56. auto familySize = strlen(family);
  57. if (nameSize < familySize || !std::equal(str, str + familySize, family)) {
  58. continue;
  59. }
  60. if (familySize == nameSize || !islower(str[familySize])) {
  61. @throw RLMException(@"Property names beginning with '%s' are not "
  62. "supported. Swift follows ARC's ownership "
  63. "rules for methods based on their name, which "
  64. "results in memory leaks when accessing "
  65. "properties which return retained values via KVC.",
  66. family);
  67. }
  68. return;
  69. }
  70. }
  71. static bool rawTypeShouldBeTreatedAsComputedProperty(NSString *rawType) {
  72. return [rawType isEqualToString:@"@\"RLMLinkingObjects\""] || [rawType hasPrefix:@"@\"RLMLinkingObjects<"];
  73. }
  74. @implementation RLMProperty
  75. + (instancetype)propertyForObjectStoreProperty:(const realm::Property &)prop {
  76. auto ret = [[RLMProperty alloc] initWithName:@(prop.name.c_str())
  77. type:static_cast<RLMPropertyType>(prop.type & ~realm::PropertyType::Flags)
  78. objectClassName:prop.object_type.length() ? @(prop.object_type.c_str()) : nil
  79. linkOriginPropertyName:prop.link_origin_property_name.length() ? @(prop.link_origin_property_name.c_str()) : nil
  80. indexed:prop.is_indexed
  81. optional:is_nullable(prop.type)];
  82. if (is_array(prop.type)) {
  83. ret->_array = true;
  84. }
  85. return ret;
  86. }
  87. - (instancetype)initWithName:(NSString *)name
  88. type:(RLMPropertyType)type
  89. objectClassName:(NSString *)objectClassName
  90. linkOriginPropertyName:(NSString *)linkOriginPropertyName
  91. indexed:(BOOL)indexed
  92. optional:(BOOL)optional {
  93. self = [super init];
  94. if (self) {
  95. _name = name;
  96. _type = type;
  97. _objectClassName = objectClassName;
  98. _linkOriginPropertyName = linkOriginPropertyName;
  99. _indexed = indexed;
  100. _optional = optional;
  101. [self updateAccessors];
  102. }
  103. return self;
  104. }
  105. - (void)setName:(NSString *)name {
  106. _name = name;
  107. [self updateAccessors];
  108. }
  109. - (void)updateAccessors {
  110. // populate getter/setter names if generic
  111. if (!_getterName) {
  112. _getterName = _name;
  113. }
  114. if (!_setterName) {
  115. // Objective-C setters only capitalize the first letter of the property name if it falls between 'a' and 'z'
  116. int asciiCode = [_name characterAtIndex:0];
  117. BOOL shouldUppercase = asciiCode >= 'a' && asciiCode <= 'z';
  118. NSString *firstChar = [_name substringToIndex:1];
  119. firstChar = shouldUppercase ? firstChar.uppercaseString : firstChar;
  120. _setterName = [NSString stringWithFormat:@"set%@%@:", firstChar, [_name substringFromIndex:1]];
  121. }
  122. _getterSel = NSSelectorFromString(_getterName);
  123. _setterSel = NSSelectorFromString(_setterName);
  124. }
  125. static realm::util::Optional<RLMPropertyType> typeFromProtocolString(const char *type) {
  126. if (strncmp(type, "RLM", 3)) {
  127. return realm::none;
  128. }
  129. type += 3;
  130. if (strcmp(type, "Int>\"") == 0) {
  131. return RLMPropertyTypeInt;
  132. }
  133. if (strcmp(type, "Float>\"") == 0) {
  134. return RLMPropertyTypeFloat;
  135. }
  136. if (strcmp(type, "Double>\"") == 0) {
  137. return RLMPropertyTypeDouble;
  138. }
  139. if (strcmp(type, "Bool>\"") == 0) {
  140. return RLMPropertyTypeBool;
  141. }
  142. if (strcmp(type, "String>\"") == 0) {
  143. return RLMPropertyTypeString;
  144. }
  145. if (strcmp(type, "Data>\"") == 0) {
  146. return RLMPropertyTypeData;
  147. }
  148. if (strcmp(type, "Date>\"") == 0) {
  149. return RLMPropertyTypeDate;
  150. }
  151. return realm::none;
  152. }
  153. // determine RLMPropertyType from objc code - returns true if valid type was found/set
  154. - (BOOL)setTypeFromRawType:(NSString *)rawType {
  155. const char *code = rawType.UTF8String;
  156. switch (*code) {
  157. case 's': // short
  158. case 'i': // int
  159. case 'l': // long
  160. case 'q': // long long
  161. _type = RLMPropertyTypeInt;
  162. return YES;
  163. case 'f':
  164. _type = RLMPropertyTypeFloat;
  165. return YES;
  166. case 'd':
  167. _type = RLMPropertyTypeDouble;
  168. return YES;
  169. case 'c': // BOOL is stored as char - since rlm has no char type this is ok
  170. case 'B':
  171. _type = RLMPropertyTypeBool;
  172. return YES;
  173. case '@':
  174. break;
  175. default:
  176. return NO;
  177. }
  178. _optional = true;
  179. static const char arrayPrefix[] = "@\"RLMArray<";
  180. static const int arrayPrefixLen = sizeof(arrayPrefix) - 1;
  181. static const char numberPrefix[] = "@\"NSNumber<";
  182. static const int numberPrefixLen = sizeof(numberPrefix) - 1;
  183. static const char linkingObjectsPrefix[] = "@\"RLMLinkingObjects";
  184. static const int linkingObjectsPrefixLen = sizeof(linkingObjectsPrefix) - 1;
  185. if (strcmp(code, "@\"NSString\"") == 0) {
  186. _type = RLMPropertyTypeString;
  187. }
  188. else if (strcmp(code, "@\"NSDate\"") == 0) {
  189. _type = RLMPropertyTypeDate;
  190. }
  191. else if (strcmp(code, "@\"NSData\"") == 0) {
  192. _type = RLMPropertyTypeData;
  193. }
  194. else if (strncmp(code, arrayPrefix, arrayPrefixLen) == 0) {
  195. _array = true;
  196. if (auto type = typeFromProtocolString(code + arrayPrefixLen)) {
  197. _type = *type;
  198. return YES;
  199. }
  200. // get object class from type string - @"RLMArray<objectClassName>"
  201. _objectClassName = [[NSString alloc] initWithBytes:code + arrayPrefixLen
  202. length:strlen(code + arrayPrefixLen) - 2 // drop trailing >"
  203. encoding:NSUTF8StringEncoding];
  204. if ([RLMSchema classForString:_objectClassName]) {
  205. _optional = false;
  206. _type = RLMPropertyTypeObject;
  207. return YES;
  208. }
  209. @throw RLMException(@"Property '%@' is of type 'RLMArray<%@>' which is not a supported RLMArray object type. "
  210. @"RLMArrays can only contain instances of RLMObject subclasses. "
  211. @"See https://realm.io/docs/objc/latest/#to-many for more information.", _name, _objectClassName);
  212. }
  213. else if (strncmp(code, numberPrefix, numberPrefixLen) == 0) {
  214. auto type = typeFromProtocolString(code + numberPrefixLen);
  215. if (type && (*type == RLMPropertyTypeInt || *type == RLMPropertyTypeFloat || *type == RLMPropertyTypeDouble || *type == RLMPropertyTypeBool)) {
  216. _type = *type;
  217. return YES;
  218. }
  219. @throw RLMException(@"Property '%@' is of type %s which is not a supported NSNumber object type. "
  220. @"NSNumbers can only be RLMInt, RLMFloat, RLMDouble, and RLMBool at the moment. "
  221. @"See https://realm.io/docs/objc/latest for more information.", _name, code + 1);
  222. }
  223. else if (strncmp(code, linkingObjectsPrefix, linkingObjectsPrefixLen) == 0 &&
  224. (code[linkingObjectsPrefixLen] == '"' || code[linkingObjectsPrefixLen] == '<')) {
  225. _type = RLMPropertyTypeLinkingObjects;
  226. _optional = false;
  227. _array = true;
  228. if (!_objectClassName || !_linkOriginPropertyName) {
  229. @throw RLMException(@"Property '%@' is of type RLMLinkingObjects but +linkingObjectsProperties did not specify the class "
  230. "or property that is the origin of the link.", _name);
  231. }
  232. // If the property was declared with a protocol indicating the contained type, validate that it matches
  233. // the class from the dictionary returned by +linkingObjectsProperties.
  234. if (code[linkingObjectsPrefixLen] == '<') {
  235. NSString *classNameFromProtocol = [[NSString alloc] initWithBytes:code + linkingObjectsPrefixLen + 1
  236. length:strlen(code + linkingObjectsPrefixLen) - 3 // drop trailing >"
  237. encoding:NSUTF8StringEncoding];
  238. if (![_objectClassName isEqualToString:classNameFromProtocol]) {
  239. @throw RLMException(@"Property '%@' was declared with type RLMLinkingObjects<%@>, but a conflicting "
  240. "class name of '%@' was returned by +linkingObjectsProperties.", _name,
  241. classNameFromProtocol, _objectClassName);
  242. }
  243. }
  244. }
  245. else if (strcmp(code, "@\"NSNumber\"") == 0) {
  246. @throw RLMException(@"Property '%@' requires a protocol defining the contained type - example: NSNumber<RLMInt>.", _name);
  247. }
  248. else if (strcmp(code, "@\"RLMArray\"") == 0) {
  249. @throw RLMException(@"Property '%@' requires a protocol defining the contained type - example: RLMArray<Person>.", _name);
  250. }
  251. else {
  252. NSString *className;
  253. Class cls = nil;
  254. if (code[1] == '\0') {
  255. className = @"id";
  256. }
  257. else {
  258. // for objects strip the quotes and @
  259. className = [rawType substringWithRange:NSMakeRange(2, rawType.length-3)];
  260. cls = [RLMSchema classForString:className];
  261. }
  262. if (!cls) {
  263. @throw RLMException(@"Property '%@' is declared as '%@', which is not a supported RLMObject property type. "
  264. @"All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject. "
  265. @"See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.", _name, className);
  266. }
  267. _type = RLMPropertyTypeObject;
  268. _optional = true;
  269. _objectClassName = [cls className] ?: className;
  270. }
  271. return YES;
  272. }
  273. - (void)parseObjcProperty:(objc_property_t)property
  274. readOnly:(bool *)readOnly
  275. computed:(bool *)computed
  276. rawType:(NSString **)rawType {
  277. unsigned int count;
  278. objc_property_attribute_t *attrs = property_copyAttributeList(property, &count);
  279. *computed = true;
  280. for (size_t i = 0; i < count; ++i) {
  281. switch (*attrs[i].name) {
  282. case 'T':
  283. *rawType = @(attrs[i].value);
  284. break;
  285. case 'R':
  286. *readOnly = true;
  287. break;
  288. case 'N':
  289. // nonatomic
  290. break;
  291. case 'D':
  292. // dynamic
  293. break;
  294. case 'G':
  295. _getterName = @(attrs[i].value);
  296. break;
  297. case 'S':
  298. _setterName = @(attrs[i].value);
  299. break;
  300. case 'V': // backing ivar name
  301. *computed = false;
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. free(attrs);
  308. }
  309. - (instancetype)initSwiftPropertyWithName:(NSString *)name
  310. indexed:(BOOL)indexed
  311. linkPropertyDescriptor:(RLMPropertyDescriptor *)linkPropertyDescriptor
  312. property:(objc_property_t)property
  313. instance:(RLMObject *)obj {
  314. self = [super init];
  315. if (!self) {
  316. return nil;
  317. }
  318. RLMValidateSwiftPropertyName(name);
  319. _name = name;
  320. _indexed = indexed;
  321. if (linkPropertyDescriptor) {
  322. _objectClassName = [linkPropertyDescriptor.objectClass className];
  323. _linkOriginPropertyName = linkPropertyDescriptor.propertyName;
  324. }
  325. NSString *rawType;
  326. bool readOnly = false;
  327. bool isComputed = false;
  328. [self parseObjcProperty:property readOnly:&readOnly computed:&isComputed rawType:&rawType];
  329. if (!readOnly && isComputed) {
  330. // Check for lazy property.
  331. NSString *backingPropertyName = [NSString stringWithFormat:@"%@.storage", name];
  332. if (class_getInstanceVariable([obj class], backingPropertyName.UTF8String)) {
  333. isComputed = false;
  334. }
  335. }
  336. if (readOnly || isComputed) {
  337. return nil;
  338. }
  339. id propertyValue = [obj valueForKey:_name];
  340. // FIXME: temporarily workaround added since Objective-C generics used in Swift show up as `@`
  341. // * broken starting in Swift 3.0 Xcode 8 b1
  342. // * tested to still be broken in Swift 3.0 Xcode 8 b6
  343. // * if the Realm Objective-C Swift tests pass with this removed, it's been fixed
  344. // * once it has been fixed, remove this entire conditional block (contents included) entirely
  345. // * Bug Report: SR-2031 https://bugs.swift.org/browse/SR-2031
  346. if ([rawType isEqualToString:@"@"]) {
  347. if (propertyValue) {
  348. rawType = [NSString stringWithFormat:@"@\"%@\"", [propertyValue class]];
  349. } else if (linkPropertyDescriptor) {
  350. // we're going to naively assume that the user used the correct type since we can't check it
  351. rawType = @"@\"RLMLinkingObjects\"";
  352. }
  353. }
  354. // convert array types to objc variant
  355. if ([rawType isEqualToString:@"@\"RLMArray\""]) {
  356. RLMArray *value = propertyValue;
  357. _type = value.type;
  358. _optional = value.optional;
  359. _array = true;
  360. _objectClassName = value.objectClassName;
  361. if (_type == RLMPropertyTypeObject && ![RLMSchema classForString:_objectClassName]) {
  362. @throw RLMException(@"Property '%@' is of type 'RLMArray<%@>' which is not a supported RLMArray object type. "
  363. @"RLMArrays can only contain instances of RLMObject subclasses. "
  364. @"See https://realm.io/docs/objc/latest/#to-many for more information.", _name, _objectClassName);
  365. }
  366. }
  367. else if ([rawType isEqualToString:@"@\"NSNumber\""]) {
  368. const char *numberType = [propertyValue objCType];
  369. if (!numberType) {
  370. @throw RLMException(@"Can't persist NSNumber without default value: use a Swift-native number type or provide a default value.");
  371. }
  372. _optional = true;
  373. switch (*numberType) {
  374. case 'i': case 'l': case 'q':
  375. _type = RLMPropertyTypeInt;
  376. break;
  377. case 'f':
  378. _type = RLMPropertyTypeFloat;
  379. break;
  380. case 'd':
  381. _type = RLMPropertyTypeDouble;
  382. break;
  383. case 'B': case 'c':
  384. _type = RLMPropertyTypeBool;
  385. break;
  386. default:
  387. @throw RLMException(@"Can't persist NSNumber of type '%s': only integers, floats, doubles, and bools are currently supported.", numberType);
  388. }
  389. }
  390. else if (![self setTypeFromRawType:rawType]) {
  391. @throw RLMException(@"Can't persist property '%@' with incompatible type. "
  392. "Add to Object.ignoredProperties() class method to ignore.",
  393. self.name);
  394. }
  395. if ([rawType isEqualToString:@"c"]) {
  396. // Check if it's a BOOL or Int8 by trying to set it to 2 and seeing if
  397. // it actually sets it to 1.
  398. [obj setValue:@2 forKey:name];
  399. NSNumber *value = [obj valueForKey:name];
  400. _type = value.intValue == 2 ? RLMPropertyTypeInt : RLMPropertyTypeBool;
  401. }
  402. // update getter/setter names
  403. [self updateAccessors];
  404. return self;
  405. }
  406. - (instancetype)initWithName:(NSString *)name
  407. indexed:(BOOL)indexed
  408. linkPropertyDescriptor:(RLMPropertyDescriptor *)linkPropertyDescriptor
  409. property:(objc_property_t)property
  410. {
  411. self = [super init];
  412. if (!self) {
  413. return nil;
  414. }
  415. _name = name;
  416. _indexed = indexed;
  417. if (linkPropertyDescriptor) {
  418. _objectClassName = [linkPropertyDescriptor.objectClass className];
  419. _linkOriginPropertyName = linkPropertyDescriptor.propertyName;
  420. }
  421. NSString *rawType;
  422. bool isReadOnly = false;
  423. bool isComputed = false;
  424. [self parseObjcProperty:property readOnly:&isReadOnly computed:&isComputed rawType:&rawType];
  425. bool shouldBeTreatedAsComputedProperty = rawTypeShouldBeTreatedAsComputedProperty(rawType);
  426. if ((isReadOnly || isComputed) && !shouldBeTreatedAsComputedProperty) {
  427. return nil;
  428. }
  429. if (![self setTypeFromRawType:rawType]) {
  430. @throw RLMException(@"Can't persist property '%@' with incompatible type. "
  431. "Add to ignoredPropertyNames: method to ignore.", self.name);
  432. }
  433. if (!isReadOnly && shouldBeTreatedAsComputedProperty) {
  434. @throw RLMException(@"Property '%@' must be declared as readonly as %@ properties cannot be written to.",
  435. self.name, RLMTypeToString(_type));
  436. }
  437. // update getter/setter names
  438. [self updateAccessors];
  439. return self;
  440. }
  441. - (instancetype)initSwiftListPropertyWithName:(NSString *)name
  442. instance:(id)object {
  443. self = [super init];
  444. if (!self) {
  445. return nil;
  446. }
  447. _name = name;
  448. _array = true;
  449. _swiftIvar = class_getInstanceVariable([object class], name.UTF8String);
  450. RLMArray *array = [object_getIvar(object, _swiftIvar) _rlmArray];
  451. _type = array.type;
  452. _optional = array.optional;
  453. _objectClassName = array.objectClassName;
  454. // no obj-c property for generic lists, and thus no getter/setter names
  455. return self;
  456. }
  457. - (instancetype)initSwiftOptionalPropertyWithName:(NSString *)name
  458. indexed:(BOOL)indexed
  459. ivar:(Ivar)ivar
  460. propertyType:(RLMPropertyType)propertyType {
  461. self = [super init];
  462. if (!self) {
  463. return nil;
  464. }
  465. _name = name;
  466. _type = propertyType;
  467. _indexed = indexed;
  468. _swiftIvar = ivar;
  469. _optional = true;
  470. // no obj-c property for generic optionals, and thus no getter/setter names
  471. return self;
  472. }
  473. - (instancetype)initSwiftLinkingObjectsPropertyWithName:(NSString *)name
  474. ivar:(Ivar)ivar
  475. objectClassName:(NSString *)objectClassName
  476. linkOriginPropertyName:(NSString *)linkOriginPropertyName {
  477. self = [super init];
  478. if (!self) {
  479. return nil;
  480. }
  481. _name = name;
  482. _type = RLMPropertyTypeLinkingObjects;
  483. _array = true;
  484. _objectClassName = objectClassName;
  485. _linkOriginPropertyName = linkOriginPropertyName;
  486. _swiftIvar = ivar;
  487. // no obj-c property for generic linking objects properties, and thus no getter/setter names
  488. return self;
  489. }
  490. - (id)copyWithZone:(NSZone *)zone {
  491. RLMProperty *prop = [[RLMProperty allocWithZone:zone] init];
  492. prop->_name = _name;
  493. prop->_columnName = _columnName;
  494. prop->_type = _type;
  495. prop->_objectClassName = _objectClassName;
  496. prop->_array = _array;
  497. prop->_indexed = _indexed;
  498. prop->_getterName = _getterName;
  499. prop->_setterName = _setterName;
  500. prop->_getterSel = _getterSel;
  501. prop->_setterSel = _setterSel;
  502. prop->_isPrimary = _isPrimary;
  503. prop->_swiftIvar = _swiftIvar;
  504. prop->_optional = _optional;
  505. prop->_linkOriginPropertyName = _linkOriginPropertyName;
  506. return prop;
  507. }
  508. - (RLMProperty *)copyWithNewName:(NSString *)name {
  509. RLMProperty *prop = [self copy];
  510. prop.name = name;
  511. return prop;
  512. }
  513. - (BOOL)isEqual:(id)object {
  514. if (![object isKindOfClass:[RLMProperty class]]) {
  515. return NO;
  516. }
  517. return [self isEqualToProperty:object];
  518. }
  519. - (BOOL)isEqualToProperty:(RLMProperty *)property {
  520. return _type == property->_type
  521. && _indexed == property->_indexed
  522. && _isPrimary == property->_isPrimary
  523. && _optional == property->_optional
  524. && [_name isEqualToString:property->_name]
  525. && (_objectClassName == property->_objectClassName || [_objectClassName isEqualToString:property->_objectClassName])
  526. && (_linkOriginPropertyName == property->_linkOriginPropertyName ||
  527. [_linkOriginPropertyName isEqualToString:property->_linkOriginPropertyName]);
  528. }
  529. - (NSString *)description {
  530. return [NSString stringWithFormat:
  531. @"%@ {\n"
  532. "\ttype = %@;\n"
  533. "\tobjectClassName = %@;\n"
  534. "\tlinkOriginPropertyName = %@;\n"
  535. "\tindexed = %@;\n"
  536. "\tisPrimary = %@;\n"
  537. "\tarray = %@;\n"
  538. "\toptional = %@;\n"
  539. "}",
  540. self.name, RLMTypeToString(self.type), self.objectClassName,
  541. self.linkOriginPropertyName,
  542. self.indexed ? @"YES" : @"NO",
  543. self.isPrimary ? @"YES" : @"NO",
  544. self.array ? @"YES" : @"NO",
  545. self.optional ? @"YES" : @"NO"];
  546. }
  547. - (NSString *)columnName {
  548. return _columnName ?: _name;
  549. }
  550. - (realm::Property)objectStoreCopy:(RLMSchema *)schema {
  551. realm::Property p;
  552. p.name = self.columnName.UTF8String;
  553. if (_objectClassName) {
  554. RLMObjectSchema *targetSchema = schema[_objectClassName];
  555. p.object_type = (targetSchema.objectName ?: _objectClassName).UTF8String;
  556. if (_linkOriginPropertyName) {
  557. p.link_origin_property_name = (targetSchema[_linkOriginPropertyName].columnName ?: _linkOriginPropertyName).UTF8String;
  558. }
  559. }
  560. p.is_indexed = static_cast<bool>(_indexed);
  561. p.type = static_cast<realm::PropertyType>(_type);
  562. if (_array) {
  563. p.type |= realm::PropertyType::Array;
  564. }
  565. if (_optional) {
  566. p.type |= realm::PropertyType::Nullable;
  567. }
  568. return p;
  569. }
  570. @end
  571. @implementation RLMPropertyDescriptor
  572. + (instancetype)descriptorWithClass:(Class)objectClass propertyName:(NSString *)propertyName
  573. {
  574. RLMPropertyDescriptor *descriptor = [[RLMPropertyDescriptor alloc] init];
  575. descriptor->_objectClass = objectClass;
  576. descriptor->_propertyName = propertyName;
  577. return descriptor;
  578. }
  579. @end