Skip to content

Instantly share code, notes, and snippets.

@oharaandrew314
Last active March 17, 2022 15:47
Show Gist options
  • Select an option

  • Save oharaandrew314/a7fb56484e89049d716f104517319cf4 to your computer and use it in GitHub Desktop.

Select an option

Save oharaandrew314/a7fb56484e89049d716f104517319cf4 to your computer and use it in GitHub Desktop.
heaxagonal-clients-backcompat
fun main() {
val v1 = CatsDao.v1(ClientV1("http://catdocs.com"))
val v2 = CatsDao.v2(ClientV2("https://api.catdocs.com"))
val backCompat = CatsDao.backCompat(v1 = v1, v2 = v2)
val apiFeatureFlag = FeatureFlag.static(
treatments = mapOf("123" to "v1", "b692b290-40c4-4c0f-89ac-ed0a537e1736" to "v2"),
default = "v2"
)
val dao = CatsDao.toggled(apiFeatureFlag, v1 = v1, v2 = backCompat)
val ui = CatUi(dao)
println(ui.renderCatHtml("123"))
println(ui.renderCatHtml("b692b290-40c4-4c0f-89ac-ed0a537e1736"))
}
fun CatsDao.Companion.toggled(flag: FeatureFlag, v1: CatsDao, v2: CatsDao) = CatsDao { id ->
val treatment = flag[id]
val dao = if (treatment == "v1") v1 else v2
dao[id]
}
fun CatsDao.Companion.backCompat(v1: CatsDao, v2: CatsDao) = CatsDao { id ->
v2[id] ?: v1[id]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment