Files
firebase-ios-sdk/Firebase/Database/Api/FIRMutableData.m
T

149 lines
4.5 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FIRMutableData.h"
2019-08-05 13:27:47 -07:00
#import "FChildrenNode.h"
2017-05-15 12:27:07 -07:00
#import "FIRMutableData_Private.h"
2019-08-05 13:27:47 -07:00
#import "FIndexedNode.h"
#import "FNamedNode.h"
2017-05-15 12:27:07 -07:00
#import "FSnapshotHolder.h"
#import "FSnapshotUtilities.h"
#import "FTransformedEnumerator.h"
@interface FIRMutableData ()
2019-08-05 13:27:47 -07:00
- (id)initWithPrefixPath:(FPath *)path
andSnapshotHolder:(FSnapshotHolder *)snapshotHolder;
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
@property(strong, nonatomic) FSnapshotHolder *data;
@property(strong, nonatomic) FPath *prefixPath;
2017-05-15 12:27:07 -07:00
@end
@implementation FIRMutableData
@synthesize data;
@synthesize prefixPath;
2019-08-05 13:27:47 -07:00
- (id)initWithNode:(id<FNode>)node {
FSnapshotHolder *holder = [[FSnapshotHolder alloc] init];
FPath *path = [FPath empty];
2017-05-15 12:27:07 -07:00
[holder updateSnapshot:path withNewSnapshot:node];
return [self initWithPrefixPath:path andSnapshotHolder:holder];
}
2019-08-05 13:27:47 -07:00
- (id)initWithPrefixPath:(FPath *)path
andSnapshotHolder:(FSnapshotHolder *)snapshotHolder {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
self.prefixPath = path;
self.data = snapshotHolder;
}
return self;
}
- (FIRMutableData *)childDataByAppendingPath:(NSString *)path {
2019-08-05 13:27:47 -07:00
FPath *wholePath = [self.prefixPath childFromString:path];
return [[FIRMutableData alloc] initWithPrefixPath:wholePath
andSnapshotHolder:self.data];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (FIRMutableData *)parent {
2017-05-15 12:27:07 -07:00
if ([self.prefixPath isEmpty]) {
return nil;
} else {
2019-08-05 13:27:47 -07:00
FPath *path = [self.prefixPath parent];
return [[FIRMutableData alloc] initWithPrefixPath:path
andSnapshotHolder:self.data];
2017-05-15 12:27:07 -07:00
}
}
2019-08-05 13:27:47 -07:00
- (void)setValue:(id)aValue {
id<FNode> node = [FSnapshotUtilities nodeFrom:aValue
withValidationFrom:@"setValue:"];
2017-05-15 12:27:07 -07:00
[self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
}
2019-08-05 13:27:47 -07:00
- (void)setPriority:(id)aPriority {
2017-05-15 12:27:07 -07:00
id<FNode> node = [self.data getNode:self.prefixPath];
id<FNode> pri = [FSnapshotUtilities nodeFrom:aPriority];
node = [node updatePriority:pri];
[self.data updateSnapshot:self.prefixPath withNewSnapshot:node];
}
2019-08-05 13:27:47 -07:00
- (id)value {
2017-05-15 12:27:07 -07:00
return [[self.data getNode:self.prefixPath] val];
}
2019-08-05 13:27:47 -07:00
- (id)priority {
2017-05-15 12:27:07 -07:00
return [[[self.data getNode:self.prefixPath] getPriority] val];
}
2019-08-05 13:27:47 -07:00
- (BOOL)hasChildren {
2017-05-15 12:27:07 -07:00
id<FNode> node = [self.data getNode:self.prefixPath];
2019-08-05 13:27:47 -07:00
return ![node isLeafNode] && ![(FChildrenNode *)node isEmpty];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (BOOL)hasChildAtPath:(NSString *)path {
2017-05-15 12:27:07 -07:00
id<FNode> node = [self.data getNode:self.prefixPath];
2019-08-05 13:27:47 -07:00
FPath *childPath = [[FPath alloc] initWith:path];
2017-05-15 12:27:07 -07:00
return ![[node getChild:childPath] isEmpty];
}
2019-08-05 13:27:47 -07:00
- (NSUInteger)childrenCount {
2017-05-15 12:27:07 -07:00
return [[self.data getNode:self.prefixPath] numChildren];
}
2019-08-05 13:27:47 -07:00
- (NSString *)key {
2017-05-15 12:27:07 -07:00
return [self.prefixPath getBack];
}
2019-08-05 13:27:47 -07:00
- (id<FNode>)nodeValue {
2017-05-15 12:27:07 -07:00
return [self.data getNode:self.prefixPath];
}
2019-08-05 13:27:47 -07:00
- (NSEnumerator<FIRMutableData *> *)children {
FIndexedNode *indexedNode =
[FIndexedNode indexedNodeWithNode:self.nodeValue];
return [[FTransformedEnumerator alloc]
initWithEnumerator:[indexedNode childEnumerator]
andTransform:^id(FNamedNode *node) {
FPath *childPath = [self.prefixPath childFromString:node.name];
FIRMutableData *childData =
[[FIRMutableData alloc] initWithPrefixPath:childPath
andSnapshotHolder:self.data];
return childData;
}];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (BOOL)isEqualToData:(FIRMutableData *)other {
return self.data == other.data &&
[[self.prefixPath description]
isEqualToString:[other.prefixPath description]];
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
- (NSString *)description {
2017-05-15 12:27:07 -07:00
if (self.key == nil) {
2019-08-05 13:27:47 -07:00
return [NSString
stringWithFormat:@"FIRMutableData (top-most transaction) %@ %@",
self.key, self.value];
2017-05-15 12:27:07 -07:00
} else {
2019-08-05 13:27:47 -07:00
return [NSString
stringWithFormat:@"FIRMutableData (%@) %@", self.key, self.value];
2017-05-15 12:27:07 -07:00
}
}
@end