Bladeren bron

feat: 增加 didSet 监听器的判断

陈文艺 1 maand geleden
bovenliggende
commit
730ad21275
1 gewijzigde bestanden met toevoegingen van 14 en 2 verwijderingen
  1. 14 2
      Sources/AutoCodableMacro/AutoCodableMacro.swift

+ 14 - 2
Sources/AutoCodableMacro/AutoCodableMacro.swift

@@ -56,8 +56,17 @@ public struct AutoCodableMacro: MemberMacro {
             }
             
             // 过滤计算属性
-            guard binding.accessorBlock == nil else {
-                return nil
+            if let accessorBlock = binding.accessorBlock {
+                // 判断 accessor 类型
+                switch accessorBlock.accessors {
+                case .accessors(let accessors):
+                    let hasGet = accessors.contains { $0.accessorSpecifier.tokenKind == .keyword(.get) }
+                    if hasGet {
+                        return nil
+                    }
+                case .getter:
+                    return nil
+                }
             }
             
             // 3. 提取属性名称
@@ -133,6 +142,7 @@ enum AutoCodableMacroError: DiagnosticMessage {
     case onlyClassesAndStructs
     case noStoredProperties
     case protocolError
+    case customError(String)
     
     var message: String {
         switch self {
@@ -142,6 +152,8 @@ enum AutoCodableMacroError: DiagnosticMessage {
             "Type has no stored properties; Codable implementation will be empty"
         case .protocolError:
             "类型必须遵循 Codable、Decodable 或 Encodable"
+        case .customError(let err):
+            err
         }
     }