Skip to content
A list of cool DSLs made with Swift 5.1’s @functionBuilder
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md Added SociableWeaver to readme Dec 16, 2019

README.md

awesome-function-builders

A list of cool DSLs made with Swift 5.1’s @functionBuilder

Currently, you instead have to use @_functionBuilder as it is a private implementation. This will change in the future, however.

Feel free to contribute if you make or find something awesome.

Contents

Guides

A list of helpful guides/tutorials on function builders

GraphQL

  • Artemis - Interact with GraphQL in Swift - not strings
Operation(.query) {
  Add(\.country, alias: "canada") {
    Add(\.name)
    Add(\.continent) {
      Add(\.name)
    }
  }.code("CA")
}
  • graphique - Experimental GraphQL query builders
query("") {
  hero {
    \.name
    lens(\.friends) {
      \.name
    }
  }
}
  • Graphiti - Swift GraphQL Schema/Type framework for macOS and Linux
Schema<StarWarsAPI, StarWarsStore> {
  Enum(Episode.self) {
    Value(.newHope)
      .description("Released in 1977.")
    Value(.empire)
      .description("Released in 1980.")
    Value(.jedi)
      .description("Released in 1983.")
  }
  .description("One of the films in the Star Wars Trilogy.")
  ...
}
Weave(.query) {
    Object(Post.self) {
        Field(Post.CodingKeys.title)

        Object(Post.CodingKeys.author) {
            Field(Author.CodingKeys.id)
            Field(Author.CodingKeys.name)
                .argument(key: "lastName", value: "Doe")
        }

        Object(Post.CodingKeys.comments) {
            Field(Comment.CodingKeys.id)
            Field(Comment.CodingKeys.content)
        }
        .argument(key: "filter", value: CommentFilter.recent)
    }
}
  • ...

HTML

  • HTML-DSL - A DSL for writing HTML in Swift
html(lang: "en-US") {
  body(customData: ["hello":"world"]) {
    article(classes: "readme", "modern") {
      h1 {
        "Hello World"
      }
    }
  }
}
  • Vaux - A HTML DSL library for Swift
html {
  body {
    link(url: url, label: "Google", inline: true)
  }
}
  • ...

Networking

  • swift-request - Declarative HTTP networking, designed for SwiftUI
Request {
  Url("https://jsonplaceholder.typicode.com/todo")
  Header.Accept(.json)
}
.onData { ... }
let myJson = Json {
  JsonProperty(key: "firstName", value: "Carson")
}
myJson["firstName"].string // "Carson"
  • ...

NSAttributedString

NSAttributedString {
  AText("Hello world")
    .font(.systemFont(ofSize: 24))
    .foregroundColor(.red)
  LineBreak()
  AText("with Swift")
     .font(.systemFont(ofSize: 20))
     .foregroundColor(.orange)
}
  • ...

SwiftUI

  • ControlFlowUI - A library that add control flow functionality to SwitUI, using the power of @functionBuilder and ViewBuilder
List(dogs.identified(by: \.name)) { dog in
  SwitchValue(dog) {
    CaseIs(Animal.self) { value in
      Text(value.species)
    }
    CaseIs(Dog.self) { value in
      Text(value.breed)
    }
  }
}
  • PathBuilder - Implementation of function builder for SwiftUI Path.
Path {
  Move(to: CGPoint(x: 50, y: 50))
  Line(to: CGPoint(x: 100, y: 100))
  Line(to: CGPoint(x: 0, y: 100))
  Close()
}
  • SwiftWebUI - A demo implementation of SwiftUI for the Web
VStack {
  Text("🥑🍞 #\(counter)")
    .padding(.all)
    .background(.green, cornerRadius: 12)
    .foregroundColor(.white)
    .tapAction(self.countUp)
}
  • ...

UIKit

  • BoxLayout - [WIP] SwiftUI's interface like AutoLayout DSL
BoxCenter {
  BoxVStack {
    BoxElement { toggleView }
    BoxEmpty()
      .frame(height: 20)
    if flag {
      BoxElement { top }
        .aspectRatio(ratio: CGSize(width: 1, height: 1))
    }
  }
}
Lego {
  ForIn(3...4) { x in
    Section(layout:FlowLayout(col: x)) {
      ForIn(0...(x + 3)) { y in
        ImageItem(value: UIImage(named: "\(y % 2)")!)
      }
    }
  }
}
  • Mockingbird - An experiment of implementing a UI layout and rendering framework inspired by SwiftUI
var content: some Node {
  VerticalStack {
    Repeated(0..<count) {
      Button(action: { self.count += 1 }) {
        BoxedText()
      }
    }
  }
  .cornerRadius(20)
  .animation(.spring)
}
  • TurtleBuilder - Turtle graphics made on the top of Swift's function builder. It allows you to use a Logo-like syntax to create and draw lines in your Swift project.
let turtle = Turtle {
  penDown()
  loop(9) {
      left(140)
      forward(30)
      left(-100)
      forward(30)
  }
  penUp()
}
  • ...

Other

  • ...
You can’t perform that action at this time.