Skip to content

Commit

Permalink
fix Codable public impl under public extension
Browse files Browse the repository at this point in the history
  • Loading branch information
winddpan committed Jan 4, 2024
1 parent 1114708 commit 480916e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -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) {
Expand All @@ -56,6 +63,7 @@ struct ModelMemberPropertyContainer {
if !attributes.isEmpty {
attributes.append("")
}

return attributes.joined(separator: " ")
}

Expand Down Expand Up @@ -178,6 +186,7 @@ private extension ModelMemberPropertyContainer {
}

var mp = ModelMemberProperty(name: name, type: type)
mp.modifiers = variable.modifiers
let attributes = variable.attributes

// isOptional
Expand Down
7 changes: 7 additions & 0 deletions Tests/CodableWrapperTests/DeclareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
//
Expand Down

0 comments on commit 480916e

Please sign in to comment.