Skip to content

Commit af848c4

Browse files
authored
Make SentrySDKSettings only visible to Swift (#6927)
1 parent d6ab868 commit af848c4

13 files changed

+125
-298
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 8 additions & 16 deletions
Large diffs are not rendered by default.

Sources/Sentry/SentryClient.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,8 @@ - (void)captureReplayEvent:(SentryReplayEvent *)replayEvent
529529

530530
// Hybrid SDKs may override the sdk info for a replay Event,
531531
// the same SDK should be used for the envelope header.
532-
SentrySdkInfo *sdkInfo = replayEvent.sdk
533-
? [[SentrySdkInfo alloc]
534-
initWithDict:SENTRY_UNWRAP_NULLABLE_DICT(NSString *, id, replayEvent.sdk)]
535-
: [SentrySdkInfo global];
536532
SentryEnvelopeHeader *envelopeHeader =
537-
[[SentryEnvelopeHeader alloc] initWithId:replayEvent.eventId
538-
sdkInfo:sdkInfo
539-
traceContext:nil];
533+
[[SentryEnvelopeHeader alloc] initWithId:replayEvent.eventId sdkInfo:replayEvent.sdk];
540534

541535
SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader
542536
items:@[ videoEnvelopeItem ]];
@@ -907,7 +901,7 @@ - (void)setSdk:(SentryEvent *)event
907901
return;
908902
}
909903

910-
event.sdk = [[[SentrySdkInfo alloc] initWithOptions:self.options] serialize];
904+
event.sdk = [SentrySdkInfoObjC optionsToDict:self.options];
911905
}
912906

913907
- (void)setUserInfo:(NSDictionary *_Nullable)userInfo withEvent:(SentryEvent *_Nullable)event

