From 480916e2a6c56b8195324a4615ffd269aa539bb9 Mon Sep 17 00:00:00 2001 From: winddpan Date: Thu, 4 Jan 2024 15:42:33 +0800 Subject: [PATCH] fix Codable `public impl` under public extension --- .../ModelMemberPropertyContainer.swift | 11 ++++++++++- Tests/CodableWrapperTests/DeclareTests.swift | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift b/Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift index e84ae81..09fff55 100644 --- a/Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift +++ b/Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift @@ -4,6 +4,7 @@ import SwiftSyntaxMacros private struct ModelMemberProperty { var name: String var type: String + var modifiers: DeclModifierListSyntax = [] var isOptional: Bool = false var normalKeys: [String] = [] var nestedKeys: [String] = [] @@ -43,11 +44,17 @@ struct ModelMemberPropertyContainer { } private func attributesPrefix(option: AttributeOption) -> String { + let hasPublicProperites = memberProperties.contains(where: { + $0.modifiers.contains(where: { + $0.name.text == "public" || $0.name.text == "open" + }) + }) + let modifiers = decl.modifiers.compactMap { $0.name.text } var attributes: [String] = [] if option.contains(.open), modifiers.contains("open") { attributes.append("open") - } else if option.contains(.public), modifiers.contains("open") || modifiers.contains("public") { + } else if option.contains(.public), hasPublicProperites || modifiers.contains("open") || modifiers.contains("public") { attributes.append("public") } if option.contains(.required), decl.is(ClassDeclSyntax.self) { @@ -56,6 +63,7 @@ struct ModelMemberPropertyContainer { if !attributes.isEmpty { attributes.append("") } + return attributes.joined(separator: " ") } @@ -178,6 +186,7 @@ private extension ModelMemberPropertyContainer { } var mp = ModelMemberProperty(name: name, type: type) + mp.modifiers = variable.modifiers let attributes = variable.attributes // isOptional diff --git a/Tests/CodableWrapperTests/DeclareTests.swift b/Tests/CodableWrapperTests/DeclareTests.swift index d6cc624..1837a46 100644 --- a/Tests/CodableWrapperTests/DeclareTests.swift +++ b/Tests/CodableWrapperTests/DeclareTests.swift @@ -58,6 +58,13 @@ struct StructWraningX { @CodingKey("abc") var subVal: String = "1_1" } +public extension ClassModel11 { + @Codable + class A { + public var val: String = "1" + } +} + // @CodableSubclass // struct StructWraning0 {} //