Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow partial sdk info override #1816

Merged
merged 4 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Allow partial SDK info override (#1816)

## 7.15.0

### Features
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/Public/SentrySdkInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ SENTRY_NO_INIT

- (instancetype)initWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict
orDefaults:(SentrySdkInfo *)info;
vaind marked this conversation as resolved.
Show resolved Hide resolved

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
// SentrySdkInfo already expects a dictionary with {"sdk": {"name": ..., "value": ...}}
// so we're passing the whole options object.
if ([options[@"sdk"] isKindOfClass:[NSDictionary class]]) {
_sdkInfo = [[SentrySdkInfo alloc] initWithDict:options];
_sdkInfo = [[SentrySdkInfo alloc] initWithDict:options orDefaults:_sdkInfo];
}

if (nil != error && nil != *error) {
Expand Down
19 changes: 19 additions & 0 deletions Sources/Sentry/SentrySdkInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ - (instancetype)initWithDict:(NSDictionary *)dict
return [self initWithName:name andVersion:version];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to reuse the new method with sth like:

return [self initWithDict:dict orDefaults:nil];

but that would require the arg to be nullable - do you want me to make the change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we could reuse the new method. We could also define an internal constructor that both initWithDict: and initWithDict:orDefaults use. Then we don't need to make the orDefaults nullable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried here, not sure that's the right way in objective-c though: 80edcb8

}

- (instancetype)initWithDict:(NSDictionary *)dict orDefaults:(SentrySdkInfo *)info;
{
NSString *name = info.name;
NSString *version = info.version;

if (nil != dict[@"sdk"] && [dict[@"sdk"] isKindOfClass:[NSDictionary class]]) {
NSDictionary<NSString *, id> *sdkInfoDict = dict[@"sdk"];
if ([sdkInfoDict[@"name"] isKindOfClass:[NSString class]]) {
name = sdkInfoDict[@"name"];
}

if ([sdkInfoDict[@"version"] isKindOfClass:[NSString class]]) {
version = sdkInfoDict[@"version"];
}
}

return [self initWithName:name andVersion:version];
}

- (NSDictionary<NSString *, id> *)serialize
{
return @{ @"sdk" : @ { @"name" : self.name, @"version" : self.version } };
Expand Down
28 changes: 28 additions & 0 deletions Tests/SentryTests/SentryOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,34 @@ - (void)testSetCustomSdkInfo
XCTAssertEqual(dict[@"version"], options.sdkInfo.version);
}

- (void)testSetCustomSdkName
{
NSDictionary *dict = @{ @"name" : @"custom.sdk" };

NSError *error = nil;
SentryOptions *options =
[[SentryOptions alloc] initWithDict:@{ @"sdk" : dict, @"dsn" : @"https://a:[email protected]/1" }
didFailWithError:&error];

XCTAssertNil(error);
XCTAssertEqual(dict[@"name"], options.sdkInfo.name);
XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); // default version
}

- (void)testSetCustomSdkVersion
{
NSDictionary *dict = @{ @"version" : @"1.2.3-alpha.0" };

NSError *error = nil;
SentryOptions *options =
[[SentryOptions alloc] initWithDict:@{ @"sdk" : dict, @"dsn" : @"https://a:[email protected]/1" }
didFailWithError:&error];

XCTAssertNil(error);
XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); // default name
XCTAssertEqual(dict[@"version"], options.sdkInfo.version);
}

- (void)testMaxAttachmentSize
{
NSNumber *maxAttachmentSize = @21;
Expand Down