Skip to content

Commit

Permalink
fix: fixed initialized immutable variables not encoded by default (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyamahunt committed Dec 4, 2023
1 parent d378204 commit 31db2fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Sources/CodableMacroPlugin/Builders/IgnoreCodingBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct IgnoreCodingBuilder<Input: Variable>: RegistrationBuilder {
/// An attribute type indicating explicit decoding/encoding when attached
/// to variable declarations.
///
/// Attaching attributes of this type to computed properties or initialized
/// immutable properties indicates this variable should be encoded for the type.
/// Attaching attributes of this type to computed properties indicates
/// this variable should be encoded for the type.
fileprivate protocol CodingAttribute: PropertyAttribute {}
extension CodedIn: CodingAttribute {}
extension CodedAt: CodingAttribute {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ where Wrapped.Initialization: RequiredVariableInitialization {
var decode: Bool? { options.`init` ? base.decode : false }
/// Whether the variable is to be encoded.
///
/// Depends on whether variable is initializable if underlying variable doesn't
/// specify explicit encoding. Otherwise depends on whether underlying variable
/// is to be decoded.
var encode: Bool? { base.encode ?? options.`init` }
/// Depends on whether variable is initializable if underlying variable
/// doesn't specify explicit encoding. Otherwise depends on whether
/// underlying variable is to be decoded.
var encode: Bool? {
return base.encode ?? (options.initialized || options.`init`)
}

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

/// Indicates the initialization type for this variable.
Expand Down
4 changes: 2 additions & 2 deletions Sources/MetaCodable/IgnoreCoding.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Indicates the field needs to ignored from decoding and encoding.
///
/// This macro can be applied to initialized mutable variables to ignore
/// them from both decoding and encoding.
/// This macro can be applied to initialized variables to ignore them
/// from both decoding and encoding.
/// ```swift
/// @IgnoreCoding
/// var field: String = "some"
Expand Down
6 changes: 6 additions & 0 deletions Tests/MetaCodableTests/GroupedVariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ final class GroupedVariableTests: XCTestCase {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.one, forKey: CodingKeys.one)
try container.encode(self.two, forKey: CodingKeys.two)
try container.encode(self.three, forKey: CodingKeys.three)
}
}
extension SomeCodable {
enum CodingKeys: String, CodingKey {
case one = "one"
case two = "two"
case three = "three"
}
}
"""
Expand Down Expand Up @@ -220,13 +222,15 @@ final class GroupedVariableTests: XCTestCase {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.one, forKey: CodingKeys.one)
try container.encode(self.two, forKey: CodingKeys.two)
try container.encode(self.three, forKey: CodingKeys.three)
}
}
extension SomeCodable {
enum CodingKeys: String, CodingKey {
case one = "one"
case two = "two"
case three = "three"
}
}
Expand Down Expand Up @@ -266,13 +270,15 @@ final class GroupedVariableTests: XCTestCase {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.one, forKey: CodingKeys.one)
try container.encode(self.two, forKey: CodingKeys.two)
try container.encode(self.three, forKey: CodingKeys.three)
}
}
extension SomeCodable {
enum CodingKeys: String, CodingKey {
case one = "one"
case two = "two"
case three = "three"
}
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/MetaCodableTests/VariableDeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ final class VariableDeclarationTests: XCTestCase {
extension SomeCodable: Encodable {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: CodingKeys.value)
}
}
extension SomeCodable {
enum CodingKeys: String, CodingKey {
case value = "value"
}
}
"""
Expand Down

0 comments on commit 31db2fd

Please sign in to comment.