-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathKeyedVariable.swift
More file actions
78 lines (72 loc) · 2.94 KB
/
KeyedVariable.swift
File metadata and controls
78 lines (72 loc) · 2.94 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
/// A variable value containing data whether explicit decoding/encoding
/// is asked.
///
/// The `KeyedVariable` type forwards `Variable`
/// decoding/encoding, initialization implementations and only
/// decoding/encoding condition are customized.
struct KeyedVariable<Var: Variable>: ComposedVariable {
/// The customization options for `KeyedVariable`.
///
/// `KeyedVariable` uses the instance of this type,
/// provided during initialization, for customizing code generation.
struct Options {
/// Whether variable needs to be decoded/encoded.
///
/// `True` if `CodedAt` or `CodedIn` macro
/// is attached to variable.
let code: Bool
}
/// The value wrapped by this instance.
///
/// The wrapped variable's type data is
/// preserved and provided during initialization.
let base: Var
/// The options for customizations.
///
/// Options is provided during initialization.
let options: Options
}
extension KeyedVariable: ConditionalVariable where Var: ConditionalVariable {
/// Whether the variable is to be decoded.
///
/// Provides whether underlying variable value is to be decoded,
/// if provided code option is set as `false` otherwise `true`.
var decode: Bool? { options.code ? true : base.decode }
/// Whether the variable is to be encoded.
///
/// Provides whether underlying variable value is to be encoded,
/// if provided code option is set as `false` otherwise `true`.
var encode: Bool? { options.code ? true : base.encode }
}
extension KeyedVariable: PropertyVariable where Var: PropertyVariable {
/// Whether the variable type requires `Decodable` conformance.
///
/// Provides whether underlying variable type requires
/// `Decodable` conformance and provided code option
/// is set as `true`. Otherwise depends on whether
/// underlying variable is to be decoded.
var requireDecodable: Bool? {
options.code ? base.requireDecodable : base.decode
}
/// Whether the variable type requires `Encodable` conformance.
///
/// Provides whether underlying variable type requires
/// `Encodable` conformance and provided code option
/// is set as `true`. Otherwise depends on whether
/// underlying variable is to be encoded.
var requireEncodable: Bool? {
options.code ? base.requireEncodable : base.encode
}
}
extension KeyedVariable: InitializableVariable
where Var: InitializableVariable {
/// The initialization type of this variable.
///
/// Initialization type is the same as underlying wrapped variable.
typealias Initialization = Var.Initialization
}
extension KeyedVariable: NamedVariable where Var: NamedVariable {}
extension KeyedVariable: ValuedVariable where Var: ValuedVariable {}
extension KeyedVariable: AssociatedVariable where Var: AssociatedVariable {}
extension KeyedVariable: DefaultPropertyVariable
where Var: DefaultPropertyVariable {}