ModifierGen

Generates chainable method code from Swift methods.

Github issues Github forks Github stars Github top language

image

Usage

You can download it from Release

USAGE: swchaingen --input-dir <input-dir> --output-dir <output-dir> [--overwrite]

OPTIONS:
  --input-dir <input-dir> input dir path
  --output-dir <output-dir>
                          output dir path for generated files
  --overwrite             overwrite files
  -h, --help              Show help information.

Example

If you have a code like the following,

struct Item {
    struct SubItem {
        var name: String
        var description: String

        public mutating func update(name: String) {
            self.name = name
        }
    }

    var name: String
    var description: String
    var subItems: [SubItem]

    mutating func update(name: String) {
        self.name = name
    }

    mutating func update(description: String) {
        self.description = description
    }
}

This tool is capable of generating the following code

extension Item {
    @_disfavoredOverload
    func update(name: String) -> Self {
        var new = self
        new.update(name: name)
        return new
    }

    @_disfavoredOverload
    func update(description: String) -> Self {
        var new = self
        new.update(description: description)
        return new
    }
}

extension Item.SubItem {
    @_disfavoredOverload
    public
    func update(name: String) -> Self {
        var new = self
        new.update(name: name)
        return new
    }
}

Then you can connect and call the methods as follows

let item = Item(name: "Hello", description: "hello", subItems: [])
        .update(name: "こんにちは")
        .update(description: "こんにちは")

License

swift-method-chainable is released under the MIT License. See LICENSE

GitHub

View Github