Skip to content

Commit 1364f6b

Browse files
committed
feat: CodingKeyIgnored
1 parent e8300fc commit 1364f6b

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Requirements
22

3-
| Xcode | Minimun Deployments | Version |
4-
| --------- | ------------------- | -------------------------------------------------------------- |
5-
| Xcode15+ | iOS13+ / macOS11+ | 1.0+ |
6-
| < Xcode15 | < iOS13 / macOS11 | [0.3.3](https://github.com/winddpan/CodableWrapper/tree/0.3.3) |
3+
| Xcode | Minimun Deployments | Version |
4+
| -------- | ------------------- | -------------------------------------------------------------- |
5+
| Xcode15+ | iOS13+ / macOS11+ | 1.0+ |
6+
| Xcode15- | iOS13- / macOS11- | [0.3.3](https://github.com/winddpan/CodableWrapper/tree/0.3.3) |
77

88
# About
99

@@ -22,9 +22,11 @@ The project objective is to enhance the usage experience of the Codable protocol
2222
## Installation
2323

2424
#### CocoaPods
25+
2526
``` pod 'CodableWrapper', :git => 'https://github.com/winddpan/CodableWrapper.git' ```
2627

2728
#### Swift Package Manager
29+
2830
``` https://github.com/winddpan/CodableWrapper ```
2931

3032
# Example
@@ -151,6 +153,15 @@ final class CodableWrapperTests: XCTestCase {
151153
}
152154
}
153155
```
156+
157+
```swift
158+
@Codable(wiseInit: false)
159+
public struct TestModel {
160+
public var userName: String = ""
161+
162+
// Disable WiseInit Automatic generated
163+
}
164+
```
154165

155166
## @CodingKey
156167

@@ -166,6 +177,20 @@ final class CodableWrapperTests: XCTestCase {
166177
// { "u9": "jhon" }
167178
```
168179

180+
## @CodingKeyIgnored
181+
182+
- Ignored propery on encode and decode
183+
184+
```swift
185+
struct NonCodable {}
186+
187+
@Codable
188+
struct TestModel {
189+
@CodingKeyIgnored
190+
var nonCodable: NonCodable?
191+
}
192+
```
193+
169194
## @CodingNestedKey
170195

171196
* Custom `CodingKey`s in `nested dictionary`

Sources/CodableWrapper/CodableWrapperMacros.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public macro CodableSubclass() = #externalMacro(module: "CodableWrapperMacros",
88
@attached(peer)
99
public macro CodingKey(_ key: String ...) = #externalMacro(module: "CodableWrapperMacros", type: "CodingKey")
1010

11+
@attached(peer)
12+
public macro CodingKeyIgnored() = #externalMacro(module: "CodableWrapperMacros", type: "CodingKeyIgnored")
13+
1114
@attached(peer)
1215
public macro CodingNestedKey(_ key: String ...) = #externalMacro(module: "CodableWrapperMacros", type: "CodingNestedKey")
1316

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import SwiftSyntax
2+
import SwiftSyntaxMacros
3+
4+
public struct CodingKeyIgnored: PeerMacro {
5+
public static func expansion(of node: AttributeSyntax, providingPeersOf declaration: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
6+
return []
7+
}
8+
}

Sources/CodableWrapperMacros/ModelMemberPropertyContainer.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,24 @@ private extension ModelMemberPropertyContainer {
208208
let names = patterns.compactMap { $0.as(IdentifierPatternSyntax.self)?.identifier.text }
209209

210210
return try names.compactMap { name -> ModelMemberProperty? in
211+
let attributes = variable.attributes
212+
211213
guard !variable.isLazyVar else {
212214
return nil
213215
}
214216
guard let type = variable.inferType else {
215217
throw ASTError("please declare property type: \(name)")
216218
}
217219

220+
// CodingKeyIgnored
221+
if let _ = attributes.first(where: { element in
222+
element.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.description == "CodingKeyIgnored"
223+
}) {
224+
return nil
225+
}
226+
218227
var mp = ModelMemberProperty(name: name, type: type)
219228
mp.modifiers = variable.modifiers
220-
let attributes = variable.attributes
221-
222229
// isOptional
223230
mp.isOptional = variable.isOptionalType
224231

Sources/CodableWrapperMacros/Plugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct CodableWrapperPlugin: CompilerPlugin {
77
Codable.self,
88
CodableSubclass.self,
99
CodingKey.self,
10+
CodingKeyIgnored.self,
1011
CodingNestedKey.self,
1112
CodingTransformer.self,
1213
]

Tests/CodableWrapperTests/ExampleTest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ enum Animal: String, Codable {
1414
case fish
1515
}
1616

17+
struct NonCodable {}
18+
1719
@Codable
1820
struct ExampleModel: Codable {
21+
@CodingKeyIgnored
22+
var nonCodable: NonCodable?
23+
1924
@CodingKey("aString")
2025
var stringVal: String = "scyano"
2126

0 commit comments

Comments
 (0)