Skip to content

Commit

Permalink
Remove several sources of fatal errors from @group (#531)
Browse files Browse the repository at this point in the history
* Remove as many sources of fatal errors from @group value handling as we currently can.

Fixes #530
  • Loading branch information
gwynne authored Aug 16, 2022
1 parent 7fc64d3 commit 38670d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Sources/FluentKit/Properties/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ extension GroupProperty: AnyDatabaseProperty {
}

public func input(to input: DatabaseInput) {
self.value!.input(to: input.prefixed(by: self.prefix))
self.value?.input(to: input.prefixed(by: self.prefix))
}

public func output(from output: DatabaseOutput) throws {
if self.value == nil { self.value = .init() }
try self.value!.output(from: output.prefixed(by: self.prefix))
}
}
Expand All @@ -80,11 +81,14 @@ extension GroupProperty: AnyDatabaseProperty {

extension GroupProperty: AnyCodableProperty {
public func encode(to encoder: Encoder) throws {
try self.value!.encode(to: encoder)
try self.value?.encode(to: encoder)
}

public func decode(from decoder: Decoder) throws {
self.value = try .init(from: decoder)
let container = try decoder.singleValueContainer()

guard !container.decodeNil() else { return }
self.value = .some(try container.decode(Value.self))
}
}

Expand Down
43 changes: 43 additions & 0 deletions Tests/FluentKitTests/FluentKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,49 @@ final class FluentKitTests: XCTestCase {
XCTAssertEqual(decodedFoo2.$preFoo.id, foo2.$preFoo.id)
XCTAssert({ guard case .none = decodedFoo2.$preFoo.value else { return false }; return true }())
}

func testGroupCoding() throws {
final class GroupedFoo: Fields {
@Field(key: "hello")
var string: String

init() {}
}

final class GroupFoo: Model {
static let schema = "group_foos"

@ID(key: .id) var id: UUID?
@Group(key: "group") var group: GroupedFoo

init() {}
}
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys]
let decoder = JSONDecoder()

let groupFoo = GroupFoo()
groupFoo.id = UUID()
groupFoo.group.string = "hi"
let encoded = try encoder.encode(groupFoo)
XCTAssertEqual(String(decoding: encoded, as: UTF8.self), #"{"group":{"string":"hi"},"id":"\#(groupFoo.id!.uuidString)"}"#)

// TODO: This currently causes a fatal error when the Codable conformance tries to encode the unset group members.
/*
let missingGroupFoo = GroupFoo()
missingGroupFoo.id = UUID()
let missingEncoded = try encoder.encode(missingGroupFoo)
XCTAssertEqual(String(decoding: missingEncoded, as: UTF8.self), #"{"id":"\#(groupFoo.id!.uuidString)"}"#)
*/

let decoded = try decoder.decode(GroupFoo.self, from: encoded)
XCTAssertEqual(decoded.id?.uuidString, groupFoo.id?.uuidString)
XCTAssertEqual(decoded.group.string, groupFoo.group.string)

let decodedMissing = try decoder.decode(GroupFoo.self, from: #"{"id":"\#(groupFoo.id!.uuidString)"}"#.data(using: .utf8)!)
XCTAssertEqual(decodedMissing.id?.uuidString, groupFoo.id?.uuidString)
XCTAssertNotNil(decodedMissing.$group.value)
}

func testDatabaseGeneratedIDOverride() throws {
final class DGOFoo: Model {
Expand Down

0 comments on commit 38670d2

Please sign in to comment.