Skip to content

Commit 6bc4934

Browse files
Don't encode HasMany or HasOne to an SQLEncoder
1 parent 80dba3d commit 6bc4934

File tree

3 files changed

+6
-22
lines changed

3 files changed

+6
-22
lines changed

Sources/Alchemy/SQL/Rune/Model/Fields/ModelFieldReader.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ private struct _KeyedEncodingContainer<M: Model, Key: CodingKey>: KeyedEncodingC
7676
return
7777
}
7878

79+
guard !(value is AnyHas) else { return }
80+
7981
let keyString = encoder.mappingStrategy.map(input: key.stringValue)
8082
guard let convertible = value as? SQLValueConvertible else {
8183
// Assume anything else is JSON.

Sources/Alchemy/SQL/Rune/Relationships/PropertyWrappers/HasManyRelationship.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public final class HasManyRelationship<From: Model, To: ModelMaybeOptional>: Any
4747
public init(from decoder: Decoder) throws {}
4848

4949
public func encode(to encoder: Encoder) throws {
50-
if !(encoder is SQLEncoder) {
51-
try value.encode(to: encoder)
50+
if !(encoder is SQLEncoder), let underlyingValue = value {
51+
try underlyingValue.encode(to: encoder)
5252
}
5353
}
5454
}
@@ -58,12 +58,3 @@ extension HasManyRelationship: Equatable where To: Equatable {
5858
lhs.value == rhs.value
5959
}
6060
}
61-
62-
public extension KeyedEncodingContainer {
63-
// Only encode the underlying value if it exists.
64-
mutating func encode<From, To>(_ value: HasManyRelationship<From, To>, forKey key: Key) throws {
65-
if let underlyingValue = value.value {
66-
try encode(underlyingValue, forKey: key)
67-
}
68-
}
69-
}

Sources/Alchemy/SQL/Rune/Relationships/PropertyWrappers/HasOneRelationship.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public final class HasOneRelationship<From: Model, To: ModelMaybeOptional>: AnyH
4747
public init(from decoder: Decoder) throws {}
4848

4949
public func encode(to encoder: Encoder) throws {
50-
if !(encoder is SQLEncoder) {
51-
try value.encode(to: encoder)
50+
if !(encoder is SQLEncoder), let underlyingValue = value {
51+
try underlyingValue.encode(to: encoder)
5252
}
5353
}
5454
}
@@ -58,12 +58,3 @@ extension HasOneRelationship: Equatable where To: Equatable {
5858
lhs.value == rhs.value
5959
}
6060
}
61-
62-
public extension KeyedEncodingContainer {
63-
// Only encode the underlying value if it exists.
64-
mutating func encode<From, To>(_ value: HasOneRelationship<From, To>, forKey key: Key) throws {
65-
if let underlyingValue = value.value {
66-
try encode(underlyingValue, forKey: key)
67-
}
68-
}
69-
}

0 commit comments

Comments
 (0)