-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDefault.swift
More file actions
92 lines (86 loc) · 3.26 KB
/
Default.swift
File metadata and controls
92 lines (86 loc) · 3.26 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@_implementationOnly import SwiftSyntax
/// Attribute type for `Default` macro-attribute.
///
/// This type can validate`Default` macro-attribute
/// usage and extract data for `Codable` macro to
/// generate implementation.
package struct Default: PropertyAttribute {
/// The node syntax provided
/// during initialization.
let node: AttributeSyntax
/// The default value expression provided.
var expr: ExprSyntax {
return node.arguments!
.as(LabeledExprListSyntax.self)!.first!.expression
}
/// 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.
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.
///
/// The following conditions are checked by the
/// built diagnoser:
/// * Attached declaration is a variable declaration.
/// * Attached declaration is not a static variable
/// declaration
/// * Macro usage is not duplicated for the same
/// declaration.
/// * This attribute isn't used combined with
/// `IgnoreCoding` attribute.
///
/// - Returns: The built diagnoser instance.
func diagnoser() -> DiagnosticProducer {
return AggregatedDiagnosticProducer {
attachedToUngroupedVariable()
attachedToNonStaticVariable()
cantDuplicate()
cantBeCombined(with: IgnoreCoding.self)
}
}
}
extension Registration
where
Decl: AttributableDeclSyntax, Var: PropertyVariable,
Var.Initialization == RequiredInitialization
{
/// The variable data with default expression
/// that output registration will have.
typealias DefOutput = AnyPropertyVariable<AnyRequiredVariableInitialization>
/// Update registration with default value if exists.
///
/// New registration is updated with default expression data that will be
/// used for decoding failure and memberwise initializer(s), if provided.
///
/// - Returns: Newly built registration with default expression data.
func addDefaultValueIfExists() -> Registration<Decl, Key, DefOutput> {
guard let attr = Default(from: self.decl)
else { return self.updating(with: self.variable.any) }
let newVar = self.variable.with(default: attr.expr)
return self.updating(with: newVar.any)
}
}
fileprivate extension PropertyVariable
where Initialization == RequiredInitialization {
/// Update variable data with the default value expression provided.
///
/// `DefaultValueVariable` is created with this variable as base
/// and default expression provided.
///
/// - Parameter expr: The default expression to add.
/// - Returns: Created variable data with default expression.
func with(default expr: ExprSyntax) -> DefaultValueVariable<Self> {
return .init(base: self, options: .init(expr: expr))
}
}