-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathMPUserAttributeChange.m
More file actions
62 lines (49 loc) · 1.97 KB
/
MPUserAttributeChange.m
File metadata and controls
62 lines (49 loc) · 1.97 KB
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
#import "MPUserAttributeChange.h"
#import "MPIConstants.h"
@implementation MPUserAttributeChange
@synthesize valueToLog = _valueToLog;
- (nullable instancetype)initWithUserAttributes:(nullable NSDictionary<NSString *, id> *)userAttributes key:(nonnull NSString *)key value:(nullable id)value {
Class NSStringClass = [NSString class];
Class NSArrayClass = [NSArray class];
BOOL validKey = !MPIsNull(key) && [key isKindOfClass:NSStringClass];
BOOL isValueAnArray = [value isKindOfClass:NSArrayClass];
NSAssert(validKey, @"'key' must be a string.");
NSAssert(value == nil || (value != nil && ([value isKindOfClass:NSStringClass] || [value isKindOfClass:[NSNumber class]] || isValueAnArray) || (NSNull *)value == [NSNull null]), @"'value' must be either nil, string, number, or array of strings.");
if (!validKey || (!userAttributes && !value)) {
return nil;
}
self = [super init];
if (self) {
_userAttributes = userAttributes;
_key = key;
_value = value;
_changed = YES;
_deleted = NO;
id existingValue = userAttributes[key];
if (existingValue) {
_isArray = [existingValue isKindOfClass:NSArrayClass] || isValueAnArray;
BOOL isExistingValueNull = (NSNull *)existingValue == [NSNull null];
BOOL isNewValueNull = (NSNull *)value == [NSNull null];
if (isNewValueNull) {
_changed = !isExistingValueNull;
} else if (value) {
_changed = isExistingValueNull || ![existingValue isEqual:value];
} else {
_changed = YES;
}
} else {
_isArray = isValueAnArray;
}
}
return self;
}
- (id)valueToLog {
if (!_valueToLog) {
_valueToLog = _value && !_deleted ? _value : [NSNull null];
}
return _valueToLog;
}
- (void)setValueToLog:(id)valueToLog {
_valueToLog = valueToLog ? valueToLog : [NSNull null];
}
@end