(re)Creating a Ktor DSL

My mission at the moment is to understand the JetBrains Ktor HTTP library. So last week I wrote some tests for one of the example projects (https://ktor.io/docs/server-create-http-apis.html), with a view to comparing Ktor with http4k, the server-as-a-function alternative.

To be a fair comparison I want to make sure that I run the same tests against Ktor and http4k. This is something of a problem, as they have a quite different approach to in-memory testing.

This week then I’ll look at reimplementing part of Ktor’s domain specific language to target http4k, so that the same tests can be used to ensure that we implement the same behaviours. On the way we’ll learn how to implement a cheap and cheerful DSL in Kotlin.

In this episode

00:00:52 Reviewing our tests 00:02:13 I have a cunning plan 00:02:38 Substitute our DSL entry point 00:03:03 Change the receiver of the DSL body block 00:04:07 Add the properties we require in the DSL block 00:04:33 Adapt http4k HttpHandler to look like Ktor HttpClient 00:06:04 We can extend assertions to avoid other conversions 00:07:37 Rinse and repeat for other operations 00:08:27 Nesting DSLs 00:09:12 Add another context for the nesting 00:14:14 That turns out to compile 00:14:29 and our test source is unchanged 00:15:33 Review

This video is in a playlist of Ktor episodes (https://www.youtube.com/playlist?list=PL1ssMPpyqochUiQEM9PZ_P-9CbV1Il81B) and http4k (https://www.youtube.com/playlist?list=PL1ssMPpyqocg5TKqmiGWlvi3O5L8XPe8Q)

If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It’s about far more than just the syntax differences between the languages – it shows how to upgrade your thinking to a more functional style.

submitted by /u/dmcg
[link] [comments]