-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDefault.swift
More file actions
93 lines (91 loc) · 3.37 KB
/
Copy pathDefault.swift
File metadata and controls
93 lines (91 loc) · 3.37 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
93
/// Provides a `default` value to be used when decoding fails and
/// when not initialized explicitly in memberwise initializer(s).
///
/// If the value is missing or has incorrect data type, the default value
/// will be used instead of throwing error and terminating decoding.
/// i.e. for a field declared as:
/// ```swift
/// @Default("some")
/// let field: String
/// ```
/// if empty json provided or type at `CodingKey` is different
/// ```json
/// { "field": 5 } // or {}
/// ```
/// the default value provided in this case `some` will be used as
/// `field`'s value.
///
/// - Parameter default: The default value to use.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The field type must confirm to `Codable` and
/// default value type `T` must be the same as field type.
@attached(peer)
@available(swift 5.9)
public macro Default<T>(_ default: T) =
#externalMacro(module: "MacroPlugin", type: "Default")
/// Provides a `default` value to be used when value is missing
/// and when not initialized explicitly in memberwise initializer(s).
///
/// If the value is missing , the default value will be used instead of
/// throwing error and terminating decoding. i.e. for a field declared as:
/// ```swift
/// @Default(ifMissing: "some")
/// let field: String
/// ```
/// if empty json provided
/// ```json
/// {}
/// ```
/// the default value provided in this case `some` will be used as
/// `field`'s value.
///
/// - Parameter default: The default value to use.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The field type must confirm to `Codable` and
/// default value type `T` must be the same as field type.
@attached(peer)
@available(swift 5.9)
public macro Default<T>(ifMissing default: T) =
#externalMacro(module: "MacroPlugin", type: "Default")
/// Provides different `default` values to be used for missing value
/// and decoding errors.
///
/// If the value is missing, the `missingDefault` value will be used,
/// while for incorrect data type, `errorDefault` value will be used,
/// instead of throwing error and terminating decoding.
/// i.e. for a field declared as:
/// ```swift
/// @Default(ifMissing: "some", forErrors: "another")
/// let field: String
/// ```
/// if type at `CodingKey` is different or empty json provided
/// ```json
/// { "field": 5 } // or {}
/// ```
/// the default value `some` and `another` will be used as
/// `field`'s value respectively.
///
/// - Parameters:
/// - missingDefault: The default value to use when value is missing.
/// - errorDefault: The default value to use for other errors.
///
/// - Note: This macro on its own only validates if attached declaration
/// is a variable declaration. ``Codable(commonStrategies:)`` macro uses this macro
/// when generating final implementations.
///
/// - Important: The field type must confirm to `Codable` and
/// default value type `T` must be the same as field type.
@attached(peer)
@available(swift 5.9)
public macro Default<T>(
ifMissing missingDefault: T, forErrors errorDefault: T
) =
#externalMacro(module: "MacroPlugin", type: "Default")