Skip to content

Commit 04d9bfa

Browse files
committed
(ref): Move SentryEventDecodable to Swift
1 parent dcf4a8e commit 04d9bfa

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

Sources/Sentry/Public/SentryEvent.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ NS_SWIFT_NAME(Event)
181181
/**
182182
* Init an @c SentryEvent will set all needed fields by default.
183183
*/
184-
- (instancetype)init;
184+
- (instancetype)init NS_DESIGNATED_INITIALIZER;
185185

186186
/**
187187
* Init a @c SentryEvent with a @c SentryLevelError and set all needed fields by default.
@@ -196,24 +196,4 @@ NS_SWIFT_NAME(Event)
196196

197197
@end
198198

199-
/**
200-
* Subclass of SentryEvent so we can add the Decodable implementation via a Swift extension. We need
201-
* this due to our mixed use of public Swift and ObjC classes. We could avoid this class by
202-
* converting SentryReplayEvent back to ObjC, but we rather accept this tradeoff as we want to
203-
* convert all public classes to Swift in the future. This class needs to be public as we can't add
204-
* the Decodable extension implementation to a class that is not public.
205-
*
206-
* @note: We can’t add the extension for Decodable directly on SentryEvent, because we get an error
207-
* in SentryReplayEvent: 'required' initializer 'init(from:)' must be provided by subclass of
208-
* 'Event' Once we add the initializer with required convenience public init(from decoder: any
209-
* Decoder) throws { fatalError("init(from:) has not been implemented")
210-
* }
211-
* we get the error initializer 'init(from:)' is declared in extension of 'Event' and cannot be
212-
* overridden. Therefore, we add the Decodable implementation not on the Event, but to a subclass of
213-
* the event.
214-
*/
215-
@interface SentryEventDecodable : SentryEvent
216-
217-
@end
218-
219199
NS_ASSUME_NONNULL_END

Sources/Sentry/SentryEvent.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@
2525

2626
@implementation SentryEvent
2727

28+
// This is a designated initializer so that it can be called by the Swift
29+
// sublcass that adds a `Decodable` conformance. It shares an implementation
30+
// with `initWithLevel:`.
2831
- (instancetype)init
2932
{
30-
return [self initWithLevel:kSentryLevelNone];
33+
self = [super init];
34+
return [self commonInit:kSentryLevelNone];
3135
}
3236

3337
- (instancetype)initWithLevel:(enum SentryLevel)level
3438
{
3539
self = [super init];
40+
return [self commonInit:level];
41+
}
42+
43+
- (instancetype)commonInit:(enum SentryLevel)level
44+
{
3645
if (self) {
3746
self.eventId = [[SentryId alloc] init];
3847
self.level = level;
@@ -210,8 +219,4 @@ - (BOOL)isAppHangEvent
210219

211220
@end
212221

213-
@implementation SentryEventDecodable
214-
215-
@end
216-
217222
NS_ASSUME_NONNULL_END

Sources/Swift/Protocol/Codable/SentryEventCodable.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
@_implementationOnly import _SentryPrivate
22
import Foundation
33

4-
extension SentryEventDecodable: Decodable {
4+
/**
5+
* Subclass of SentryEvent so we can add the Decodable implementation via a Swift extension. We need
6+
* this due to our mixed use of public Swift and ObjC classes. We could avoid this class by
7+
* converting SentryReplayEvent back to ObjC, but we rather accept this tradeoff as we want to
8+
* convert all public classes to Swift in the future. This does not need to be public, but was previously
9+
* defined in objc and was public. In the next major version of the SDK we should make it `internal` and `final`
10+
* and remove the `@objc` annotation.
11+
*
12+
* @note: We can’t add the extension for Decodable directly on SentryEvent, because we get an error
13+
* in SentryReplayEvent: 'required' initializer 'init(from:)' must be provided by subclass of
14+
* 'Event' Once we add the initializer with required convenience public init(from decoder: any
15+
* Decoder) throws { fatalError("init(from:) has not been implemented")
16+
* }
17+
* we get the error initializer 'init(from:)' is declared in extension of 'Event' and cannot be
18+
* overridden. Therefore, we add the Decodable implementation not on the Event, but to a subclass of
19+
* the event.
20+
*/
21+
@objc(SentryEventDecodable)
22+
public class SentryEventDecodable: Event, Decodable {
523

624
private enum CodingKeys: String, CodingKey {
725
case eventId = "event_id"

0 commit comments

Comments
 (0)