@@ -24,11 +24,28 @@ where Var: ConditionalVariable, Var.Generated: ConditionalVariableSyntax {
2424 /// False for variables with `@IgnoreCoding`
2525 /// and `@IgnoreEncoding` attributes.
2626 let encode : Bool ?
27- /// The condition expression based on which encoding is decided.
27+
28+ /// The condition based on which encoding is decided.
2829 ///
29- /// This expression accepts arguments from this variable and resolves
30+ /// This condition accepts arguments from this variable and resolves
3031 /// to either `true` or `false` based on which encoding is ignored.
31- let encodingConditionExpr : ExprSyntax ?
32+ let encodingCondition : Condition ?
33+
34+ /// Condition for determining when encoding should not be performed.
35+ enum Condition {
36+ /// Encoding is performed only if the provided expression evaluates
37+ /// to false.
38+ ///
39+ /// The expression accepts arguments from this variable and resolves
40+ /// to either `true` or `false` based on which encoding is decided.
41+ case `if`( ExprSyntax )
42+ /// Encoding is performed only if the provided expression evaluates
43+ /// to false.
44+ ///
45+ /// The expression accepts argument as the value that contains
46+ /// this property.
47+ case basedOn( ExprSyntax )
48+ }
3249 }
3350
3451 /// The value wrapped by this instance.
@@ -59,17 +76,27 @@ where Var: ConditionalVariable, Var.Generated: ConditionalVariableSyntax {
5976 to location: Var . CodingLocation
6077 ) -> Var . Generated {
6178 let syntax = base. encoding ( in: context, to: location)
62- guard
63- let conditionExpr = options. encodingConditionExpr
64- else { return syntax }
65- let args = self . conditionArguments
79+ guard let condition = options. encodingCondition else { return syntax }
80+
81+ let conditionExpr : ExprSyntax
82+ let args : LabeledExprListSyntax
83+ switch condition {
84+ case . if( let expr) :
85+ conditionExpr = expr
86+ args = self . conditionArguments
87+ case . basedOn( let expr) :
88+ conditionExpr = expr
89+ args = LabeledExprListSyntax {
90+ LabeledExprSyntax ( expression: " self " as ExprSyntax )
91+ }
92+ }
93+
6694 let returnList = TupleTypeElementListSyntax {
6795 for _ in args {
6896 TupleTypeElementSyntax ( type: " _ " as TypeSyntax )
6997 }
7098 }
71- let expr : ExprSyntax =
72- " !{ () -> ( \( returnList) ) -> Bool in \( conditionExpr) }()( \( args) ) "
99+ let expr : ExprSyntax = " !{ () -> ( \( returnList) ) -> Bool in \( conditionExpr) }()( \( args) ) "
73100 return syntax. adding ( condition: [ . init( expression: expr) ] )
74101 }
75102}
0 commit comments