Swiftpack.co -  hainayanda/Builder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
hainayanda/Builder
Builder is Swift builder pattern library that utilize dynamicCallable and dynamicMemberLookup as its core.
.package(url: "https://github.com/hainayanda/Builder.git", from: "1.0.3")

Builder

Builder is Swift builder pattern library that utilize dynamicCallable and dynamicMemberLookup as its core.

codebeat badge build test SwiftPM Compatible Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 5.1 or higher (or 5.3 when using Swift Package Manager)
  • iOS 10 or higher

Only Swift Package Manager

  • macOS 10.10 or higher
  • tvOS 10 or higher

Installation

Cocoapods

Builder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Builder', '~> 1.0.3'

Swift Package Manager from XCode

  • Add it using XCode menu File > Swift Package > Add Package Dependency
  • Add https://github.com/hainayanda/Builder.git as Swift Package URL
  • Set rules at version, with Up to Next Major option and put 1.0.1 as its version
  • Click next and wait

Swift Package Manager from Package.swift

Add as your target dependency in Package.swift

dependencies: [
  .package(url: "https://github.com/hainayanda/Builder.git", .upToNextMajor(from: "1.0.2"))
]

Use it in your target as Builder

 .target(
  name: "MyModule",
  dependencies: ["Builder"]
)

Author

Nayanda Haberty, hainayanda@outlook.com

License

Impose is available under the MIT license. See the LICENSE file for more info.

Basic Usage

Using Builder is simple. You just need to pass the object you want to build and assign its writable property as its a function. Example:

If you have a class like this:

class MyObject {
    var string: String
    var int: Int
    var double: Double
    
    ...
    ...
}

you can build it like this:

let myObject: MyObject = builder(MyObject())
    .string("some string")
    .int(10)
    .double(1.2)
    .build()

Its even can assign property of property as deep as you need, as long the property is mutable:

let view: UIView = builder(SomObject())
    .string("some string")
    .int(10)
    .double(1.2)
    .subObject.string("some string")
    .subObject.int(10)
    .subObject.double(1.2)
    .build()

Buildable

There's a protocol named Buildable which declared like this:

public protocol Buildable {
    init()
}

its just to ensure you can call builder global function by only passing its Type:

let view: MyObjectImplementBuildable = builder(MyObjectImplementBuildable.self)
    .string("some string")
    .int(10)
    .double(1.2)
    .build()

Error Handling

Sometimes you can mistakenly try to assigning property that immutable. By default you can always see the debugPrint output like this:

Failed to assign property keypath of MyType with property type MyPropertyType because its not writable

But it can be forgotton and became a bug in the future. So you can always force it to throw fatalError if something like this is happes by assigning errorHandling on BuilderConfig:

BuilderConfig.errorHandling = .fatalErrorOnAssigningLet

it will create a fatal error when immutable property is assigned, so you can always fix it before it became a problem in the future. You can always handling it manually by assigning closure:

BuilderConfig.errorHandling = .manual { message in
    debugPrint("FIX THIS: \(message)")
}

Contribute

You know how, just clone and do pull request

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Dependencies

Release Notes

v1.0.3
2 weeks ago
  • Builder Config shared now public
  • Make Builder more protocol oriented

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics