-
Notifications
You must be signed in to change notification settings - Fork 235
NewCodable macros #1808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kperryua
merged 15 commits into
swiftlang:experimental/new-codable
from
tevelee:experimental/new-codable
Mar 11, 2026
Merged
NewCodable macros #1808
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9bfd5f8
fix build issues
tevelee 4f6ed48
@JSONEncodable macro
tevelee 0ae71df
@CodingKey macro
tevelee 4d51b83
@JSONDecodable macro
tevelee 1f71da8
@JSONCodable macro
tevelee 44dd4f7
@CodableDefault macro
tevelee ae17f72
@CodableAlias macro
tevelee 11cac52
Code review: docs
tevelee 8864683
Code review: include missing field names
tevelee be0124b
Code review: CodingFields enum
tevelee 5d52714
Code review: separate CodingKeys conformances
tevelee b0fa117
Code review: permissive handling of unknown keys
tevelee 6c9d38d
Code review: rename alias macro
tevelee 97ea315
Code review: #expect(throws:)
tevelee 76e0a9e
Code review: use decodeEachField
tevelee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| /// Experimental NewCodable macro API. | ||
| /// | ||
| /// The macro spellings in this file are provisional and may evolve with the | ||
| /// macro-based Codable design. The per-property marker macros below are | ||
| /// especially likely to change as the feature set is refined. | ||
| @attached(member, names: named(CodingFields)) | ||
| @attached(extension, conformances: JSONEncodable, names: named(encode)) | ||
| public macro JSONEncodable() = #externalMacro(module: "NewCodableMacros", type: "JSONEncodableMacro") | ||
|
|
||
| /// Experimental macro that synthesizes `JSONDecodable` conformance. | ||
| @attached(member, names: named(CodingFields)) | ||
| @attached(extension, conformances: JSONDecodable, names: named(decode)) | ||
| public macro JSONDecodable() = #externalMacro(module: "NewCodableMacros", type: "JSONDecodableMacro") | ||
|
|
||
| /// Experimental macro that synthesizes both `JSONEncodable` and `JSONDecodable`. | ||
| @attached(member, names: named(CodingFields)) | ||
| @attached(extension, conformances: JSONEncodable, JSONDecodable, names: named(encode), named(decode)) | ||
| public macro JSONCodable() = #externalMacro(module: "NewCodableMacros", type: "JSONCodableMacro") | ||
|
|
||
| /// Experimental per-property marker macro for overriding the serialized key. | ||
| @attached(peer) | ||
| public macro CodingKey(_ name: String) = #externalMacro(module: "NewCodableMacros", type: "CodingKeyMacro") | ||
|
|
||
| /// Experimental per-property marker macro for supplying a default decoding value. | ||
| @attached(peer) | ||
| public macro CodableDefault<T>(_ value: T) = #externalMacro(module: "NewCodableMacros", type: "CodableDefaultMacro") | ||
|
|
||
| /// Experimental per-property marker macro for accepting alternate decoding keys. | ||
| @attached(peer) | ||
| public macro CodableAlias(_ names: String...) = #externalMacro(module: "NewCodableMacros", type: "CodableAliasMacro") | ||
This file contains hidden or 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,24 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
|
|
||
| public struct CodableAliasMacro: PeerMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingPeersOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| [] | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
|
|
||
| public struct CodableDefaultMacro: PeerMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingPeersOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| [] | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
|
|
||
| public struct CodingKeyMacro: PeerMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingPeersOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| [] | ||
| } | ||
| } |
This file contains hidden or 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,67 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
| import SwiftDiagnostics | ||
|
|
||
| public struct JSONCodableMacro { } | ||
|
|
||
| extension JSONCodableMacro: MemberMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingMembersOf declaration: some DeclGroupSyntax, | ||
| conformingTo protocols: [TypeSyntax], | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| guard declaration.is(StructDeclSyntax.self) else { | ||
| context.diagnose(.init( | ||
| node: node, | ||
| message: JSONEncodableDiagnostic.notAStruct | ||
| )) | ||
| return [] | ||
| } | ||
|
|
||
| let properties = extractStoredProperties(from: declaration.memberBlock, in: context) | ||
| guard !properties.isEmpty else { | ||
| return [] | ||
| } | ||
|
|
||
| return [makeCodingFieldsDecl(from: properties, kind: .coding)] | ||
| } | ||
| } | ||
|
|
||
| extension JSONCodableMacro: ExtensionMacro { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| attachedTo declaration: some DeclGroupSyntax, | ||
| providingExtensionsOf type: some TypeSyntaxProtocol, | ||
| conformingTo protocols: [TypeSyntax], | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [ExtensionDeclSyntax] { | ||
| let encodableExtensions = try JSONEncodableMacro.expansion( | ||
| of: node, | ||
| attachedTo: declaration, | ||
| providingExtensionsOf: type, | ||
| conformingTo: protocols, | ||
| in: context | ||
| ) | ||
| let decodableExtensions = try JSONDecodableMacro.expansion( | ||
| of: node, | ||
| attachedTo: declaration, | ||
| providingExtensionsOf: type, | ||
| conformingTo: protocols, | ||
| in: context | ||
| ) | ||
| return encodableExtensions + decodableExtensions | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.