GPBWellKnownTypes.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Importing sources here to force the linker to include our category methods in
  31. // the static library. If these were compiled separately, the category methods
  32. // below would be stripped by the linker.
  33. #import "google/protobuf/Timestamp.pbobjc.m"
  34. #import "google/protobuf/Duration.pbobjc.m"
  35. #import "GPBWellKnownTypes.h"
  36. static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds,
  37. int32_t nanos) {
  38. return seconds + (NSTimeInterval)nanos / 1e9;
  39. }
  40. static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time,
  41. int64_t *outSeconds) {
  42. NSTimeInterval seconds;
  43. NSTimeInterval nanos = modf(time, &seconds);
  44. nanos *= 1e9;
  45. *outSeconds = (int64_t)seconds;
  46. return (int32_t)nanos;
  47. }
  48. @implementation GPBTimestamp (GBPWellKnownTypes)
  49. - (instancetype)initWithDate:(NSDate *)date {
  50. return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970];
  51. }
  52. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  53. if ((self = [super init])) {
  54. int64_t seconds;
  55. int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970(
  56. timeIntervalSince1970, &seconds);
  57. self.seconds = seconds;
  58. self.nanos = nanos;
  59. }
  60. return self;
  61. }
  62. - (NSDate *)date {
  63. return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970];
  64. }
  65. - (void)setDate:(NSDate *)date {
  66. self.timeIntervalSince1970 = date.timeIntervalSince1970;
  67. }
  68. - (NSTimeInterval)timeIntervalSince1970 {
  69. return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos);
  70. }
  71. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  72. int64_t seconds;
  73. int32_t nanos =
  74. SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds);
  75. self.seconds = seconds;
  76. self.nanos = nanos;
  77. }
  78. @end
  79. @implementation GPBDuration (GBPWellKnownTypes)
  80. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  81. if ((self = [super init])) {
  82. int64_t seconds;
  83. int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970(
  84. timeIntervalSince1970, &seconds);
  85. self.seconds = seconds;
  86. self.nanos = nanos;
  87. }
  88. return self;
  89. }
  90. - (NSTimeInterval)timeIntervalSince1970 {
  91. return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos);
  92. }
  93. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  94. int64_t seconds;
  95. int32_t nanos =
  96. SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds);
  97. self.seconds = seconds;
  98. self.nanos = nanos;
  99. }
  100. @end
  101. NSString *const GPBTypeGoogleApisComPrefix = @"type.googleapis.com/";
  102. @implementation GPBAny (GBPWellKnownTypes)
  103. - (instancetype)initWithMessage:(GPBMessage*)message {
  104. self = [super init];
  105. if (self) {
  106. [self setMessage:message];
  107. }
  108. return self;
  109. }
  110. - (NSString*)typeName {
  111. NSAssert([self.typeURL hasPrefix:GPBTypeGoogleApisComPrefix],
  112. @"Invalid any type url (%@).", self.typeURL);
  113. if (![self.typeURL hasPrefix:GPBTypeGoogleApisComPrefix]) {
  114. return nil;
  115. }
  116. return [self.typeURL substringFromIndex:[GPBTypeGoogleApisComPrefix length]];
  117. }
  118. - (void)setMessage:(GPBMessage*)message {
  119. self.typeURL = [GPBTypeGoogleApisComPrefix stringByAppendingString:message.descriptor.name];
  120. self.value = message.data;
  121. }
  122. - (GPBMessage*)messageOfClass:(Class)messageClass {
  123. if ([self wrapsMessageOfClass:messageClass]) {
  124. GPBMessage* message = [messageClass message];
  125. [message mergeFromData:self.value extensionRegistry:nil];
  126. return message;
  127. } else {
  128. return nil;
  129. }
  130. }
  131. - (BOOL)wrapsMessageOfClass:(Class)messageClass {
  132. NSAssert([messageClass isSubclassofClass:[GPBMessage class]],
  133. @"Given class (%@) is not a subclass of GPBMessage",
  134. [messageClass name]);
  135. if (![messageClass isSubclassOfClass:[GPBMessage class]]) {
  136. return NO;
  137. }
  138. return [[self typeName] isEqualToString:messageClass.descriptor.name];
  139. }
  140. @end