From 6d1811ab35e048aeeace3f2dedfdfa618ff4de98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Thu, 28 Mar 2024 10:24:25 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Set=20default=20values=20=E2=80=8B=E2=80=8B?= =?UTF-8?q?for=20mutable=20variables=20via=20initialization=20syntax.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/PluginCore/Attributes/Default.swift | 20 +++++++++++++++++++ .../Variables/Type/MemberGroup.swift | 1 + .../VariableDeclarationTests.swift | 18 ++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Sources/PluginCore/Attributes/Default.swift b/Sources/PluginCore/Attributes/Default.swift index fcdabde7..33b7e1f4 100644 --- a/Sources/PluginCore/Attributes/Default.swift +++ b/Sources/PluginCore/Attributes/Default.swift @@ -77,6 +77,26 @@ where } } +extension Registration +where +Decl == PropertyDeclSyntax, Var: PropertyVariable & InitializableVariable, Var.Initialization == AnyRequiredVariableInitialization, Var == AnyPropertyVariable +{ + /// Update registration with binding initializer value. If the ``Default`` attribute is applied, it takes precedence. + /// + /// New registration is updated with default expression data that will be + /// used for decoding failure and memberwise initializer(s), if provided. + /// + /// - Returns: Newly built registration with default expression data or self. + func addDefaultValueIfInitializerExists() -> Self { + guard Default(from: self.decl) == nil, let value = decl.binding.initializer?.value, let variable = self.variable.base as? AnyPropertyVariable else { + return self + } + + let newVar = variable.with(default: value) + return self.updating(with: newVar.any) + } +} + fileprivate extension PropertyVariable where Initialization == RequiredInitialization { /// Update variable data with the default value expression provided. diff --git a/Sources/PluginCore/Variables/Type/MemberGroup.swift b/Sources/PluginCore/Variables/Type/MemberGroup.swift index aaa6a29b..b6146389 100644 --- a/Sources/PluginCore/Variables/Type/MemberGroup.swift +++ b/Sources/PluginCore/Variables/Type/MemberGroup.swift @@ -192,6 +192,7 @@ where Decl.ChildSyntaxInput == Void, Decl.MemberSyntax == PropertyDeclSyntax { .useHelperCoderIfExists() .checkForAlternateKeyValues(addTo: codingKeys, context: context) .addDefaultValueIfExists() + .addDefaultValueIfInitializerExists() .checkCanBeInitialized() .checkCodingIgnored() } diff --git a/Tests/MetaCodableTests/VariableDeclarationTests.swift b/Tests/MetaCodableTests/VariableDeclarationTests.swift index ca84ae78..783adf91 100644 --- a/Tests/MetaCodableTests/VariableDeclarationTests.swift +++ b/Tests/MetaCodableTests/VariableDeclarationTests.swift @@ -69,7 +69,11 @@ final class VariableDeclarationTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.value = try container.decode(String.self, forKey: CodingKeys.value) + do { + self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? "some" + } catch { + self.value = "some" + } } } @@ -308,8 +312,16 @@ final class VariableDeclarationTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.value1 = try container.decode(String.self, forKey: CodingKeys.value1) - self.value2 = try container.decode(String.self, forKey: CodingKeys.value2) + do { + self.value1 = try container.decodeIfPresent(String.self, forKey: CodingKeys.value1) ?? "some" + } catch { + self.value1 = "some" + } + do { + self.value2 = try container.decodeIfPresent(String.self, forKey: CodingKeys.value2) ?? "some" + } catch { + self.value2 = "some" + } } } From 2103192f949ce5ecfae8e08a03c66089214c0406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Thu, 28 Mar 2024 17:53:59 +0800 Subject: [PATCH 2/6] Streamlined protocol conformance --- Sources/PluginCore/Attributes/Default.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/PluginCore/Attributes/Default.swift b/Sources/PluginCore/Attributes/Default.swift index 33b7e1f4..065245f0 100644 --- a/Sources/PluginCore/Attributes/Default.swift +++ b/Sources/PluginCore/Attributes/Default.swift @@ -79,7 +79,8 @@ where extension Registration where -Decl == PropertyDeclSyntax, Var: PropertyVariable & InitializableVariable, Var.Initialization == AnyRequiredVariableInitialization, Var == AnyPropertyVariable + Decl == PropertyDeclSyntax, + Var == AnyPropertyVariable { /// Update registration with binding initializer value. If the ``Default`` attribute is applied, it takes precedence. /// From d2e8cdb553248183112240ebb2ed24cc00292793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Fri, 29 Mar 2024 16:01:07 +0800 Subject: [PATCH 3/6] 1. When there are initialized mutable variables, only an initializing constructor with default parameters is generated. 2. Modify test cases --- Sources/PluginCore/Attributes/CodedBy.swift | 2 +- Sources/PluginCore/Attributes/Default.swift | 18 +++--- .../OptionalInitialization.swift | 24 ------- ...quiredInitializationWithDefaultValue.swift | 2 +- .../RequiredVariableInitialization.swift | 9 --- .../Property/DefaultValueVariable.swift | 2 +- .../Property/InitializationVariable.swift | 6 +- .../Variables/Type/MemberGroup.swift | 2 +- .../CodableInheritanceTests.swift | 18 +++++- .../GroupedMutableVariableTests.swift | 39 ++++++------ .../MetaCodableTests/IgnoreCodingTests.swift | 62 ++++++++++++++++--- .../IgnoreInitializedTests.swift | 12 +++- .../VariableDeclarationTests.swift | 18 +----- 13 files changed, 113 insertions(+), 101 deletions(-) delete mode 100644 Sources/PluginCore/Variables/Initialization/OptionalInitialization.swift diff --git a/Sources/PluginCore/Attributes/CodedBy.swift b/Sources/PluginCore/Attributes/CodedBy.swift index a6f8ae76..d95cccdb 100644 --- a/Sources/PluginCore/Attributes/CodedBy.swift +++ b/Sources/PluginCore/Attributes/CodedBy.swift @@ -74,7 +74,7 @@ package struct CodedBy: PropertyAttribute { extension Registration where Decl: AttributableDeclSyntax, Var: DefaultPropertyVariable, - Var.Initialization == RequiredInitialization + Var.Initialization: RequiredVariableInitialization { /// The optional variable data with helper expression /// that output registration will have. diff --git a/Sources/PluginCore/Attributes/Default.swift b/Sources/PluginCore/Attributes/Default.swift index 065245f0..0fd6f5c1 100644 --- a/Sources/PluginCore/Attributes/Default.swift +++ b/Sources/PluginCore/Attributes/Default.swift @@ -58,7 +58,7 @@ package struct Default: PropertyAttribute { extension Registration where Decl: AttributableDeclSyntax, Var: PropertyVariable, - Var.Initialization == RequiredInitialization + Var.Initialization: RequiredVariableInitialization { /// The variable data with default expression /// that output registration will have. @@ -77,29 +77,31 @@ where } } +extension AnyPropertyVariable: DefaultPropertyVariable {} + extension Registration where - Decl == PropertyDeclSyntax, - Var == AnyPropertyVariable + Decl: AttributableDeclSyntax, Var: PropertyVariable, + Var.Initialization: RequiredVariableInitialization { /// Update registration with binding initializer value. If the ``Default`` attribute is applied, it takes precedence. /// /// New registration is updated with default expression data that will be /// used for decoding failure and memberwise initializer(s), if provided. /// + /// - Parameter decl: The declaration to check for attribute. /// - Returns: Newly built registration with default expression data or self. - func addDefaultValueIfInitializerExists() -> Self { - guard Default(from: self.decl) == nil, let value = decl.binding.initializer?.value, let variable = self.variable.base as? AnyPropertyVariable else { - return self + func addDefaultValueIfInitializerExists() -> Registration> { + guard let value = self.variable.value else { + return self.updating(with: variable.any) } - let newVar = variable.with(default: value) return self.updating(with: newVar.any) } } fileprivate extension PropertyVariable -where Initialization == RequiredInitialization { +where Initialization: RequiredVariableInitialization { /// Update variable data with the default value expression provided. /// /// `DefaultValueVariable` is created with this variable as base diff --git a/Sources/PluginCore/Variables/Initialization/OptionalInitialization.swift b/Sources/PluginCore/Variables/Initialization/OptionalInitialization.swift deleted file mode 100644 index 3cbc574b..00000000 --- a/Sources/PluginCore/Variables/Initialization/OptionalInitialization.swift +++ /dev/null @@ -1,24 +0,0 @@ -/// Represents initialization is optional for the variable. -/// -/// The variable must be mutable and initialized already. -struct OptionalInitialization: VariableInitialization -where Wrapped: RequiredVariableInitialization { - /// The value wrapped by this instance. - /// - /// Only function parameter and code block provided - /// with`RequiredVariableInitialization` - /// can be wrapped by this instance. - let base: Wrapped - - /// Adds current initialization type to memberwise initialization - /// generator. - /// - /// New memberwise initialization generator is created after adding this - /// initialization as optional and returned. - /// - /// - Parameter generator: The init-generator to add in. - /// - Returns: The modified generator containing this initialization. - func add(to generator: MemberwiseInitGenerator) -> MemberwiseInitGenerator { - generator.add(optional: .init(param: base.param, code: base.code)) - } -} diff --git a/Sources/PluginCore/Variables/Initialization/RequiredInitializationWithDefaultValue.swift b/Sources/PluginCore/Variables/Initialization/RequiredInitializationWithDefaultValue.swift index 63409a40..f5ecd25b 100644 --- a/Sources/PluginCore/Variables/Initialization/RequiredInitializationWithDefaultValue.swift +++ b/Sources/PluginCore/Variables/Initialization/RequiredInitializationWithDefaultValue.swift @@ -12,7 +12,7 @@ struct RequiredInitializationWithDefaultValue: RequiredVariableInitialization { /// syntax is fetched from this value and /// updated when adding this initialization /// type to init-generator. - let base: RequiredInitialization + let base: RequiredVariableInitialization /// The default expression when /// no value provided explicitly. /// diff --git a/Sources/PluginCore/Variables/Initialization/RequiredVariableInitialization.swift b/Sources/PluginCore/Variables/Initialization/RequiredVariableInitialization.swift index 486f32d1..5cd35c65 100644 --- a/Sources/PluginCore/Variables/Initialization/RequiredVariableInitialization.swift +++ b/Sources/PluginCore/Variables/Initialization/RequiredVariableInitialization.swift @@ -20,12 +20,3 @@ package protocol RequiredVariableInitialization: VariableInitialization { /// generating initializer. var code: CodeBlockItemSyntax { get } } - -extension RequiredVariableInitialization { - /// Converts initialization to optional from required initialization. - /// - /// Wraps current instance in `OptionalInitialization`. - var optionalize: OptionalInitialization { - return .init(base: self) - } -} diff --git a/Sources/PluginCore/Variables/Property/DefaultValueVariable.swift b/Sources/PluginCore/Variables/Property/DefaultValueVariable.swift index ead46173..a646d231 100644 --- a/Sources/PluginCore/Variables/Property/DefaultValueVariable.swift +++ b/Sources/PluginCore/Variables/Property/DefaultValueVariable.swift @@ -10,7 +10,7 @@ /// * For providing default value to variable in memberwise initializer(s). struct DefaultValueVariable: ComposedVariable, PropertyVariable where - Wrapped: PropertyVariable, Wrapped.Initialization == RequiredInitialization + Wrapped: PropertyVariable, Wrapped.Initialization: RequiredVariableInitialization { /// The customization options for `DefaultValueVariable`. /// diff --git a/Sources/PluginCore/Variables/Property/InitializationVariable.swift b/Sources/PluginCore/Variables/Property/InitializationVariable.swift index afcd323c..096dc80d 100644 --- a/Sources/PluginCore/Variables/Property/InitializationVariable.swift +++ b/Sources/PluginCore/Variables/Property/InitializationVariable.swift @@ -110,11 +110,7 @@ where in context: some MacroExpansionContext ) -> AnyInitialization { return if options.`init` { - if options.initialized { - base.initializing(in: context).optionalize.any - } else { - base.initializing(in: context).any - } + base.initializing(in: context).any } else { IgnoredInitialization().any } diff --git a/Sources/PluginCore/Variables/Type/MemberGroup.swift b/Sources/PluginCore/Variables/Type/MemberGroup.swift index b6146389..0441dfc7 100644 --- a/Sources/PluginCore/Variables/Type/MemberGroup.swift +++ b/Sources/PluginCore/Variables/Type/MemberGroup.swift @@ -184,6 +184,7 @@ where Decl.ChildSyntaxInput == Void, Decl.MemberSyntax == PropertyDeclSyntax { return input .transformKeysAccordingToStrategy(attachedTo: decl) + .addDefaultValueIfInitializerExists() .checkInitializedCodingIgnored(attachedAt: decl) .registerKeyPath( provider: CodedAt(from: input.decl) @@ -192,7 +193,6 @@ where Decl.ChildSyntaxInput == Void, Decl.MemberSyntax == PropertyDeclSyntax { .useHelperCoderIfExists() .checkForAlternateKeyValues(addTo: codingKeys, context: context) .addDefaultValueIfExists() - .addDefaultValueIfInitializerExists() .checkCanBeInitialized() .checkCodingIgnored() } diff --git a/Tests/MetaCodableTests/CodableInheritanceTests.swift b/Tests/MetaCodableTests/CodableInheritanceTests.swift index a53acf8c..d3e95bd9 100644 --- a/Tests/MetaCodableTests/CodableInheritanceTests.swift +++ b/Tests/MetaCodableTests/CodableInheritanceTests.swift @@ -79,7 +79,11 @@ final class CodableInheritanceTests: XCTestCase { required init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.value = try container.decode(String.self, forKey: CodingKeys.value) + do { + self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? "" + } catch { + self.value = "" + } } func encode(to encoder: any Encoder) throws { @@ -122,7 +126,11 @@ final class CodableInheritanceTests: XCTestCase { required init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.value = try container.decode(String.self, forKey: CodingKeys.value) + do { + self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? "" + } catch { + self.value = "" + } try super.init(from: decoder) } @@ -161,7 +169,11 @@ final class CodableInheritanceTests: XCTestCase { required init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.value = try container.decode(String.self, forKey: CodingKeys.value) + do { + self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? "" + } catch { + self.value = "" + } try super.init(from: decoder) } diff --git a/Tests/MetaCodableTests/GroupedMutableVariableTests.swift b/Tests/MetaCodableTests/GroupedMutableVariableTests.swift index f9254fa4..c5881435 100644 --- a/Tests/MetaCodableTests/GroupedMutableVariableTests.swift +++ b/Tests/MetaCodableTests/GroupedMutableVariableTests.swift @@ -68,12 +68,7 @@ final class GroupedMutableVariableTests: XCTestCase { struct SomeCodable { var one, two: String, three: String = "" - init(one: String, two: String) { - self.one = one - self.two = two - } - - init(one: String, two: String, three: String) { + init(one: String, two: String, three: String = "") { self.one = one self.two = two self.three = three @@ -85,7 +80,11 @@ final class GroupedMutableVariableTests: XCTestCase { let container = try decoder.container(keyedBy: CodingKeys.self) self.one = try container.decode(String.self, forKey: CodingKeys.one) self.two = try container.decode(String.self, forKey: CodingKeys.two) - self.three = try container.decode(String.self, forKey: CodingKeys.three) + do { + self.three = try container.decodeIfPresent(String.self, forKey: CodingKeys.three) ?? "" + } catch { + self.three = "" + } } } @@ -215,12 +214,7 @@ final class GroupedMutableVariableTests: XCTestCase { struct SomeCodable { var one: String, two: String = "", three: Int - init(one: String, three: Int) { - self.one = one - self.three = three - } - - init(one: String, two: String, three: Int) { + init(one: String, two: String = "", three: Int) { self.one = one self.two = two self.three = three @@ -231,7 +225,11 @@ final class GroupedMutableVariableTests: XCTestCase { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.one = try container.decode(String.self, forKey: CodingKeys.one) - self.two = try container.decode(String.self, forKey: CodingKeys.two) + do { + self.two = try container.decodeIfPresent(String.self, forKey: CodingKeys.two) ?? "" + } catch { + self.two = "" + } self.three = try container.decode(Int.self, forKey: CodingKeys.three) } } @@ -270,12 +268,7 @@ final class GroupedMutableVariableTests: XCTestCase { struct SomeCodable { var one: String, two = "", three: Int - init(one: String, three: Int) { - self.one = one - self.three = three - } - - init(one: String, two: Int, three: Int) { + init(one: String, two: Int = "", three: Int) { self.one = one self.two = two self.three = three @@ -286,7 +279,11 @@ final class GroupedMutableVariableTests: XCTestCase { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.one = try container.decode(String.self, forKey: CodingKeys.one) - self.two = try container.decode(Int.self, forKey: CodingKeys.two) + do { + self.two = try container.decodeIfPresent(Int.self, forKey: CodingKeys.two) ?? "" + } catch { + self.two = "" + } self.three = try container.decode(Int.self, forKey: CodingKeys.three) } } diff --git a/Tests/MetaCodableTests/IgnoreCodingTests.swift b/Tests/MetaCodableTests/IgnoreCodingTests.swift index 75705dc9..d98708e8 100644 --- a/Tests/MetaCodableTests/IgnoreCodingTests.swift +++ b/Tests/MetaCodableTests/IgnoreCodingTests.swift @@ -295,7 +295,11 @@ final class IgnoreCodingTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.one = try container.decode(String.self, forKey: CodingKeys.one) + do { + self.one = try container.decodeIfPresent(String.self, forKey: CodingKeys.one) ?? "some" + } catch { + self.one = "some" + } self.two = try container.decode(String.self, forKey: CodingKeys.two) } } @@ -398,10 +402,30 @@ final class IgnoreCodingTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - let deeply_container = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.deeply) - let nested_deeply_container = try deeply_container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.nested) - self.four = try nested_deeply_container.decode(String.self, forKey: CodingKeys.two) - self.three = try nested_deeply_container.decode(String.self, forKey: CodingKeys.three) + if let deeply_container = try? container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.deeply) { + if let nested_deeply_container = try? deeply_container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.nested) { + do { + self.four = try nested_deeply_container.decodeIfPresent(String.self, forKey: CodingKeys.two) ?? "some" + } catch { + self.four = "some" + } + do { + self.three = try nested_deeply_container.decodeIfPresent(String.self, forKey: CodingKeys.three) ?? "some" + } catch { + self.three = "some" + } + } else { + self.one = "some" + self.two = "some" + self.four = "some" + self.three = "some" + } + } else { + self.one = "some" + self.two = "some" + self.four = "some" + self.three = "some" + } } } @@ -457,10 +481,30 @@ final class IgnoreCodingTests: XCTestCase { required init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - let deeply_container = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.deeply) - let nested_deeply_container = try deeply_container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.nested) - self.four = try nested_deeply_container.decode(String.self, forKey: CodingKeys.two) - self.three = try nested_deeply_container.decode(String.self, forKey: CodingKeys.three) + if let deeply_container = try? container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.deeply) { + if let nested_deeply_container = try? deeply_container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.nested) { + do { + self.four = try nested_deeply_container.decodeIfPresent(String.self, forKey: CodingKeys.two) ?? "some" + } catch { + self.four = "some" + } + do { + self.three = try nested_deeply_container.decodeIfPresent(String.self, forKey: CodingKeys.three) ?? "some" + } catch { + self.three = "some" + } + } else { + self.one = "some" + self.two = "some" + self.four = "some" + self.three = "some" + } + } else { + self.one = "some" + self.two = "some" + self.four = "some" + self.three = "some" + } } func encode(to encoder: any Encoder) throws { diff --git a/Tests/MetaCodableTests/IgnoreInitializedTests.swift b/Tests/MetaCodableTests/IgnoreInitializedTests.swift index 7671cf16..26846956 100644 --- a/Tests/MetaCodableTests/IgnoreInitializedTests.swift +++ b/Tests/MetaCodableTests/IgnoreInitializedTests.swift @@ -298,7 +298,11 @@ final class IgnoreInitializedTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.one = try container.decode(String.self, forKey: CodingKeys.one) + do { + self.one = try container.decodeIfPresent(String.self, forKey: CodingKeys.one) ?? "some" + } catch { + self.one = "some" + } } } @@ -376,7 +380,11 @@ final class IgnoreInitializedTests: XCTestCase { extension SomeCodable: Decodable { init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.one = try container.decode(String.self, forKey: CodingKeys.one) + do { + self.one = try container.decodeIfPresent(String.self, forKey: CodingKeys.one) ?? "some" + } catch { + self.one = "some" + } } } diff --git a/Tests/MetaCodableTests/VariableDeclarationTests.swift b/Tests/MetaCodableTests/VariableDeclarationTests.swift index 783adf91..4a67333a 100644 --- a/Tests/MetaCodableTests/VariableDeclarationTests.swift +++ b/Tests/MetaCodableTests/VariableDeclarationTests.swift @@ -58,10 +58,7 @@ final class VariableDeclarationTests: XCTestCase { struct SomeCodable { var value: String = "some" - init() { - } - - init(value: String) { + init(value: String = "some") { self.value = value } } @@ -292,18 +289,7 @@ final class VariableDeclarationTests: XCTestCase { } } - init() { - } - - init(value1: String) { - self.value1 = value1 - } - - init(value2: String) { - self.value2 = value2 - } - - init(value1: String, value2: String) { + init(value1: String = "some" , value2: String = "some" ) { self.value1 = value1 self.value2 = value2 } From 297756a69627fea99a1c6d7490e518c93f435743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Fri, 29 Mar 2024 16:12:47 +0800 Subject: [PATCH 4/6] Fixed the issue with comments after initializing variable variables after expansion. --- Sources/PluginCore/Attributes/Default.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PluginCore/Attributes/Default.swift b/Sources/PluginCore/Attributes/Default.swift index 0fd6f5c1..40fa6a8c 100644 --- a/Sources/PluginCore/Attributes/Default.swift +++ b/Sources/PluginCore/Attributes/Default.swift @@ -92,7 +92,7 @@ where /// - Parameter decl: The declaration to check for attribute. /// - Returns: Newly built registration with default expression data or self. func addDefaultValueIfInitializerExists() -> Registration> { - guard let value = self.variable.value else { + guard let value = self.variable.value?.trimmed else { return self.updating(with: variable.any) } let newVar = variable.with(default: value) From 20b128df2bc27ccbebf7560308c4ed5dc52b144b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Fri, 29 Mar 2024 16:40:15 +0800 Subject: [PATCH 5/6] fixed: `Default` attribute conflict. --- Sources/PluginCore/Attributes/Default.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PluginCore/Attributes/Default.swift b/Sources/PluginCore/Attributes/Default.swift index 40fa6a8c..fe0c8a69 100644 --- a/Sources/PluginCore/Attributes/Default.swift +++ b/Sources/PluginCore/Attributes/Default.swift @@ -92,7 +92,7 @@ where /// - Parameter decl: The declaration to check for attribute. /// - Returns: Newly built registration with default expression data or self. func addDefaultValueIfInitializerExists() -> Registration> { - guard let value = self.variable.value?.trimmed else { + guard Default(from: decl) == nil, let value = self.variable.value?.trimmed else { return self.updating(with: variable.any) } let newVar = variable.with(default: value) From 089d250ec9f06f11723327ec9b24ceab97a4bd29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=AC=E8=8A=B1=E6=A1=A5?= Date: Fri, 29 Mar 2024 16:58:47 +0800 Subject: [PATCH 6/6] fiexed: test case --- Tests/MetaCodableTests/VariableDeclarationTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/MetaCodableTests/VariableDeclarationTests.swift b/Tests/MetaCodableTests/VariableDeclarationTests.swift index 4a67333a..7ced4d63 100644 --- a/Tests/MetaCodableTests/VariableDeclarationTests.swift +++ b/Tests/MetaCodableTests/VariableDeclarationTests.swift @@ -289,7 +289,7 @@ final class VariableDeclarationTests: XCTestCase { } } - init(value1: String = "some" , value2: String = "some" ) { + init(value1: String = "some", value2: String = "some") { self.value1 = value1 self.value2 = value2 }