Skip to content

Instantly share code, notes, and snippets.

@azonov
Created August 6, 2019 07:09
Show Gist options
  • Save azonov/67215014b21006c9b930c2cdc1549582 to your computer and use it in GitHub Desktop.
Save azonov/67215014b21006c9b930c2cdc1549582 to your computer and use it in GitHub Desktop.
@dynamicMemberLookup
@dynamicCallable
class Dsl<UrlsType> {
private let urls: UrlsType
private var components: [String] = []
init(urls: UrlsType) {
self.urls = urls
}
subscript(dynamicMember keyPath: KeyPath<UrlsType, String>) -> Dsl {
components.append(urls[keyPath: keyPath])
return self
}
func dynamicallyCall(withArguments args: [String]) -> Dsl {
components.append(contentsOf: args)
return self
}
func make(replacements: [String: String] = [:]) -> String {
return components.joined(separator: "/")
}
}
@azonov
Copy link
Author

azonov commented Aug 6, 2019

struct ProfileUrls {
    
    let user = "user"
    let posts = "posts"
}
let dsl = Dsl(urls: ProfileUrls())
let postId = "123"
dsl.user.posts(postId).make()// user/posts/123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment