RLMSyncUtil_Private.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #define RLM_SYNC_UNINITIALIZABLE \
  56. - (instancetype)init __attribute__((unavailable("This type cannot be created directly"))); \
  57. + (instancetype)new __attribute__((unavailable("This type cannot be created directly")));
  58. NS_ASSUME_NONNULL_END
  59. /// A macro to parse a string out of a JSON dictionary, or return nil.
  60. #define RLM_SYNC_PARSE_STRING_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  61. { \
  62. id data = json_macro_val[key_macro_val]; \
  63. if (![data isKindOfClass:[NSString class]]) { return nil; } \
  64. self.prop_macro_val = data; \
  65. } \
  66. #define RLM_SYNC_PARSE_OPTIONAL_STRING(json_macro_val, key_macro_val, prop_macro_val) \
  67. { \
  68. id data = json_macro_val[key_macro_val]; \
  69. if (![data isKindOfClass:[NSString class]]) { data = nil; } \
  70. self.prop_macro_val = data; \
  71. } \
  72. #define RLM_SYNC_PARSE_OPTIONAL_BOOL(json_macro_val, key_macro_val, prop_macro_val) \
  73. { \
  74. id data = json_macro_val[key_macro_val]; \
  75. if (![data isKindOfClass:[NSNumber class]]) { data = @NO; } \
  76. self.prop_macro_val = [data boolValue]; \
  77. } \
  78. /// A macro to parse a double out of a JSON dictionary, or return nil.
  79. #define RLM_SYNC_PARSE_DOUBLE_OR_ABORT(json_macro_val, key_macro_val, prop_macro_val) \
  80. { \
  81. id data = json_macro_val[key_macro_val]; \
  82. if (![data isKindOfClass:[NSNumber class]]) { return nil; } \
  83. self.prop_macro_val = [data doubleValue]; \
  84. } \
  85. /// A macro to build a sub-model out of a JSON dictionary, or return nil.
  86. #define RLM_SYNC_PARSE_MODEL_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  87. { \
  88. id raw = json_macro_val[key_macro_val]; \
  89. if (![raw isKindOfClass:[NSDictionary class]]) { return nil; } \
  90. id model = [[class_macro_val alloc] initWithDictionary:raw]; \
  91. if (!model) { return nil; } \
  92. self.prop_macro_val = model; \
  93. } \
  94. /// A macro to build an array of sub-models out of a JSON dictionary, or return nil.
  95. #define RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  96. { \
  97. NSArray *jsonArray = json_macro_val[key_macro_val]; \
  98. if (![jsonArray isKindOfClass:[NSArray class]]) { return nil; } \
  99. NSMutableArray *buffer = [NSMutableArray array]; \
  100. for (id value in jsonArray) { \
  101. id next = nil; \
  102. if ([value isKindOfClass:[NSDictionary class]]) { next = [[class_macro_val alloc] initWithDictionary:value]; } \
  103. if (!next) { return nil; } \
  104. [buffer addObject:next]; \
  105. } \
  106. self.prop_macro_val = [buffer copy]; \
  107. } \
  108. #define RLM_SYNC_PARSE_OPTIONAL_MODEL(json_macro_val, key_macro_val, class_macro_val, prop_macro_val) \
  109. { \
  110. id model; \
  111. id raw = json_macro_val[key_macro_val]; \
  112. if (![raw isKindOfClass:[NSDictionary class]]) { model = nil; } \
  113. else { model = [[class_macro_val alloc] initWithDictionary:raw]; } \
  114. self.prop_macro_val = model; \
  115. } \