-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCodable.swift
More file actions
59 lines (56 loc) · 2.24 KB
/
Copy pathCodable.swift
File metadata and controls
59 lines (56 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import SwiftSyntax
/// Attribute type for `Codable` macro-attribute.
///
/// Describes a macro that validates `Codable` macro usage
/// and generates `Codable` conformances and implementations.
///
/// This macro performs extension macro expansion depending on `Codable`
/// conformance of type:
/// * Extension macro expansion, to confirm to `Decodable` or `Encodable`
/// protocols depending on whether type doesn't already conform to `Decodable`
/// or `Encodable` respectively.
/// * Extension macro expansion, to generate custom `CodingKey` type for
/// the attached declaration named `CodingKeys` and use this type for
/// `Codable` implementation of both `init(from:)` and `encode(to:)`
/// methods.
/// * If attached declaration already conforms to `Codable` this macro expansion
/// is skipped.
package struct Codable: PeerAttribute {
/// The node syntax provided
/// during initialization.
let node: AttributeSyntax
/// Creates a new instance with the provided node
///
/// The initializer fails to create new instance if the name
/// of the provided node is different than this attribute.
///
/// - Parameter node: The attribute syntax to create with.
/// - Returns: Newly created attribute instance.
package init?(from node: AttributeSyntax) {
guard
node.attributeName.as(IdentifierTypeSyntax.self)!
.name.text == Self.name
else { return nil }
self.node = node
}
/// Builds diagnoser that can validate this macro
/// attached declaration.
///
/// Builds diagnoser that validates attached declaration
/// is `struct`/`class`/`enum`/`protocol` declaration
/// and macro usage is not duplicated for the same declaration.
///
/// - Returns: The built diagnoser instance.
func diagnoser() -> DiagnosticProducer {
return AggregatedDiagnosticProducer {
expect(
syntaxes: StructDeclSyntax.self, ClassDeclSyntax.self,
EnumDeclSyntax.self, ActorDeclSyntax.self,
ProtocolDeclSyntax.self
)
cantBeCombined(with: ConformDecodable.self)
cantBeCombined(with: ConformEncodable.self)
cantDuplicate()
}
}
}