RLMSyncUtil_Private.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 NSString *const kRLMSyncAppIDKey;
  41. extern NSString *const kRLMSyncDataKey;
  42. extern NSString *const kRLMSyncErrorJSONKey;
  43. extern NSString *const kRLMSyncErrorStatusCodeKey;
  44. extern NSString *const kRLMSyncIdentityKey;
  45. extern NSString *const kRLMSyncIsAdminKey;
  46. extern NSString *const kRLMSyncNewPasswordKey;
  47. extern NSString *const kRLMSyncPasswordKey;
  48. extern NSString *const kRLMSyncPathKey;
  49. extern NSString *const kRLMSyncTokenKey;
  50. extern NSString *const kRLMSyncProviderKey;
  51. extern NSString *const kRLMSyncProviderIDKey;
  52. extern NSString *const kRLMSyncRegisterKey;
  53. extern NSString *const kRLMSyncUnderlyingErrorKey;
  54. extern NSString *const kRLMSyncUserIDKey;
  55. FOUNDATION_EXTERN uint8_t RLMGetComputedPermissions(RLMRealm *realm, id _Nullable object);
  56. #define RLM_SYNC_UNINITIALIZABLE \
  57. - (instancetype)init __attribute__((unavailable("This type cannot be created directly"))); \
  58. + (instancetype)new __attribute__((unavailable("This type cannot be created directly")));
  59. NS_ASSUME_NONNULL_END
  60. /// A macro to parse a string out of a JSON dictionary, or return nil.
  61. #define RLM_SYNC_PARSE_STRING_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  62. { \
  63. id data = json_macro_val[key_macro_val]; \
  64. if (![data isKindOfClass:[NSString class]]) { return nil; } \
  65. self.prop_macro_val = data; \
  66. } \
  67. #define RLM_SYNC_PARSE_OPTIONAL_STRING(json_macro_val, key_macro_val, prop_macro_val) \
  68. { \
  69. id data = json_macro_val[key_macro_val]; \
  70. if (![data isKindOfClass:[NSString class]]) { data = nil; } \
  71. self.prop_macro_val = data; \
  72. } \
  73. #define RLM_SYNC_PARSE_OPTIONAL_BOOL(json_macro_val, key_macro_val, prop_macro_val) \
  74. { \
  75. id data = json_macro_val[key_macro_val]; \
  76. if (![data isKindOfClass:[NSNumber class]]) { data = @NO; } \
  77. self.prop_macro_val = [data boolValue]; \
  78. } \
  79. /// A macro to parse a double out of a JSON dictionary, or return nil.
  80. #define RLM_SYNC_PARSE_DOUBLE_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  81. { \
  82. id data = json_macro_val[key_macro_val]; \
  83. if (![data isKindOfClass:[NSNumber class]]) { return nil; } \
  84. self.prop_macro_val = [data doubleValue]; \
  85. } \
  86. /// A macro to build a sub-model out of a JSON dictionary, or return nil.
  87. #define RLM_SYNC_PARSE_MODEL_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  88. { \
  89. id raw = json_macro_val[key_macro_val]; \
  90. if (![raw isKindOfClass:[NSDictionary class]]) { return nil; } \
  91. id model = [[class_macro_val alloc] initWithDictionary:raw]; \
  92. if (!model) { return nil; } \
  93. self.prop_macro_val = model; \
  94. } \
  95. /// A macro to build an array of sub-models out of a JSON dictionary, or return nil.
  96. #define RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  97. { \
  98. NSArray *jsonArray = json_macro_val[key_macro_val]; \
  99. if (![jsonArray isKindOfClass:[NSArray class]]) { return nil; } \
  100. NSMutableArray *buffer = [NSMutableArray array]; \
  101. for (id value in jsonArray) { \
  102. id next = nil; \
  103. if ([value isKindOfClass:[NSDictionary class]]) { next = [[class_macro_val alloc] initWithDictionary:value]; } \
  104. if (!next) { return nil; } \
  105. [buffer addObject:next]; \
  106. } \
  107. self.prop_macro_val = [buffer copy]; \
  108. } \
  109. #define RLM_SYNC_PARSE_OPTIONAL_MODEL(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  110. { \
  111. id model; \
  112. id raw = json_macro_val[key_macro_val]; \
  113. if (![raw isKindOfClass:[NSDictionary class]]) { model = nil; } \
  114. else { model = [[class_macro_val alloc] initWithDictionary:raw]; } \
  115. self.prop_macro_val = model; \
  116. } \