-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added externally tagged enum support
- Loading branch information
1 parent
4bfeac3
commit c1097bb
Showing
59 changed files
with
2,770 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
@_implementationOnly import SwiftSyntax | ||
|
||
/// Attribute type for `CodedAs` macro-attribute. | ||
/// | ||
/// This type can validate`CodedAs` macro-attribute | ||
/// usage and extract data for `Codable` macro to | ||
/// generate implementation. | ||
struct CodedAs: PropertyAttribute { | ||
/// The node syntax provided | ||
/// during initialization. | ||
let node: AttributeSyntax | ||
|
||
/// The alternate 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)! | ||
.description == 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 an enum-case 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 { | ||
expect(syntaxes: EnumCaseDeclSyntax.self) | ||
cantDuplicate() | ||
cantBeCombined(with: IgnoreCoding.self) | ||
} | ||
} | ||
} | ||
|
||
extension Registration where Key == ExprSyntax?, Decl: AttributableDeclSyntax { | ||
/// Update registration with alternate value expression data. | ||
/// | ||
/// New registration is updated with value expression data that will be | ||
/// used for decoding/encoding, if provided and registration doesn't | ||
/// already have a value. | ||
/// | ||
/// - Returns: Newly built registration with value expression data. | ||
func checkForAlternateValue() -> Self { | ||
guard | ||
self.key == nil, | ||
let attr = CodedAs(from: self.decl) | ||
else { return self } | ||
return self.updating(with: attr.expr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Sources/CodableMacroPlugin/Diagnostics/Condition/DeclarationCondition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
@_implementationOnly import SwiftSyntax | ||
|
||
/// Validates provided syntax is of current declaration. | ||
/// | ||
/// Checks provided syntax type is of the syntax type `S`. | ||
struct DeclarationCondition<S: SyntaxProtocol>: DiagnosticCondition { | ||
/// Determines whether provided syntax passes validation. | ||
/// | ||
/// This type checks the provided syntax with current data for validation. | ||
/// Checks provided syntax type is of the syntax type `S`. | ||
/// | ||
/// - Parameter syntax: The syntax to validate. | ||
/// - Returns: Whether syntax passes validation. | ||
func satisfied(by syntax: some SyntaxProtocol) -> Bool { | ||
return syntax.is(S.self) | ||
} | ||
} | ||
|
||
extension Attribute { | ||
/// Whether declaration is `struct` declaration. | ||
/// | ||
/// Uses `DeclarationCondition` to check syntax type. | ||
var isStruct: DeclarationCondition<StructDeclSyntax> { .init() } | ||
/// Whether declaration is `class` declaration. | ||
/// | ||
/// Uses `DeclarationCondition` to check syntax type. | ||
var isClass: DeclarationCondition<ClassDeclSyntax> { .init() } | ||
/// Whether declaration is `enum` declaration. | ||
/// | ||
/// Uses `DeclarationCondition` to check syntax type. | ||
var isEnum: DeclarationCondition<EnumDeclSyntax> { .init() } | ||
/// Whether declaration is `actor` declaration. | ||
/// | ||
/// Uses `DeclarationCondition` to check syntax type. | ||
var isActor: DeclarationCondition<ActorDeclSyntax> { .init() } | ||
/// Whether declaration is `variable` declaration. | ||
/// | ||
/// Uses `DeclarationCondition` to check syntax type. | ||
var isVariable: DeclarationCondition<VariableDeclSyntax> { .init() } | ||
} |
Oops, something went wrong.