-
Notifications
You must be signed in to change notification settings - Fork 43
/
SPMemberDictionary.m
69 lines (49 loc) · 2.21 KB
/
SPMemberDictionary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// SPMemberDictionary.m
// Simperium
//
// Created by Jorge Leandro Perez on 01-21-2021.
// Copyright (c) 2021 Simperium. All rights reserved.
//
#import "SPMemberDictionary.h"
#import "SPLogger.h"
#pragma mark - Constants
static SPLogLevels logLevel = SPLogLevelsInfo;
#pragma mark SPMemberDictionary
@implementation SPMemberDictionary
- (id)defaultValue {
return @{};
}
- (NSDictionary *)diff:(id)thisValue otherValue:(id)otherValue {
// Failsafe: In Release Builds, let's return an empty diff if the input is invalid
NSString *mismatchMessage = @"Simperium error: couldn't diff dictionaries because their classes weren't NSDictionary";
NSAssert([thisValue isKindOfClass:[NSDictionary class]] && [otherValue isKindOfClass:[NSDictionary class]], mismatchMessage);
if (![thisValue isKindOfClass:[NSDictionary class]] || ![otherValue isKindOfClass:[NSDictionary class]]) {
SPLogError(mismatchMessage);
return @{ };
}
if ([thisValue isEqual:otherValue]) {
return @{ };
}
// Construct the diff in the expected format
return @{
OP_OP : OP_REPLACE,
OP_VALUE : otherValue
};
}
- (id)getValueFromDictionary:(NSDictionary *)dict key:(NSString *)key object:(id<SPDiffable>)object {
id value = [super getValueFromDictionary:dict key:key object:object];
// Failsafe
return [value isKindOfClass:[NSDictionary class]] ? value : nil;
}
- (id)applyDiff:(id)thisValue otherValue:(id)otherValue error:(NSError **)error {
NSAssert(thisValue == nil || [thisValue isKindOfClass:[NSDictionary class]], @"Simperium error: couldn't apply diff to double because its class wasn't NSDictionary");
NSAssert(otherValue == nil || [otherValue isKindOfClass:[NSDictionary class]], @"Simperium error: couldn't apply diff to double because its class wasn't NSDictionary");
// TODO: We should probably diff, eventually
return otherValue;
}
- (NSDictionary *)transform:(id)thisValue otherValue:(id)otherValue oldValue:(id)oldValue error:(NSError **)error {
// By default, don't transform anything, and take the local pending value
return [NSDictionary dictionaryWithObjectsAndKeys:OP_REPLACE, OP_OP, thisValue, OP_VALUE, nil];
}
@end