Skip to content

Commit 480916e

Browse files
committed
fix Codable public impl under public extension
1 parent 1114708 commit 480916e

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import SwiftSyntaxMacros
44
private struct ModelMemberProperty {
55
var name: String
66
var type: String
7+
var modifiers: DeclModifierListSyntax = []
78
var isOptional: Bool = false
89
var normalKeys: [String] = []
910
var nestedKeys: [String] = []
@@ -43,11 +44,17 @@ struct ModelMemberPropertyContainer {
4344
}
4445

4546
private func attributesPrefix(option: AttributeOption) -> String {
47+
let hasPublicProperites = memberProperties.contains(where: {
48+
$0.modifiers.contains(where: {
49+
$0.name.text == "public" || $0.name.text == "open"
50+
})
51+
})
52+
4653
let modifiers = decl.modifiers.compactMap { $0.name.text }
4754
var attributes: [String] = []
4855
if option.contains(.open), modifiers.contains("open") {
4956
attributes.append("open")
50-
} else if option.contains(.public), modifiers.contains("open") || modifiers.contains("public") {
57+
} else if option.contains(.public), hasPublicProperites || modifiers.contains("open") || modifiers.contains("public") {
5158
attributes.append("public")
5259
}
5360
if option.contains(.required), decl.is(ClassDeclSyntax.self) {
@@ -56,6 +63,7 @@ struct ModelMemberPropertyContainer {
5663
if !attributes.isEmpty {
5764
attributes.append("")
5865
}
66+
5967
return attributes.joined(separator: " ")
6068
}
6169

@@ -178,6 +186,7 @@ private extension ModelMemberPropertyContainer {
178186
}
179187

180188
var mp = ModelMemberProperty(name: name, type: type)
189+
mp.modifiers = variable.modifiers
181190
let attributes = variable.attributes
182191

183192
// isOptional

Tests/CodableWrapperTests/DeclareTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ struct StructWraningX {
5858
@CodingKey("abc") var subVal: String = "1_1"
5959
}
6060

61+
public extension ClassModel11 {
62+
@Codable
63+
class A {
64+
public var val: String = "1"
65+
}
66+
}
67+
6168
// @CodableSubclass
6269
// struct StructWraning0 {}
6370
//

0 commit comments

Comments
 (0)