RLMProperty.mm 25 KB

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