Skip to content

Commit ff72020

Browse files
committed
Fix decoding and encoding issue for status structures
1 parent 023463f commit ff72020

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

Sources/ATProtoKit/Models/Lexicons/app.bsky/Actor/AppBskyActorDefs.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,12 +1493,33 @@ extension AppBskyLexicon.Actor {
14931493
case isActive
14941494
}
14951495

1496-
// Union
1496+
// Unions
14971497
/// A reference containing the list of embeds..
14981498
public enum EmbedUnion: Sendable, Codable, Equatable, Hashable {
14991499

15001500
/// An external embed view.
15011501
case externalView(AppBskyLexicon.Embed.ExternalDefinition.View)
1502+
1503+
public init(from decoder: any Decoder) throws {
1504+
let container = try decoder.singleValueContainer()
1505+
1506+
if let value = try? container.decode(AppBskyLexicon.Embed.ExternalDefinition.View.self) {
1507+
self = .externalView(value)
1508+
} else {
1509+
throw DecodingError.typeMismatch(
1510+
EmbedUnion.self, DecodingError.Context(
1511+
codingPath: decoder.codingPath, debugDescription: "Unknown EmbedUnion type"))
1512+
}
1513+
}
1514+
1515+
public func encode(to encoder: any Encoder) throws {
1516+
var container = encoder.singleValueContainer()
1517+
1518+
switch self {
1519+
case .externalView(let value):
1520+
try container.encode(value)
1521+
}
1522+
}
15021523
}
15031524
}
15041525
}

Sources/ATProtoKit/Models/Lexicons/app.bsky/Actor/AppBskyActorStatus.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension AppBskyLexicon.Actor {
2525
///
2626
/// - Note: According to the AT Protocol specifications: "An optional embed associated with
2727
/// the status."
28-
public let embed: String?
28+
public let embed: EmbedUnion?
2929

3030
/// The amount of time the status has lasted in minutes.
3131
///
@@ -36,7 +36,7 @@ extension AppBskyLexicon.Actor {
3636
/// The date and time the record was created.
3737
public let createdAt: Date
3838

39-
public init(status: Status, embed: String?, durationMinutes: Int?, createdAt: Date) {
39+
public init(status: Status, embed: EmbedUnion?, durationMinutes: Int?, createdAt: Date) {
4040
self.status = status
4141
self.embed = embed
4242
self.durationMinutes = durationMinutes
@@ -47,7 +47,7 @@ extension AppBskyLexicon.Actor {
4747
let container = try decoder.container(keyedBy: CodingKeys.self)
4848

4949
self.status = try container.decode(AppBskyLexicon.Actor.StatusRecord.Status.self, forKey: .status)
50-
self.embed = try container.decodeIfPresent(String.self, forKey: .embed)
50+
self.embed = try container.decodeIfPresent(EmbedUnion.self, forKey: .embed)
5151
self.durationMinutes = try container.decodeIfPresent(Int.self, forKey: .durationMinutes)
5252
self.createdAt = try container.decodeDate(forKey: .createdAt)
5353
}
@@ -85,6 +85,27 @@ extension AppBskyLexicon.Actor {
8585

8686
/// An external embed view.
8787
case externalView(AppBskyLexicon.Embed.ExternalDefinition.View)
88+
89+
public init(from decoder: any Decoder) throws {
90+
let container = try decoder.singleValueContainer()
91+
92+
if let value = try? container.decode(AppBskyLexicon.Embed.ExternalDefinition.View.self) {
93+
self = .externalView(value)
94+
} else {
95+
throw DecodingError.typeMismatch(
96+
EmbedUnion.self, DecodingError.Context(
97+
codingPath: decoder.codingPath, debugDescription: "Unknown EmbedUnion type"))
98+
}
99+
}
100+
101+
public func encode(to encoder: any Encoder) throws {
102+
var container = encoder.singleValueContainer()
103+
104+
switch self {
105+
case .externalView(let value):
106+
try container.encode(value)
107+
}
108+
}
88109
}
89110
}
90111
}

0 commit comments

Comments
 (0)