Skip to content

Commit

Permalink
Merge 81395a8 into 6bc31df
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin authored Mar 19, 2024
2 parents 6bc31df + 81395a8 commit 571d868
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 84 deletions.
24 changes: 11 additions & 13 deletions Sources/Sentry/NSArray+SentrySanitize.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@
#import "NSDate+SentryExtras.h"
#import "NSDictionary+SentrySanitize.h"

@implementation
NSArray (SentrySanitize)
@implementation SentryArray

- (NSArray *)sentry_sanitize
+ (NSArray *)sanitizeArray:(NSArray *)array;
{
NSMutableArray *array = [NSMutableArray array];
for (id rawValue in self) {

NSMutableArray *result = [NSMutableArray array];
for (id rawValue in array) {
if ([rawValue isKindOfClass:NSString.class]) {
[array addObject:rawValue];
[result addObject:rawValue];
} else if ([rawValue isKindOfClass:NSNumber.class]) {
[array addObject:rawValue];
[result addObject:rawValue];
} else if ([rawValue isKindOfClass:NSDictionary.class]) {
[array addObject:[(NSDictionary *)rawValue sentry_sanitize]];
[result addObject:[(NSDictionary *)rawValue sentry_sanitize]];
} else if ([rawValue isKindOfClass:NSArray.class]) {
[array addObject:[(NSArray *)rawValue sentry_sanitize]];
[result addObject:[SentryArray sanitizeArray:rawValue]];
} else if ([rawValue isKindOfClass:NSDate.class]) {
[array addObject:[(NSDate *)rawValue sentry_toIso8601String]];
[result addObject:[(NSDate *)rawValue sentry_toIso8601String]];
} else {
[array addObject:[rawValue description]];
[result addObject:[rawValue description]];
}
}
return array;
return result;
}

@end
2 changes: 1 addition & 1 deletion Sources/Sentry/NSDictionary+SentrySanitize.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ - (NSDictionary *)sentry_sanitize
} else if ([rawValue isKindOfClass:NSDictionary.class]) {
[dict setValue:[(NSDictionary *)rawValue sentry_sanitize] forKey:stringKey];
} else if ([rawValue isKindOfClass:NSArray.class]) {
[dict setValue:[(NSArray *)rawValue sentry_sanitize] forKey:stringKey];
[dict setValue:[SentryArray sanitizeArray:rawValue] forKey:stringKey];
} else if ([rawValue isKindOfClass:NSDate.class]) {
[dict setValue:[(NSDate *)rawValue sentry_toIso8601String] forKey:stringKey];
} else {
Expand Down
5 changes: 2 additions & 3 deletions Sources/Sentry/NSLocale+Sentry.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#import "NSLocale+Sentry.h"

@implementation
NSLocale (Sentry)
@implementation SentryLocale

- (BOOL)sentry_timeIs24HourFormat
+ (BOOL)timeIs24HourFormat
{
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateStyle:NSDateFormatterNoStyle];
Expand Down
24 changes: 13 additions & 11 deletions Sources/Sentry/NSMutableDictionary+Sentry.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#import "NSMutableDictionary+Sentry.h"

@implementation
NSMutableDictionary (Sentry)
@implementation SentryDictionary

- (void)mergeEntriesFromDictionary:(NSDictionary *)otherDictionary
+ (void)mergeEntriesFromDictionary:(NSDictionary *)dictionary
intoDictionary:(NSMutableDictionary *)destination
{
[otherDictionary enumerateKeysAndObjectsUsingBlock:^(id otherKey, id otherObj, BOOL *stop) {
[dictionary enumerateKeysAndObjectsUsingBlock:^(id otherKey, id otherObj, BOOL *stop) {
if ([otherObj isKindOfClass:NSDictionary.class] &&
[self[otherKey] isKindOfClass:NSDictionary.class]) {
NSMutableDictionary *mergedDict = ((NSDictionary *)self[otherKey]).mutableCopy;
[mergedDict mergeEntriesFromDictionary:(NSDictionary *)otherObj];
self[otherKey] = mergedDict;
[destination[otherKey] isKindOfClass:NSDictionary.class]) {
NSMutableDictionary *mergedDict = ((NSDictionary *)destination[otherKey]).mutableCopy;
[SentryDictionary mergeEntriesFromDictionary:otherObj intoDictionary:mergedDict];
destination[otherKey] = mergedDict;
return;
}

self[otherKey] = otherObj;
destination[otherKey] = otherObj;
}];
}

- (void)setBoolValue:(nullable NSNumber *)value forKey:(NSString *)key
+ (void)setBoolValue:(nullable NSNumber *)value
forKey:(NSString *)key
intoDictionary:(NSMutableDictionary *)destination
{
if (value != nil) {
[self setValue:@([value boolValue]) forKey:key];
[destination setValue:@([value boolValue]) forKey:key];
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ - (void)applyCultureContextToEvent:(SentryEvent *)event
}
#endif
culture[@"locale"] = self.locale.localeIdentifier;
culture[@"is_24_hour_format"] = @(self.locale.sentry_timeIs24HourFormat);
culture[@"is_24_hour_format"] = @([SentryLocale timeIs24HourFormat]);
culture[@"timezone"] = self.timezone.name;
}];
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentryFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ - (instancetype)init
[serializedData setValue:self.imageAddress forKey:@"image_addr"];
[serializedData setValue:self.instructionAddress forKey:@"instruction_addr"];
[serializedData setValue:self.platform forKey:@"platform"];
[serializedData setBoolValue:self.inApp forKey:@"in_app"];
[serializedData setBoolValue:self.stackStart forKey:@"stack_start"];
[SentryDictionary setBoolValue:self.inApp forKey:@"in_app" intoDictionary:serializedData];
[SentryDictionary setBoolValue:self.stackStart
forKey:@"stack_start"
intoDictionary:serializedData];

return serializedData;
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryMechanism.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (instancetype)initWithType:(NSString *)type
{
NSMutableDictionary *serializedData = @{ @"type" : self.type }.mutableCopy;

[serializedData setBoolValue:self.handled forKey:@"handled"];
[SentryDictionary setBoolValue:self.handled forKey:@"handled" intoDictionary:serializedData];
[serializedData setValue:self.synthetic forKey:@"synthetic"];
[serializedData setValue:self.desc forKey:@"description"];
[serializedData setValue:[self.data sentry_sanitize] forKey:@"data"];
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryScope.m
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ - (SentryEvent *__nullable)applyToEvent:(SentryEvent *)event

NSMutableDictionary *newContext = [self context].mutableCopy;
if (event.context != nil) {
[newContext mergeEntriesFromDictionary:event.context];
[SentryDictionary mergeEntriesFromDictionary:event.context intoDictionary:newContext];
}

if (self.span != nil) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentrySession.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ - (void)incrementErrors
}
.mutableCopy;

[serializedData setBoolValue:_init forKey:@"init"];
[SentryDictionary setBoolValue:_init forKey:@"init" intoDictionary:serializedData];

NSString *statusString = nameForSentrySessionStatus(_status);

Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryStacktrace.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ - (void)fixDuplicateFrames
if (self.registers.count > 0) {
[serializedData setValue:self.registers forKey:@"registers"];
}
[serializedData setBoolValue:self.snapshot forKey:@"snapshot"];

[SentryDictionary setBoolValue:self.snapshot forKey:@"snapshot" intoDictionary:serializedData];
return serializedData;
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/Sentry/SentryThread.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ - (instancetype)initWithThreadId:(NSNumber *)threadId
{
NSMutableDictionary *serializedData =
@{ @"id" : self.threadId ? self.threadId : @(99) }.mutableCopy;

[serializedData setBoolValue:self.crashed forKey:@"crashed"];
[serializedData setBoolValue:self.current forKey:@"current"];
[SentryDictionary setBoolValue:self.crashed forKey:@"crashed" intoDictionary:serializedData];
[SentryDictionary setBoolValue:self.current forKey:@"current" intoDictionary:serializedData];
[serializedData setValue:self.name forKey:@"name"];
[serializedData setValue:[self.stacktrace serialize] forKey:@"stacktrace"];
[serializedData setBoolValue:self.isMain forKey:@"main"];
[SentryDictionary setBoolValue:self.isMain forKey:@"main" intoDictionary:serializedData];

return serializedData;
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ - (void)addAppStartMeasurements:(SentryTransaction *)transaction
NSMutableDictionary *context =
[[NSMutableDictionary alloc] initWithDictionary:[transaction context]];
NSDictionary *appContext = @{ @"app" : @ { @"start_type" : appStartType } };
[context mergeEntriesFromDictionary:appContext];
[SentryDictionary mergeEntriesFromDictionary:appContext intoDictionary:context];
[transaction setContext:context];

// The backend calculates statistics on the number and size of debug images for app
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryUIViewControllerSwizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ - (BOOL)swizzleRootViewControllerFromUIApplication:(id<SentryUIApplication>)app

- (void)swizzleRootViewControllerAndDescendant:(UIViewController *)rootViewController
{
NSArray<UIViewController *> *allViewControllers
= rootViewController.sentry_descendantViewControllers;
NSArray<UIViewController *> *allViewControllers =
[SentryViewController descendantsOfViewController:rootViewController];

for (UIViewController *viewController in allViewControllers) {
Class viewControllerClass = [viewController class];
Expand Down
15 changes: 7 additions & 8 deletions Sources/Sentry/UIViewController+Sentry.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

#if SENTRY_HAS_UIKIT

@implementation
UIViewController (Sentry)
@implementation SentryViewController

- (NSArray<UIViewController *> *)sentry_descendantViewControllers
+ (NSArray<UIViewController *> *)descendantsOfViewController:(UIViewController *)viewController;
{

// The implementation of UIViewController makes sure a parent can't be a child of his child.
// Therefore, we can assume the parent child relationship is correct.

NSMutableArray<UIViewController *> *allViewControllers = [NSMutableArray new];
[allViewControllers addObject:self];
[allViewControllers addObject:viewController];

NSMutableArray<UIViewController *> *toAdd =
[NSMutableArray arrayWithArray:self.childViewControllers];
[NSMutableArray arrayWithArray:viewController.childViewControllers];

while (toAdd.count > 0) {
UIViewController *viewController = [toAdd lastObject];
[allViewControllers addObject:viewController];
UIViewController *lastVC = [toAdd lastObject];
[allViewControllers addObject:lastVC];
[toAdd removeLastObject];
[toAdd addObjectsFromArray:viewController.childViewControllers];
[toAdd addObjectsFromArray:lastVC.childViewControllers];
}

return allViewControllers;
Expand Down
5 changes: 2 additions & 3 deletions Sources/Sentry/include/NSArray+SentrySanitize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

NS_ASSUME_NONNULL_BEGIN

@interface
NSArray (SentrySanitize)
@interface SentryArray : NSObject

- (NSArray *)sentry_sanitize;
+ (NSArray *)sanitizeArray:(NSArray *)array;

@end

Expand Down
5 changes: 2 additions & 3 deletions Sources/Sentry/include/NSLocale+Sentry.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#import <Foundation/Foundation.h>

@interface
NSLocale (Sentry)
@interface SentryLocale : NSObject

- (BOOL)sentry_timeIs24HourFormat;
+ (BOOL)timeIs24HourFormat;

@end
10 changes: 6 additions & 4 deletions Sources/Sentry/include/NSMutableDictionary+Sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

NS_ASSUME_NONNULL_BEGIN

@interface
NSMutableDictionary (Sentry)
@interface SentryDictionary : NSObject

/**
* Merges the otherDictionary into the given dictionary by overriding existing keys with the values
* of the other dictionary.
*/
- (void)mergeEntriesFromDictionary:(NSDictionary *)otherDictionary;
+ (void)mergeEntriesFromDictionary:(NSDictionary *)dictionary
intoDictionary:(NSMutableDictionary *)destination;

- (void)setBoolValue:(nullable NSNumber *)value forKey:(NSString *)key;
+ (void)setBoolValue:(nullable NSNumber *)value
forKey:(NSString *)key
intoDictionary:(NSMutableDictionary *)destination;

@end

Expand Down
6 changes: 2 additions & 4 deletions Sources/Sentry/include/UIViewController+Sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

NS_ASSUME_NONNULL_BEGIN

@interface
UIViewController (Sentry)
@interface SentryViewController : NSObject

/**
* An array of view controllers that are descendants, meaning children, grandchildren, ... , of the
* current view controller.
*/
@property (nonatomic, readonly, strong)
NSArray<UIViewController *> *sentry_descendantViewControllers;
+ (NSArray<UIViewController *> *)descendantsOfViewController:(UIViewController *)viewController;

@end

Expand Down
28 changes: 12 additions & 16 deletions Tests/SentryTests/Categories/NSMutableDictionarySentryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,70 @@ class NSMutableDictionarySentryTests: XCTestCase {

func testEmptyDictionary() {
let empty = NSMutableDictionary()

empty.mergeEntries(from: [String: String]())

SentryDictionary.mergeEntries(from: [String: String](), into: empty)
XCTAssertEqual(empty, NSMutableDictionary())
}

func testEmptyDict_AddsDict() {
let dict = NSMutableDictionary()
dict.mergeEntries(from: [0: [1: 1]])
SentryDictionary.mergeEntries(from: [0: [1: 1]], into: dict)

XCTAssertEqual([0: [1: 1]], dict as? [Int: [Int: Int]])
}

func testTwoEntries_ExistingGetsMerged() {
let dict = NSMutableDictionary(dictionary: [0: [0: 0], 1: [0: 0]])
dict.mergeEntries(from: [0: [1: 1]])
SentryDictionary.mergeEntries(from: [0: [1: 1]], into: dict)

XCTAssertEqual([0: [0: 0, 1: 1], 1: [0: 0]], dict as? [Int: [Int: Int]])
}

func testTwoEntries_SameGetsOverwritten() {
let dict = NSMutableDictionary(dictionary: [0: [0: 0]])
dict.mergeEntries(from: [0: [0: 1]])
SentryDictionary.mergeEntries(from: [0: [0: 1]], into: dict)

XCTAssertEqual([0: [0: 1]], dict as? [Int: [Int: Int]])
}

func testTwoLevelsNested_ExistingGetsMerged() {
let dict = NSMutableDictionary(dictionary: [0: [0: [0: 0]]])
dict.mergeEntries(from: [0: [0: [1: 1]]])
SentryDictionary.mergeEntries(from: [0: [0: [1: 1]]], into: dict)

XCTAssertEqual([0: [0: [0: 0, 1: 1]]], dict as? [Int: [Int: [Int: Int]]])
}

func testExistingNotADict_NewIsADict_GetsOverwritten() {
let dict = NSMutableDictionary(dictionary: [0: 0])
dict.mergeEntries(from: [0: [1: 1]])
SentryDictionary.mergeEntries(from: [0: [1: 1]], into: dict)

XCTAssertEqual([0: [1: 1]], dict as? [Int: [Int: Int]])
}

func testExistingIsADict_NewIsNotADict_GetsOverwritten() {
let dict = NSMutableDictionary(dictionary: [0: [0: 0]])
dict.mergeEntries(from: [0: 1])
SentryDictionary.mergeEntries(from: [0: 1], into: dict)

XCTAssertEqual([0: 1], dict as? [Int: Int])
}

func testSetBool_Nil_DoesNotSetValue() {
let dict = NSMutableDictionary()

dict.setBoolValue(nil, forKey: "key")

SentryDictionary.setBoolValue(nil, forKey: "key", into: dict)
XCTAssertEqual(0, dict.count)
}

func testSetBool_True_SetsTrue() {
let dict = NSMutableDictionary()

dict.setBoolValue(true, forKey: "key")
SentryDictionary.setBoolValue(true, forKey: "key", into: dict)

XCTAssertTrue(dict["key"] as? Bool ?? false)
}

func testSetBool_False_SetsFalse() {
let dict = NSMutableDictionary()

dict.setBoolValue(false, forKey: "key")
SentryDictionary.setBoolValue(false, forKey: "key", into: dict)

XCTAssertFalse(dict["key"] as? Bool ?? true)
XCTAssertEqual(0, dict["key"] as? NSNumber)
Expand All @@ -80,8 +76,8 @@ class NSMutableDictionarySentryTests: XCTestCase {
func testSetBool_NonZero_SetsTrue() {
let dict = NSMutableDictionary()

dict.setBoolValue(1, forKey: "key1")
dict.setBoolValue(-1, forKey: "key-1")
SentryDictionary.setBoolValue(1, forKey: "key1", into: dict)
SentryDictionary.setBoolValue(-1, forKey: "key-1", into: dict)

XCTAssertTrue(dict["key1"] as? Bool ?? false)
XCTAssertEqual(1, dict["key1"] as? NSNumber)
Expand Down
Loading

0 comments on commit 571d868

Please sign in to comment.