Skip to content

Commit 31db2fd

Browse files
authored
fix: fixed initialized immutable variables not encoded by default (#47)
1 parent d378204 commit 31db2fd

5 files changed

Lines changed: 20 additions & 9 deletions

File tree

Sources/CodableMacroPlugin/Builders/IgnoreCodingBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ struct IgnoreCodingBuilder<Input: Variable>: RegistrationBuilder {
4343
/// An attribute type indicating explicit decoding/encoding when attached
4444
/// to variable declarations.
4545
///
46-
/// Attaching attributes of this type to computed properties or initialized
47-
/// immutable properties indicates this variable should be encoded for the type.
46+
/// Attaching attributes of this type to computed properties indicates
47+
/// this variable should be encoded for the type.
4848
fileprivate protocol CodingAttribute: PropertyAttribute {}
4949
extension CodedIn: CodingAttribute {}
5050
extension CodedAt: CodingAttribute {}

Sources/CodableMacroPlugin/Variables/InitializationVariable.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ where Wrapped.Initialization: RequiredVariableInitialization {
4343
var decode: Bool? { options.`init` ? base.decode : false }
4444
/// Whether the variable is to be encoded.
4545
///
46-
/// Depends on whether variable is initializable if underlying variable doesn't
47-
/// specify explicit encoding. Otherwise depends on whether underlying variable
48-
/// is to be decoded.
49-
var encode: Bool? { base.encode ?? options.`init` }
46+
/// Depends on whether variable is initializable if underlying variable
47+
/// doesn't specify explicit encoding. Otherwise depends on whether
48+
/// underlying variable is to be decoded.
49+
var encode: Bool? {
50+
return base.encode ?? (options.initialized || options.`init`)
51+
}
5052

5153
/// Whether the variable type requires `Decodable` conformance.
5254
///
@@ -63,7 +65,7 @@ where Wrapped.Initialization: RequiredVariableInitialization {
6365
/// specifies explicit encoding. Otherwise depends on
6466
/// whether underlying variable is to be decoded.
6567
var requireEncodable: Bool? {
66-
return base.requireEncodable ?? options.`init`
68+
return base.requireEncodable ?? (options.initialized || options.`init`)
6769
}
6870

6971
/// Indicates the initialization type for this variable.

Sources/MetaCodable/IgnoreCoding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Indicates the field needs to ignored from decoding and encoding.
22
///
3-
/// This macro can be applied to initialized mutable variables to ignore
4-
/// them from both decoding and encoding.
3+
/// This macro can be applied to initialized variables to ignore them
4+
/// from both decoding and encoding.
55
/// ```swift
66
/// @IgnoreCoding
77
/// var field: String = "some"

Tests/MetaCodableTests/GroupedVariableTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ final class GroupedVariableTests: XCTestCase {
8787
var container = encoder.container(keyedBy: CodingKeys.self)
8888
try container.encode(self.one, forKey: CodingKeys.one)
8989
try container.encode(self.two, forKey: CodingKeys.two)
90+
try container.encode(self.three, forKey: CodingKeys.three)
9091
}
9192
}
9293
9394
extension SomeCodable {
9495
enum CodingKeys: String, CodingKey {
9596
case one = "one"
9697
case two = "two"
98+
case three = "three"
9799
}
98100
}
99101
"""
@@ -220,13 +222,15 @@ final class GroupedVariableTests: XCTestCase {
220222
func encode(to encoder: any Encoder) throws {
221223
var container = encoder.container(keyedBy: CodingKeys.self)
222224
try container.encode(self.one, forKey: CodingKeys.one)
225+
try container.encode(self.two, forKey: CodingKeys.two)
223226
try container.encode(self.three, forKey: CodingKeys.three)
224227
}
225228
}
226229
227230
extension SomeCodable {
228231
enum CodingKeys: String, CodingKey {
229232
case one = "one"
233+
case two = "two"
230234
case three = "three"
231235
}
232236
}
@@ -266,13 +270,15 @@ final class GroupedVariableTests: XCTestCase {
266270
func encode(to encoder: any Encoder) throws {
267271
var container = encoder.container(keyedBy: CodingKeys.self)
268272
try container.encode(self.one, forKey: CodingKeys.one)
273+
try container.encode(self.two, forKey: CodingKeys.two)
269274
try container.encode(self.three, forKey: CodingKeys.three)
270275
}
271276
}
272277
273278
extension SomeCodable {
274279
enum CodingKeys: String, CodingKey {
275280
case one = "one"
281+
case two = "two"
276282
case three = "three"
277283
}
278284
}

Tests/MetaCodableTests/VariableDeclarationTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ final class VariableDeclarationTests: XCTestCase {
3030
3131
extension SomeCodable: Encodable {
3232
func encode(to encoder: any Encoder) throws {
33+
var container = encoder.container(keyedBy: CodingKeys.self)
34+
try container.encode(self.value, forKey: CodingKeys.value)
3335
}
3436
}
3537
3638
extension SomeCodable {
3739
enum CodingKeys: String, CodingKey {
40+
case value = "value"
3841
}
3942
}
4043
"""

0 commit comments

Comments
 (0)