FIRDataSnapshot.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDataSnapshot.h"
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRDataSnapshot_Private.h"
  18. #import "FirebaseDatabase/Sources/FTransformedEnumerator.h"
  19. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabaseReference.h"
  20. #import "FirebaseDatabase/Sources/Snapshot/FChildrenNode.h"
  21. #import "FirebaseDatabase/Sources/Utilities/FValidation.h"
  22. @interface FIRDataSnapshot ()
  23. @property(nonatomic, strong) FIRDatabaseReference *ref;
  24. @end
  25. @implementation FIRDataSnapshot
  26. - (id)initWithRef:(FIRDatabaseReference *)ref indexedNode:(FIndexedNode *)node {
  27. self = [super init];
  28. if (self != nil) {
  29. self->_ref = ref;
  30. self->_node = node;
  31. }
  32. return self;
  33. }
  34. - (id)value {
  35. return [self.node.node val];
  36. }
  37. - (id)valueInExportFormat {
  38. return [self.node.node valForExport:YES];
  39. }
  40. - (FIRDataSnapshot *)childSnapshotForPath:(NSString *)childPathString {
  41. [FValidation validateFrom:@"child:" validPathString:childPathString];
  42. FPath *childPath = [[FPath alloc] initWith:childPathString];
  43. FIRDatabaseReference *childRef = [self.ref child:childPathString];
  44. id<FNode> childNode = [self.node.node getChild:childPath];
  45. return [[FIRDataSnapshot alloc]
  46. initWithRef:childRef
  47. indexedNode:[FIndexedNode indexedNodeWithNode:childNode]];
  48. }
  49. - (BOOL)hasChild:(NSString *)childPathString {
  50. [FValidation validateFrom:@"hasChild:" validPathString:childPathString];
  51. FPath *childPath = [[FPath alloc] initWith:childPathString];
  52. return ![[self.node.node getChild:childPath] isEmpty];
  53. }
  54. - (id)priority {
  55. id<FNode> priority = [self.node.node getPriority];
  56. return priority.val;
  57. }
  58. - (BOOL)hasChildren {
  59. if ([self.node.node isLeafNode]) {
  60. return false;
  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]
  76. initWithEnumerator:self.node.childEnumerator
  77. andTransform:^id(FNamedNode *node) {
  78. FIRDatabaseReference *childRef = [self.ref child:node.name];
  79. return [[FIRDataSnapshot alloc]
  80. initWithRef:childRef
  81. indexedNode:[FIndexedNode indexedNodeWithNode:node.node]];
  82. }];
  83. }
  84. - (NSString *)description {
  85. return
  86. [NSString stringWithFormat:@"Snap (%@) %@", self.key, self.node.node];
  87. }
  88. @end