Skip to content

Swift Compiler

Emi ships with a Swift compiler, so the same definitions that drive your Go backend and JavaScript front end can also power native iOS and macOS clients. The generated Swift is plain Foundation based code (it falls back to FoundationNetworking on Linux), so it does not pull in any third party dependency.

The Swift compiler generates type-safe request/response objects and an action wrapper per action, mirroring the behaviour of the other Emi compilers.

Terminal window
emi swift --path module.yaml --output ./Sources/Generated

Without --output, the generated virtual files are printed (as JSON) to the terminal so you can inspect them before writing anything to disk.

The full Swift compiler (emi swift) generates everything for a module. If you only need a slice of the output, two focused sub commands are available:

CommandDescription
emi swiftCompiles the entire Swift module (actions, DTOs, headers).
emi swift:dtoGenerates only the Swift DTO objects.
emi swift:headersGenerates the headers, usable in both client and server code.
FlagDescription
—pathPath of the Emi definition file (.yaml or .json) on disk.
—outputDirectory the generated Swift files are written to. Omit it to print to stdout.
—tagsComma separated compile features to add or remove.

For the postsModule from the OpenAPI page, emi swift generates an GetSinglePostAction.swift file that starts like this:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/**
Action to communicate with the action GetSinglePostAction
*/
struct GetSinglePostActionMeta {
let name: String = "GetSinglePostAction"
let url: String = "/posts/:id"
let method: String = "Get"
}
struct GetSinglePostActionResponse {
let statusCode: Int
let headers: [String: String]
let payload: Data?
// ...
}

Each action carries its metadata (name, URL, method) and a strongly typed response, so the calling code never has to hardcode routes or guess the response shape.