Sources/Swift/Helper/SentrySdkInfo.swift

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import Foundation
66
* @note Both name and version are required.
77
* @see https://develop.sentry.dev/sdk/event-payloads/sdk/
88
*/
9-
@_spi(Private) @objc public final class SentrySdkInfo: NSObject, SentrySerializable {
9+
final class SentrySdkInfo {
1010

11-
@objc public static func global() -> Self {
11+
static func global() -> Self {
1212
if let options = SentrySDKInternal.currentHub().getClient()?.getOptions() {
1313
let enabledFeatures = SentryDependencyContainerSwiftHelper.enabledFeatures(options)
1414
return Self(withEnabledFeatures: enabledFeatures, sendDefaultPii: SentryDependencyContainerSwiftHelper.sendDefaultPii(options))
@@ -19,49 +19,49 @@ import Foundation
1919
/**
2020
* The name of the SDK. Examples: sentry.cocoa, sentry.cocoa.vapor, ...
2121
*/
22-
@objc public let name: String
22+
let name: String
2323

2424
/**
2525
* The version of the SDK. It should have the Semantic Versioning format MAJOR.MINOR.PATCH, without
2626
* any prefix (no v or anything else in front of the major version number). Examples:
2727
* 0.1.0, 1.0.0, 2.0.0-beta0
2828
*/
29-
@objc public let version: String
29+
let version: String
3030

3131
/**
3232
* A list of names identifying enabled integrations. The list should
3333
* have all enabled integrations, including default integrations. Default
3434
* integrations are included because different SDK releases may contain different
3535
* default integrations.
3636
*/
37-
@objc public let integrations: [String]
37+
let integrations: [String]
3838

3939
/**
4040
* A list of feature names identifying enabled SDK features. This list
4141
* should contain all enabled SDK features. On some SDKs, enabling a feature in the
4242
* options also adds an integration. We encourage tracking such features with either
4343
* integrations or features but not both to reduce the payload size.
4444
*/
45-
@objc public let features: [String]
45+
let features: [String]
4646

4747
/**
4848
* A list of packages that were installed as part of this SDK or the
4949
* activated integrations. Each package consists of a name in the format
5050
* source:identifier and version.
5151
*/
52-
@objc public let packages: [[String: String]]
52+
let packages: [[String: String]]
5353

5454
/**
5555
* A set of settings as part of this SDK.
5656
*/
57-
@objc public let settings: SentrySDKSettings
57+
let settings: SentrySDKSettings
5858

59-
@objc public convenience init(withOptions options: Options?) {
59+
convenience init(withOptions options: Options?) {
6060
let features = SentryEnabledFeaturesBuilder.getEnabledFeatures(options: options)
6161
self.init(withEnabledFeatures: features, sendDefaultPii: options?.sendDefaultPii ?? false)
6262
}
6363

64-
@objc public convenience init(withEnabledFeatures features: [String], sendDefaultPii: Bool) {
64+
convenience init(withEnabledFeatures features: [String], sendDefaultPii: Bool) {
6565
let integrations = SentrySDKInternal.currentHub().trimmedInstalledIntegrationNames()
6666
var packages = SentryExtraPackages.getPackages()
6767
let sdkPackage = SentrySdkPackage.global()
@@ -77,7 +77,7 @@ import Foundation
7777
settings: SentrySDKSettings(sendDefaultPii: sendDefaultPii))
7878
}
7979

80-
@objc public init(name: String?, version: String?, integrations: [String]?, features: [String]?, packages: [[String: String]]?, settings: SentrySDKSettings) {
80+
init(name: String?, version: String?, integrations: [String]?, features: [String]?, packages: [[String: String]]?, settings: SentrySDKSettings) {
8181
self.name = name ?? ""
8282
self.version = version ?? ""
8383
self.integrations = integrations ?? []
@@ -87,40 +87,39 @@ import Foundation
8787
}
8888

8989
// swiftlint:disable cyclomatic_complexity
90-
@objc
91-
public convenience init(dict: [AnyHashable: Any]) {
90+
convenience init(dict: [AnyHashable: Any]?) {
9291
var name = ""
9392
var version = ""
9493
var integrations = Set<String>()
9594
var features = Set<String>()
9695
var packages = Set<[String: String]>()
9796
var settings = SentrySDKSettings(dict: [:])
9897

99-
if let nameValue = dict["name"] as? String {
98+
if let nameValue = dict?["name"] as? String {
10099
name = nameValue
101100
}
102101

103-
if let versionValue = dict["version"] as? String {
102+
if let versionValue = dict?["version"] as? String {
104103
version = versionValue
105104
}
106105

107-
if let integrationArray = dict["integrations"] as? [Any] {
106+
if let integrationArray = dict?["integrations"] as? [Any] {
108107
for item in integrationArray {
109108
if let integration = item as? String {
110109
integrations.insert(integration)
111110
}
112111
}
113112
}
114113

115-
if let featureArray = dict["features"] as? [Any] {
114+
if let featureArray = dict?["features"] as? [Any] {
116115
for item in featureArray {
117116
if let feature = item as? String {
118117
features.insert(feature)
119118
}
120119
}
121120
}
122121

123-
if let packageArray = dict["packages"] as? [Any] {
122+
if let packageArray = dict?["packages"] as? [Any] {
124123
for item in packageArray {
125124
if let package = item as? [String: Any],
126125
let name = package["name"] as? String,
@@ -130,7 +129,7 @@ import Foundation
130129
}
131130
}
132131

133-
if let settingsDict = dict["settings"] as? NSDictionary {
132+
if let settingsDict = dict?["settings"] as? NSDictionary {
134133
settings = SentrySDKSettings(dict: settingsDict)
135134
}
136135

@@ -145,7 +144,7 @@ import Foundation
145144
}
146145
// swiftlint:enable cyclomatic_complexity
147146

148-
@objc public func serialize() -> [String: Any] {
147+
func serialize() -> [String: Any] {
149148
[
150149
"name": self.name,
151150
"version": self.version,
@@ -156,3 +155,11 @@ import Foundation
156155
]
157156
}
158157
}
158+
159+
@_spi(Private) @objc public final class SentrySdkInfoObjC: NSObject {
160+
161+
@objc public static func optionsToDict(_ options: Options) -> [String: Any] {
162+
SentrySdkInfo(withOptions: options).serialize()
163+
}
164+
165+
}

Sources/Swift/Protocol/SentrySDKSettings.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
* Describes the settings for the Sentry SDK
33
* @see https://develop.sentry.dev/sdk/event-payloads/sdk/
44
*/
5-
@_spi(Private) @objc public final class SentrySDKSettings: NSObject {
5+
final class SentrySDKSettings {
66

7-
@objc public override init() {
7+
init() {
88
autoInferIP = false
99
}
1010

11-
@objc public convenience init(options: Options?) {
11+
convenience init(options: Options?) {
1212
self.init(sendDefaultPii: options?.sendDefaultPii ?? false)
1313
}
1414

15-
@objc public init(sendDefaultPii: Bool) {
15+
init(sendDefaultPii: Bool) {
1616
autoInferIP = sendDefaultPii
1717
}
1818

19-
@objc public init(dict: NSDictionary) {
19+
init(dict: NSDictionary) {
2020
if let inferIp = dict["infer_ip"] as? String {
2121
autoInferIP = inferIp == "auto"
2222
} else {
2323
autoInferIP = false
2424
}
2525
}
2626

27-
@objc public var autoInferIP: Bool
27+
var autoInferIP: Bool
2828

29-
@objc public func serialize() -> NSDictionary {
29+
func serialize() -> NSDictionary {
3030
[
3131
"infer_ip": autoInferIP ? "auto" : "never"
3232
]

Sources/Swift/Tools/SentryEnvelopeHeader.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @param eventId The identifier of the event. Can be nil if no event in the envelope or attachment
88
* related to event.
99
*/
10-
@objc public convenience init(id eventId: SentryId?) {
10+
convenience init(id eventId: SentryId?) {
1111
self.init(id: eventId, traceContext: nil)
1212
}
1313

@@ -31,13 +31,17 @@
3131
* instances should always provide a version.
3232
* @param traceContext Current trace state.
3333
*/
34-
@objc public
3534
init(id eventId: SentryId?, sdkInfo: SentrySdkInfo?, traceContext: TraceContext?) {
3635
self.eventId = eventId
3736
self.sdkInfo = sdkInfo
3837
self.traceContext = traceContext
3938
}
40-
39+
40+
@objc public convenience init(id eventId: SentryId?, sdkInfo: [AnyHashable: Any]?) {
41+
let info = sdkInfo.map { SentrySdkInfo(dict: $0) } ?? SentrySdkInfo.global()
42+
self.init(id: eventId, sdkInfo: info, traceContext: nil)
43+
}
44+
4145
@objc public static func empty() -> Self {
4246
Self(id: nil, traceContext: nil)
4347
}
@@ -48,7 +52,7 @@
4852
* Attachments
4953
*/
5054
@objc public var eventId: SentryId?
51-
@objc public var sdkInfo: SentrySdkInfo?
55+
var sdkInfo: SentrySdkInfo?
5256
@objc public var traceContext: TraceContext?
5357

5458
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@testable import Sentry
2+
3+
extension SentrySdkInfo {
4+
public static func == (lhs: Sentry.SentrySdkInfo, rhs: Sentry.SentrySdkInfo) -> Bool {
5+
return lhs.name == rhs.name &&
6+
lhs.version == rhs.version &&
7+
Set(lhs.integrations) == Set(rhs.integrations) &&
8+
Set(lhs.features) == Set(rhs.features) &&
9+
Set(lhs.packages) == Set(rhs.packages) &&
10+
lhs.settings == rhs.settings
11+
}
12+
}
13+
14+
#if compiler(>=6.0)
15+
extension SentrySdkInfo: @retroactive Equatable { }
16+
#else
17+
extension SentrySdkInfo: Equatable { }
18+
#endif

Tests/SentryTests/Protocol/SentrySDKSettings+Equality.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

Tests/SentryTests/Protocol/SentrySDKSettings+Equality.m

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@testable import Sentry
2+
3+
extension SentrySDKSettings {
4+
public static func == (lhs: Sentry.SentrySDKSettings, rhs: Sentry.SentrySDKSettings) -> Bool {
5+
lhs.autoInferIP == rhs.autoInferIP
6+
}
7+
}
8+
9+
#if compiler(>=6.0)
10+
extension SentrySDKSettings: @retroactive Equatable { }
11+
#else
12+
extension SentrySDKSettings: Equatable { }
13+
#endif

Tests/SentryTests/Protocol/SentrySdkInfo+Equality.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)