Swift Macros have brought in a new wave of possibilities. The aim of this project is to curate a list of community-created Macros and associated learning resources.
A lot of use-cases my Sourcery covered before can now be implemented by Swift Macros.
assertMacro, an alternative to Apple's assertMacroExpansion that automatically snapshots macro expansions.Dive into Swift Macros with these WWDC sessions:
Other Apple Resources:
@AddCompletionHandler and @AddAsync.snake_case to lowerCamelCase.CodingKeys.Builder helper class, mimicking stored properties of the associated struct.@Buildable struct Person { ... }
let person = PersonBuilder(age: 42).build()
Codable implementation with following features:
CodingKey value declaration per variable, instead of requiring you to write for all fields.CodingKey values.Codable types.CodingKey values for fields and cases.CodingKey case style per type.String identifier or customize the identifier detection.Codable kit:
structs and enums!
.random generator, you can use @SampleBuilderItem to specify the type of data you want to generate for each property in your model (images, prices, names, etc.).getProperties static func which returns an array of all property names in your model.@Buildable Automates the marking of settable properties as @BuildableTracked, excluding those marked as @BuildableIgnored.@BuildableTracked Generates setter functions for each marked property, allowing fluent chaining of modifications.@BuildableIgnored Excludes properties from automatic setter function generation.copy function for structs, making it easy to duplicate a struct instance while updating its properties with new values, similar to what Kotlin data class offers.Sendable and Actor protocols.@Spyable annotation on a protocol, the macro generates a spy class
that implements the same interface as the protocol and keeps track of interactions with its methods and properties.@Mock macro on a protocol generates Mock class. You can stub methods and properties, verifying mock calls.@Parametrize(input: ["any", "values"]) to automatically generate new test methods for all parameters.@CasePathable produces key paths for enum cases, allowing enums to be used in dynamic member lookup and other generic algorithms.CaseEquatable. This allows you to compare only the case names—ignoring any associated values.@PublicInit: Generate public memberwise init@GenerateMock: Create a mock object for testing from a protocol@KeyPathIterable: Like CaseIterable but for available keypaths on a type@StaticMemberIterable: Like CaseIterable but for available static members on a typeobjc_getAssociatedObject/objc_setAssociatedObject.)@Partial,@Required: Constructs a type with all properties set to optional(@Partial) or require(@Required). This utility will return a type that represents all subsets of a given type.@Pick,@Omit: Constructs a type by picking(@Pick) or removing(@Omit) the set of specific properties keys (only string literal) from attached Type.@Exclude,@Extract: Constructs a type by excluding(@Exclude) or extracting(@Extract) from enum all cases.@Parameters: Constructs a tuple type from the types used in the parameters of a function type.@ReturnType: Constructs a type consisting of the return type of function.TemporaryVariable provide a macro #info {...}. It capture most function calls and assign them to temporary variables.Localizable A macro that produces variables and methods, for your localization files. From the enumeration keys.#Localize An inline (freestanding) macro that expands to LocalizedStringResource and works within Swift packages.#tk("Save Changes") automatically generates a semantic localization key instead of hardcoding strings.#tkm, ensuring correct key resolution inside SPM modules.SafeDecoding A macro that implements failable decoding via custom initializer; allows auto-conformance to `Decodable`` and per-property opt-out.@MemberwiseInit can more often automatically provide your intended memberwise init, while following the same safe-by-default semantics underlying Swift’s memberwise initializers.@PropertyTraced macro makes possible to trace from which method a property has been accessed.#symbol: Verify if SFSymbol is valid#URL: Validate and return URL Object#unwrap: Unwrap value.@AssociatedValues: Add variables to retrieve the associated values.@Singleton: Generate singleton code for struct and class.#SemanticVersion("1.2.3")Equatable conformance for classes and actors.@CaseIterable and @StaticMemberIterable power macros.Take part in this exciting evolution in Swift. Your contributions are most welcome!
(top 24 of 32)
84,740 followers · starred Jun 2023
842 followers · starred Jun 2023
692 followers · starred Jun 2023
426 followers · starred Sep 2023
Swift Macros have brought in a new wave of possibilities. The aim of this project is to curate a list of community-created Macros and associated learning resources.
A lot of use-cases my Sourcery covered before can now be implemented by Swift Macros.
assertMacro, an alternative to Apple's assertMacroExpansion that automatically snapshots macro expansions.Dive into Swift Macros with these WWDC sessions:
Other Apple Resources:
@AddCompletionHandler and @AddAsync.snake_case to lowerCamelCase.CodingKeys.Builder helper class, mimicking stored properties of the associated struct.@Buildable struct Person { ... }
let person = PersonBuilder(age: 42).build()
Codable implementation with following features:
CodingKey value declaration per variable, instead of requiring you to write for all fields.CodingKey values.Codable types.CodingKey values for fields and cases.CodingKey case style per type.String identifier or customize the identifier detection.Codable kit:
structs and enums!
.random generator, you can use @SampleBuilderItem to specify the type of data you want to generate for each property in your model (images, prices, names, etc.).getProperties static func which returns an array of all property names in your model.@Buildable Automates the marking of settable properties as @BuildableTracked, excluding those marked as @BuildableIgnored.@BuildableTracked Generates setter functions for each marked property, allowing fluent chaining of modifications.@BuildableIgnored Excludes properties from automatic setter function generation.copy function for structs, making it easy to duplicate a struct instance while updating its properties with new values, similar to what Kotlin data class offers.Sendable and Actor protocols.@Spyable annotation on a protocol, the macro generates a spy class
that implements the same interface as the protocol and keeps track of interactions with its methods and properties.@Mock macro on a protocol generates Mock class. You can stub methods and properties, verifying mock calls.@Parametrize(input: ["any", "values"]) to automatically generate new test methods for all parameters.@CasePathable produces key paths for enum cases, allowing enums to be used in dynamic member lookup and other generic algorithms.CaseEquatable. This allows you to compare only the case names—ignoring any associated values.@PublicInit: Generate public memberwise init@GenerateMock: Create a mock object for testing from a protocol@KeyPathIterable: Like CaseIterable but for available keypaths on a type@StaticMemberIterable: Like CaseIterable but for available static members on a typeobjc_getAssociatedObject/objc_setAssociatedObject.)@Partial,@Required: Constructs a type with all properties set to optional(@Partial) or require(@Required). This utility will return a type that represents all subsets of a given type.@Pick,@Omit: Constructs a type by picking(@Pick) or removing(@Omit) the set of specific properties keys (only string literal) from attached Type.@Exclude,@Extract: Constructs a type by excluding(@Exclude) or extracting(@Extract) from enum all cases.@Parameters: Constructs a tuple type from the types used in the parameters of a function type.@ReturnType: Constructs a type consisting of the return type of function.TemporaryVariable provide a macro #info {...}. It capture most function calls and assign them to temporary variables.Localizable A macro that produces variables and methods, for your localization files. From the enumeration keys.#Localize An inline (freestanding) macro that expands to LocalizedStringResource and works within Swift packages.#tk("Save Changes") automatically generates a semantic localization key instead of hardcoding strings.#tkm, ensuring correct key resolution inside SPM modules.SafeDecoding A macro that implements failable decoding via custom initializer; allows auto-conformance to `Decodable`` and per-property opt-out.@MemberwiseInit can more often automatically provide your intended memberwise init, while following the same safe-by-default semantics underlying Swift’s memberwise initializers.@PropertyTraced macro makes possible to trace from which method a property has been accessed.#symbol: Verify if SFSymbol is valid#URL: Validate and return URL Object#unwrap: Unwrap value.@AssociatedValues: Add variables to retrieve the associated values.@Singleton: Generate singleton code for struct and class.#SemanticVersion("1.2.3")Equatable conformance for classes and actors.@CaseIterable and @StaticMemberIterable power macros.Take part in this exciting evolution in Swift. Your contributions are most welcome!
(top 24 of 32)
84,740 followers · starred Jun 2023
842 followers · starred Jun 2023
692 followers · starred Jun 2023
426 followers · starred Sep 2023