RLMSyncUtil_Private.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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 <Realm/RLMSyncUtil.h>
  19. #import <Realm/RLMProperty.h>
  20. #import <Realm/RLMRealmConfiguration.h>
  21. #import <Realm/RLMSyncCredentials.h>
  22. typedef NS_ENUM(NSUInteger, RLMSyncSystemErrorKind) {
  23. // Specific
  24. RLMSyncSystemErrorKindClientReset,
  25. RLMSyncSystemErrorKindPermissionDenied,
  26. // General
  27. RLMSyncSystemErrorKindClient,
  28. RLMSyncSystemErrorKindConnection,
  29. RLMSyncSystemErrorKindSession,
  30. RLMSyncSystemErrorKindUser,
  31. RLMSyncSystemErrorKindUnknown,
  32. };
  33. @class RLMSyncUser;
  34. typedef void(^RLMSyncCompletionBlock)(NSError * _Nullable, NSDictionary * _Nullable);
  35. typedef void(^RLMSyncBasicErrorReportingBlock)(NSError * _Nullable);
  36. typedef NSString* RLMServerPath;
  37. NS_ASSUME_NONNULL_BEGIN
  38. extern RLMIdentityProvider const RLMIdentityProviderAccessToken;
  39. extern RLMIdentityProvider const RLMIdentityProviderRealm;
  40. extern RLMIdentityProvider const RLMIdentityProviderCustomRefreshToken;
  41. extern NSString *const kRLMSyncAppIDKey;
  42. extern NSString *const kRLMSyncDataKey;
  43. extern NSString *const kRLMSyncErrorJSONKey;
  44. extern NSString *const kRLMSyncErrorStatusCodeKey;
  45. extern NSString *const kRLMSyncIdentityKey;
  46. extern NSString *const kRLMSyncIsAdminKey;
  47. extern NSString *const kRLMSyncNewPasswordKey;
  48. extern NSString *const kRLMSyncPasswordKey;
  49. extern NSString *const kRLMSyncPathKey;
  50. extern NSString *const kRLMSyncTokenKey;
  51. extern NSString *const kRLMSyncProviderKey;
  52. extern NSString *const kRLMSyncProviderIDKey;
  53. extern NSString *const kRLMSyncRegisterKey;
  54. extern NSString *const kRLMSyncUnderlyingErrorKey;
  55. extern NSString *const kRLMSyncUserIDKey;
  56. FOUNDATION_EXTERN uint8_t RLMGetComputedPermissions(RLMRealm *realm, id _Nullable object);
  57. #define RLM_SYNC_UNINITIALIZABLE \
  58. - (instancetype)init __attribute__((unavailable("This type cannot be created directly"))); \
  59. + (instancetype)new __attribute__((unavailable("This type cannot be created directly")));
  60. NS_ASSUME_NONNULL_END
  61. /// A macro to parse a string out of a JSON dictionary, or return nil.
  62. #define RLM_SYNC_PARSE_STRING_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  63. { \
  64. id data = json_macro_val[key_macro_val]; \
  65. if (![data isKindOfClass:[NSString class]]) { return nil; } \
  66. self.prop_macro_val = data; \
  67. } \
  68. #define RLM_SYNC_PARSE_OPTIONAL_STRING(json_macro_val, key_macro_val, prop_macro_val) \
  69. { \
  70. id data = json_macro_val[key_macro_val]; \
  71. if (![data isKindOfClass:[NSString class]]) { data = nil; } \
  72. self.prop_macro_val = data; \
  73. } \
  74. #define RLM_SYNC_PARSE_OPTIONAL_BOOL(json_macro_val, key_macro_val, prop_macro_val) \
  75. { \
  76. id data = json_macro_val[key_macro_val]; \
  77. if (![data isKindOfClass:[NSNumber class]]) { data = @NO; } \
  78. self.prop_macro_val = [data boolValue]; \
  79. } \
  80. /// A macro to parse a double out of a JSON dictionary, or return nil.
  81. #define RLM_SYNC_PARSE_DOUBLE_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  82. { \
  83. id data = json_macro_val[key_macro_val]; \
  84. if (![data isKindOfClass:[NSNumber class]]) { return nil; } \
  85. self.prop_macro_val = [data doubleValue]; \
  86. } \
  87. /// A macro to build a sub-model out of a JSON dictionary, or return nil.
  88. #define RLM_SYNC_PARSE_MODEL_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  89. { \
  90. id raw = json_macro_val[key_macro_val]; \
  91. if (![raw isKindOfClass:[NSDictionary class]]) { return nil; } \
  92. id model = [[class_macro_val alloc] initWithDictionary:raw]; \
  93. if (!model) { return nil; } \
  94. self.prop_macro_val = model; \
  95. } \
  96. /// A macro to build an array of sub-models out of a JSON dictionary, or return nil.
  97. #define RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  98. { \
  99. NSArray *jsonArray = json_macro_val[key_macro_val]; \
  100. if (![jsonArray isKindOfClass:[NSArray class]]) { return nil; } \
  101. NSMutableArray *buffer = [NSMutableArray array]; \
  102. for (id value in jsonArray) { \
  103. id next = nil; \
  104. if ([value isKindOfClass:[NSDictionary class]]) { next = [[class_macro_val alloc] initWithDictionary:value]; } \
  105. if (!next) { return nil; } \
  106. [buffer addObject:next]; \
  107. } \
  108. self.prop_macro_val = [buffer copy]; \
  109. } \
  110. #define RLM_SYNC_PARSE_OPTIONAL_MODEL(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  111. { \
  112. id model; \
  113. id raw = json_macro_val[key_macro_val]; \
  114. if (![raw isKindOfClass:[NSDictionary class]]) { model = nil; } \
  115. else { model = [[class_macro_val alloc] initWithDictionary:raw]; } \
  116. self.prop_macro_val = model; \
  117. } \