Skip to content

Instantly share code, notes, and snippets.

@iThinker
Created July 15, 2023 04:29
Show Gist options
  • Save iThinker/402a11f6e81aadb56618ac331aed4e73 to your computer and use it in GitHub Desktop.
Save iThinker/402a11f6e81aadb56618ac331aed4e73 to your computer and use it in GitHub Desktop.
import UIKit
class PathBuilder {
private var components: [String] = []
private var pathBase: String
init(pathBase: String) {
self.pathBase = pathBase
}
func append(_ component: String) {
components.append(component)
}
func build() -> String {
defer { components.removeAll() }
return pathBase + "/" + components.joined(separator: "/")
}
}
class SubPathBase {
let builder: PathBuilder
init(builder: PathBuilder) {
self.builder = builder
}
}
class APIPathRoot: SubPathBase {
func config() -> Config {
builder.append("config")
return Config(builder: builder)
}
func users(id: Int) -> Users {
builder.append("users")
builder.append("\(id)")
return Users(builder: builder)
}
class Config: SubPathBase {
func `default`() -> String {
builder.append("default")
return builder.build()
}
}
class Users: SubPathBase {
func messages() -> Messages {
builder.append("messages")
return Messages(builder: builder)
}
func settings() -> Settings {
builder.append("settings")
return Settings(builder: builder)
}
class Messages: SubPathBase {
func `get`(id: Int) -> String {
builder.append("\(id)")
return builder.build()
}
func list() -> String {
builder.append("list")
return builder.build()
}
}
class Settings: SubPathBase {
func getPop() -> String {
builder.append("pop")
return builder.build()
}
func getImap() -> String {
builder.append("imap")
return builder.build()
}
}
}
}
let builder = PathBuilder(pathBase: "root")
let root = APIPathRoot(builder: builder)
print(root.config().default())
print(root.users(id: 5).messages().list())
print(root.users(id: 6).messages().get(id: 1337))
print(root.users(id: 4).settings().getPop())
print(root.users(id: 4).settings().getImap())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment