Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Sources/AtprotoTypes/LexiconTypes/Union.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation
///Currently implemented as a union of records but could be any lexicon schema
public protocol LexiconUnion: Codable, Sendable {
static var members: [Atproto.Ref: any Atproto.Schema.Type] { get }

init(object: any Codable) throws
}

Expand All @@ -28,7 +28,7 @@ extension LexiconUnion {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: TypeHeader.self)
let ref = try container.decode(Atproto.Ref.self, forKey: .type)

let type = try Self.members[ref].tryUnwrap(
LexionUnionError.unknownType(ref)
)
Expand Down
48 changes: 42 additions & 6 deletions Tests/AtprotoTypesTests/UnionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ struct UnionTests {
[SimpleRelationshipResult].self,
from: Self.relationshipJsonString.utf8Data
)
print(result)

let encoded = try JSONEncoder().encode(result)

let _ = try JSONDecoder().decode(
[SimpleRelationshipResult].self,
from: encoded
)
}

}

//taken from //c
//taken from https://docs.bsky.app/docs/api/app-bsky-graph-get-relationships
enum SimpleRelationshipResult {
case relationship(Relationships)
case notFound(NotFoundActor)
Expand All @@ -36,38 +42,58 @@ enum SimpleRelationshipResult {
static var ref: Atproto.Ref {
.init(string: "app.bsky.graph.defs#relationship")
}
//for encoding
private(set) var ref: Atproto.Ref? = ref
let did: Atproto.DID
let blocking: Atproto.ATURI?
let blockedBy: Atproto.ATURI?
let following: Atproto.ATURI?
let followedBy: Atproto.ATURI?
let blockedByList: Atproto.ATURI?
let blockingbyList: Atproto.ATURI?

enum CodingKeys: String, CodingKey {
case ref = "$type"
case did
case blocking
case blockedBy
case following
case followedBy
case blockedByList
case blockingbyList
}
}

struct NotFoundActor: Atproto.Schema {
static var ref: Atproto.Ref {
.init(string: "app.bsky.graph.defs#notFoundActor")
}


//for encoding
private(set) var ref: Atproto.Ref? = ref
public let actor: LexiconString.AtIdentifier
var notFound: Bool = true

public init(actor: LexiconString.AtIdentifier) {
self.actor = actor
}

enum CodingKeys: String, CodingKey {
case ref = "$type"
case actor
case notFound
}
}
}

extension SimpleRelationshipResult: LexiconUnion {
static var members: [Atproto.Ref : any Atproto.Schema.Type] {
static var members: [Atproto.Ref: any Atproto.Schema.Type] {
[
Relationships.ref: Relationships.self,
NotFoundActor.ref: NotFoundActor.self
NotFoundActor.ref: NotFoundActor.self,
]
}


init(object: any Codable) throws {
if let relationships = object as? Relationships {
self = .relationship(relationships)
Expand All @@ -77,4 +103,14 @@ extension SimpleRelationshipResult: LexiconUnion {
throw LexionUnionError.unknownObject
}
}

func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .relationship(let relationships):
try container.encode(relationships)
case .notFound(let notFound):
try container.encode(notFound)
}
}
}
Loading