FIRDataSnapshot.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRDataSnapshot.h"
  17. #import "FIRDataSnapshot_Private.h"
  18. #import "FChildrenNode.h"
  19. #import "FValidation.h"
  20. #import "FTransformedEnumerator.h"
  21. #import "FIRDatabaseReference.h"
  22. @interface FIRDataSnapshot ()
  23. @property (nonatomic, strong) FIRDatabaseReference *ref;
  24. @end
  25. @implementation FIRDataSnapshot
  26. - (id)initWithRef:(FIRDatabaseReference *)ref indexedNode:(FIndexedNode *)node
  27. {
  28. self = [super init];
  29. if (self != nil) {
  30. self->_ref = ref;
  31. self->_node = node;
  32. }
  33. return self;
  34. }
  35. - (id) value {
  36. return [self.node.node val];
  37. }
  38. - (id) valueInExportFormat {
  39. return [self.node.node valForExport:YES];
  40. }
  41. - (FIRDataSnapshot *)childSnapshotForPath:(NSString *)childPathString {
  42. [FValidation validateFrom:@"child:" validPathString:childPathString];
  43. FPath* childPath = [[FPath alloc] initWith:childPathString];
  44. FIRDatabaseReference * childRef = [self.ref child:childPathString];
  45. id<FNode> childNode = [self.node.node getChild:childPath];
  46. return [[FIRDataSnapshot alloc] initWithRef:childRef indexedNode:[FIndexedNode indexedNodeWithNode:childNode]];
  47. }
  48. - (BOOL) hasChild:(NSString *)childPathString {
  49. [FValidation validateFrom:@"hasChild:" validPathString:childPathString];
  50. FPath* childPath = [[FPath alloc] initWith:childPathString];
  51. return ! [[self.node.node getChild:childPath] isEmpty];
  52. }
  53. - (id) priority {
  54. id<FNode> priority = [self.node.node getPriority];
  55. return priority.val;
  56. }
  57. - (BOOL) hasChildren {
  58. if([self.node.node isLeafNode]) {
  59. return false;
  60. }
  61. else {
  62. return ![self.node.node isEmpty];
  63. }
  64. }
  65. - (BOOL) exists {
  66. return ![self.node.node isEmpty];
  67. }
  68. - (NSString *) key {
  69. return [self.ref key];
  70. }
  71. - (NSUInteger) childrenCount {
  72. return [self.node.node numChildren];
  73. }
  74. - (NSEnumerator<FIRDataSnapshot *> *) children {
  75. return [[FTransformedEnumerator alloc] initWithEnumerator:self.node.childEnumerator andTransform:^id(FNamedNode *node) {
  76. FIRDatabaseReference *childRef = [self.ref child:node.name];
  77. return [[FIRDataSnapshot alloc] initWithRef:childRef indexedNode:[FIndexedNode indexedNodeWithNode:node.node]];
  78. }];
  79. }
  80. - (NSString *) description {
  81. return [NSString stringWithFormat:@"Snap (%@) %@", self.key, self.node.node];
  82. }
  83. @end