Skip to content

Swift macro implemented Decorator pattern for protocol

License

Notifications You must be signed in to change notification settings

Kristalev/DecoratableMacro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DecoratableMacro 🎨

Swift 6.1.0 License: MIT Swift Package Manager Compatible

Automatically generate decorator classes for Swift protocols using macros. Just add @Decoratable to your protocol — the macro will create a ready-to-use decorator class!

Features ✨

  • 🚀 Auto-generated decorators for Swift protocols
  • 🔒 Full access control support (public, internal, fileprivate, private)
  • 📦 2-minute integration via Swift Package Manager

Installation 📦

  1. Add the package to your Package.swift:
dependencies: [
    .package(
        url: "https://github.com/Kristalev/DecoratableMacro.git", 
        from: "1.0.0"
    )
]
  1. Add to your target:
.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "DecoratableMacro", package: "DecoratableMacro")
    ]
)

Usage 🛠️

  1. Annotate your protocol with @Decoratable:
import DecoratableMacro

@Decoratable
protocol Router {
    func push(viewController: UIViewController) 
}
  1. The macro generates:
class RouterDecorator: Router {
    private var decoree: any Router
    
    init(_ decoree: any Router) {
        self.decoree = decoree
    }
    
    func push(viewController: UIViewController) {
        decoree.push(viewController: viewController)
    }
}

Limitations ⚠️

  1. Works only with protocols (not classes/structs)
  2. Doesn't support generic protocols
  3. Not supported method modifier compatibility (async/throws)

License 📄

This project is MIT licensed. See LICENSE for details.

⭐ If you find this useful, please consider starring the repo! 🐞 Bugs & feature requests: Issues

About

Swift macro implemented Decorator pattern for protocol

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages