GTLRBatchResult.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (c) 2011 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. //#import <GoogleAPIClientForREST/GTLRBatchResult.h>
  16. //
  17. //#import <GoogleAPIClientForREST/GTLRUtilities.h>
  18. #import "GTLRBatchResult.h"
  19. #import "GTLRUtilities.h"
  20. static NSString *const kGTLRBatchResultSuccessesKeys = @"successesKeys";
  21. static NSString *const kGTLRBatchResultSuccessKeyPrefix = @"Success-";
  22. static NSString *const kGTLRBatchResultFailuresKeys = @"failuresKeys";
  23. static NSString *const kGTLRBatchResultFailurKeyPrefix = @"Failure-";
  24. static NSString *const kGTLRBatchResultResponseHeaders = @"responseHeaders";
  25. @implementation GTLRBatchResult
  26. @synthesize successes = _successes,
  27. failures = _failures,
  28. responseHeaders = _responseHeaders;
  29. // Since this class doesn't use the json property, provide the basic NSObject
  30. // methods needed to ensure proper behaviors.
  31. - (id)copyWithZone:(NSZone *)zone {
  32. GTLRBatchResult* newObject = [super copyWithZone:zone];
  33. newObject.successes = [self.successes copyWithZone:zone];
  34. newObject.failures = [self.failures copyWithZone:zone];
  35. newObject.responseHeaders = [self.responseHeaders copyWithZone:zone];
  36. return newObject;
  37. }
  38. - (NSUInteger)hash {
  39. NSUInteger result = [super hash];
  40. result += result * 13 + [self.successes hash];
  41. result += result * 13 + [self.failures hash];
  42. result += result * 13 + [self.responseHeaders hash];
  43. return result;
  44. }
  45. - (BOOL)isEqual:(id)object {
  46. if (self == object) return YES;
  47. if (![super isEqual:object]) {
  48. return NO;
  49. }
  50. if (![object isKindOfClass:[GTLRBatchResult class]]) {
  51. return NO;
  52. }
  53. GTLRBatchResult *other = (GTLRBatchResult *)object;
  54. if (!GTLR_AreEqualOrBothNil(self.successes, other.successes)) {
  55. return NO;
  56. }
  57. if (!GTLR_AreEqualOrBothNil(self.failures, other.failures)) {
  58. return NO;
  59. }
  60. return GTLR_AreEqualOrBothNil(self.responseHeaders, other.responseHeaders);
  61. }
  62. - (NSString *)description {
  63. return [NSString stringWithFormat:@"%@ %p (successes:%lu failures:%lu responseHeaders:%lu)",
  64. [self class], self,
  65. (unsigned long)self.successes.count,
  66. (unsigned long)self.failures.count,
  67. (unsigned long)self.responseHeaders.count];
  68. }
  69. // This class is a subclass of GTLRObject, which declares NSSecureCoding
  70. // conformance. Since this class does't really use the json property, provide
  71. // a custom implementation to maintain the contract.
  72. //
  73. // For success/failures, one could do:
  74. // [encoder encodeObject:self.successes forKey:kGTLRBatchResultSuccesses];
  75. // [encoder encodeObject:self.failures forKey:kGTLRBatchResultFailuresKeys];
  76. // and then use -decodeObjectOfClasses:forKey:, but nothing actually checks the
  77. // structure of the dictionary, so instead the dicts are blown out to provide
  78. // better validation by the encoder/decoder.
  79. + (BOOL)supportsSecureCoding {
  80. return YES;
  81. }
  82. - (instancetype)initWithCoder:(NSCoder *)decoder {
  83. self = [super initWithCoder:decoder];
  84. if (self) {
  85. NSArray<NSString *> *keys =
  86. [decoder decodeObjectOfClass:[NSArray class]
  87. forKey:kGTLRBatchResultSuccessesKeys];
  88. if (keys.count) {
  89. NSMutableDictionary *dict =
  90. [NSMutableDictionary dictionaryWithCapacity:keys.count];
  91. for (NSString *key in keys) {
  92. NSString *storageKey =
  93. [kGTLRBatchResultSuccessKeyPrefix stringByAppendingString:key];
  94. GTLRObject *obj = [decoder decodeObjectOfClass:[GTLRObject class]
  95. forKey:storageKey];
  96. if (obj) {
  97. [dict setObject:obj forKey:key];
  98. }
  99. }
  100. self.successes = dict;
  101. }
  102. keys = [decoder decodeObjectOfClass:[NSArray class]
  103. forKey:kGTLRBatchResultFailuresKeys];
  104. if (keys.count) {
  105. NSMutableDictionary *dict =
  106. [NSMutableDictionary dictionaryWithCapacity:keys.count];
  107. for (NSString *key in keys) {
  108. NSString *storageKey =
  109. [kGTLRBatchResultFailurKeyPrefix stringByAppendingString:key];
  110. GTLRObject *obj = [decoder decodeObjectOfClass:[GTLRObject class]
  111. forKey:storageKey];
  112. if (obj) {
  113. [dict setObject:obj forKey:key];
  114. }
  115. }
  116. self.failures = dict;
  117. }
  118. self.responseHeaders =
  119. [decoder decodeObjectOfClass:[NSDictionary class]
  120. forKey:kGTLRBatchResultResponseHeaders];
  121. }
  122. return self;
  123. }
  124. - (void)encodeWithCoder:(NSCoder *)encoder {
  125. [super encodeWithCoder:encoder];
  126. [encoder encodeObject:self.successes.allKeys
  127. forKey:kGTLRBatchResultSuccessesKeys];
  128. [self.successes enumerateKeysAndObjectsUsingBlock:^(NSString *key,
  129. GTLRObject * obj,
  130. BOOL * stop) {
  131. NSString *storageKey =
  132. [kGTLRBatchResultSuccessKeyPrefix stringByAppendingString:key];
  133. [encoder encodeObject:obj forKey:storageKey];
  134. }];
  135. [encoder encodeObject:self.failures.allKeys forKey:kGTLRBatchResultFailuresKeys];
  136. [self.failures enumerateKeysAndObjectsUsingBlock:^(NSString *key,
  137. GTLRObject * obj,
  138. BOOL * stop) {
  139. NSString *storageKey =
  140. [kGTLRBatchResultFailurKeyPrefix stringByAppendingString:key];
  141. [encoder encodeObject:obj forKey:storageKey];
  142. }];
  143. [encoder encodeObject:self.responseHeaders
  144. forKey:kGTLRBatchResultResponseHeaders];
  145. }
  146. @end