Skip to content

Commit fcdafa8

Browse files
committed
feat: added internally tagged enum support
1 parent c1097bb commit fcdafa8

34 files changed

+1308
-84
lines changed

Sources/CodableMacroPlugin/Attributes/Codable/Codable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Codable: Attribute {
3131
init?(from node: AttributeSyntax) {
3232
guard
3333
node.attributeName.as(IdentifierTypeSyntax.self)!
34-
.description == Self.name
34+
.name.text == Self.name
3535
else { return nil }
3636
self.node = node
3737
}

Sources/CodableMacroPlugin/Attributes/Codable/CodingKeys/CodingKeys.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct CodingKeys: PeerAttribute {
3131
init?(from node: AttributeSyntax) {
3232
guard
3333
node.attributeName.as(IdentifierTypeSyntax.self)!
34-
.description == Self.name
34+
.name.text == Self.name
3535
else { return nil }
3636
self.node = node
3737
}

Sources/CodableMacroPlugin/Attributes/Codable/IgnoreCodingInitialized.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct IgnoreCodingInitialized: PeerAttribute {
2424
init?(from node: AttributeSyntax) {
2525
guard
2626
node.attributeName.as(IdentifierTypeSyntax.self)!
27-
.description == Self.name
27+
.name.text == Self.name
2828
else { return nil }
2929
self.node = node
3030
}

Sources/CodableMacroPlugin/Attributes/CodedAs.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ struct CodedAs: PropertyAttribute {
1111
let node: AttributeSyntax
1212

1313
/// The alternate value expression provided.
14-
var expr: ExprSyntax {
15-
return node.arguments!
16-
.as(LabeledExprListSyntax.self)!.first!.expression
14+
var expr: ExprSyntax? {
15+
return node.arguments?
16+
.as(LabeledExprListSyntax.self)?.first?.expression
17+
}
18+
19+
/// The type to which to be decoded/encoded.
20+
///
21+
/// Used for enums with internal/adjacent tagging to decode
22+
/// the identifier to this type.
23+
var type: TypeSyntax? {
24+
return node.attributeName.as(IdentifierTypeSyntax.self)?
25+
.genericArgumentClause?.arguments.first?.argument
1726
}
1827

1928
/// Creates a new instance with the provided node.
@@ -26,7 +35,7 @@ struct CodedAs: PropertyAttribute {
2635
init?(from node: AttributeSyntax) {
2736
guard
2837
node.attributeName.as(IdentifierTypeSyntax.self)!
29-
.description == Self.name
38+
.name.text == Self.name
3039
else { return nil }
3140
self.node = node
3241
}
@@ -36,17 +45,35 @@ struct CodedAs: PropertyAttribute {
3645
///
3746
/// The following conditions are checked by the
3847
/// built diagnoser:
39-
/// * Attached declaration is an enum-case declaration.
4048
/// * Macro usage is not duplicated for the same declaration.
41-
/// * This attribute isn't used combined with `IgnoreCoding`
49+
/// * If macro has zero arguments provided:
50+
/// * Attached declaration is an enum declaration.
51+
/// * This attribute must be combined with `Codable`
52+
/// and `TaggedAt` attribute.
53+
/// * This attribute mustn't be combined with `CodedBy`
54+
/// attribute.
55+
/// * If macro has one argument provided:
56+
/// * Attached declaration is an enum-case declaration.
57+
/// * This attribute isn't used combined with `IgnoreCoding`
4258
/// attribute.
4359
///
4460
/// - Returns: The built diagnoser instance.
4561
func diagnoser() -> DiagnosticProducer {
4662
return AggregatedDiagnosticProducer {
47-
expect(syntaxes: EnumCaseDeclSyntax.self)
4863
cantDuplicate()
49-
cantBeCombined(with: IgnoreCoding.self)
64+
`if`(
65+
has(arguments: 1),
66+
AggregatedDiagnosticProducer {
67+
expect(syntaxes: EnumCaseDeclSyntax.self)
68+
cantBeCombined(with: IgnoreCoding.self)
69+
},
70+
else: AggregatedDiagnosticProducer {
71+
expect(syntaxes: EnumDeclSyntax.self)
72+
mustBeCombined(with: Codable.self)
73+
mustBeCombined(with: TaggedAt.self)
74+
cantBeCombined(with: CodedBy.self)
75+
}
76+
)
5077
}
5178
}
5279
}

Sources/CodableMacroPlugin/Attributes/CodedBy.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct CodedBy: PropertyAttribute {
2727
init?(from node: AttributeSyntax) {
2828
guard
2929
node.attributeName.as(IdentifierTypeSyntax.self)!
30-
.description == Self.name
30+
.name.text == Self.name
3131
else { return nil }
3232
self.node = node
3333
}
@@ -37,21 +37,36 @@ struct CodedBy: PropertyAttribute {
3737
///
3838
/// The following conditions are checked by the
3939
/// built diagnoser:
40-
/// * Attached declaration is a variable declaration.
41-
/// * Attached declaration is not a static variable
42-
/// declaration
43-
/// * Macro usage is not duplicated for the same
44-
/// declaration.
45-
/// * This attribute isn't used combined with
46-
/// `IgnoreCoding` attribute.
40+
/// * Macro usage is not duplicated for the same declaration.
41+
/// * If attached declaration is enum declaration:
42+
/// * This attribute must be combined with `Codable`
43+
/// and `TaggedAt` attribute.
44+
/// * This attribute mustn't be combined with `CodedAs`
45+
/// attribute.
46+
/// * If macro has one argument provided:
47+
/// * Attached declaration is a variable declaration.
48+
/// * Attached declaration is not a static variable
49+
/// declaration
50+
/// * This attribute isn't used combined with
51+
/// `IgnoreCoding` attribute.
4752
///
4853
/// - Returns: The built diagnoser instance.
4954
func diagnoser() -> DiagnosticProducer {
5055
return AggregatedDiagnosticProducer {
51-
expect(syntaxes: VariableDeclSyntax.self)
52-
attachedToNonStaticVariable()
5356
cantDuplicate()
54-
cantBeCombined(with: IgnoreCoding.self)
57+
`if`(
58+
isEnum,
59+
AggregatedDiagnosticProducer {
60+
mustBeCombined(with: Codable.self)
61+
mustBeCombined(with: TaggedAt.self)
62+
cantBeCombined(with: CodedAs.self)
63+
},
64+
else: AggregatedDiagnosticProducer {
65+
expect(syntaxes: VariableDeclSyntax.self)
66+
attachedToNonStaticVariable()
67+
cantBeCombined(with: IgnoreCoding.self)
68+
}
69+
)
5570
}
5671
}
5772
}

Sources/CodableMacroPlugin/Attributes/Default.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Default: PropertyAttribute {
2626
init?(from node: AttributeSyntax) {
2727
guard
2828
node.attributeName.as(IdentifierTypeSyntax.self)!
29-
.description == Self.name
29+
.name.text == Self.name
3030
else { return nil }
3131
self.node = node
3232
}

Sources/CodableMacroPlugin/Attributes/IgnoreCoding/IgnoreCoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct IgnoreCoding: PropertyAttribute {
2222
init?(from node: AttributeSyntax) {
2323
guard
2424
node.attributeName.as(IdentifierTypeSyntax.self)!
25-
.description == Self.name
25+
.name.text == Self.name
2626
else { return nil }
2727
self.node = node
2828
}

Sources/CodableMacroPlugin/Attributes/IgnoreCoding/IgnoreDecoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct IgnoreDecoding: PropertyAttribute {
2222
init?(from node: AttributeSyntax) {
2323
guard
2424
node.attributeName.as(IdentifierTypeSyntax.self)!
25-
.description == Self.name
25+
.name.text == Self.name
2626
else { return nil }
2727
self.node = node
2828
}

Sources/CodableMacroPlugin/Attributes/IgnoreCoding/IgnoreEncoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct IgnoreEncoding: PropertyAttribute {
2222
init?(from node: AttributeSyntax) {
2323
guard
2424
node.attributeName.as(IdentifierTypeSyntax.self)!
25-
.description == Self.name
25+
.name.text == Self.name
2626
else { return nil }
2727
self.node = node
2828
}

Sources/CodableMacroPlugin/Attributes/KeyPath/CodedAt.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct CodedAt: PropertyAttribute {
2222
init?(from node: AttributeSyntax) {
2323
guard
2424
node.attributeName.as(IdentifierTypeSyntax.self)!
25-
.description == Self.name
25+
.name.text == Self.name
2626
else { return nil }
2727
self.node = node
2828
}

0 commit comments

Comments
 (0)