Skip to content

Commit ca5d7e3

Browse files
committed
Barber sample
1 parent 900166b commit ca5d7e3

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

kotlin-codegen/src/main/kotlin/playground/utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fun shouldThrow(message: String, block: () -> Any?) {
55
block()
66
"Expected a throwable, but nothing was thrown."
77
} catch (e: Throwable) {
8-
if (e.message == message) {
8+
if ("$e".contains(message)) {
99
println("Test: got expected exception <<$message>>")
1010
null
1111
} else {

kotlin-jvm/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ apollo {
2424
dependencies {
2525
implementation(project(":kotlin-codegen"))
2626
// Keep dependencies sorted to minimize merge conflicts on pull-requests!
27+
implementation("app.cash.barber:barber:_")
2728
implementation ("com.github.ajalt:mordant:_")
2829
implementation("com.beust:klaxon:_")
2930
implementation("com.github.ajalt.clikt:clikt:_")
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
@file:Suppress("PackageDirectoryMismatch")
2+
3+
package playground.barber
4+
5+
import app.cash.barber.Barber
6+
import app.cash.barber.Barbershop
7+
import app.cash.barber.BarbershopBuilder
8+
import app.cash.barber.getBarber
9+
import app.cash.barber.locale.Locale.Companion.EN_US
10+
import app.cash.barber.models.Document
11+
import app.cash.barber.models.DocumentData
12+
import app.cash.barber.models.DocumentTemplate
13+
import playground.shouldBe
14+
import playground.shouldThrow
15+
import java.time.Instant
16+
import java.util.*
17+
18+
/***
19+
* cashapp/barber
20+
* A type safe Kotlin JVM library for building up localized, fillable, themed documents using Mustache templating.
21+
*
22+
* - [GitHub](https://github.com/cashapp/barber)
23+
* - [Documentation](https://cashapp.github.io/barber/)
24+
*/
25+
fun main() {
26+
println()
27+
println("# cashapp/barber")
28+
val recipientReceiptSmsDocumentTemplateEN_US = DocumentTemplate(
29+
fields = mapOf(
30+
"subject" to "{{sender}} sent you {{amount}}",
31+
"headline" to "New transfer",
32+
"short_description" to "{{sender}} sent {{amount}}",
33+
"secondary_button_url" to "{{cancelUrl}}"
34+
),
35+
source = RecipientReceipt::class,
36+
targets = setOf(TransactionalSmsDocument::class),
37+
locale = EN_US
38+
)
39+
40+
val barbershop = BarbershopBuilder()
41+
.installDocumentTemplate<RecipientReceipt>(recipientReceiptSmsDocumentTemplateEN_US)
42+
.installDocument<TransactionalSmsDocument>()
43+
.build()
44+
45+
// Get a Barber who knows how to render RecipientReceipt data into a TransactionalSmsDocument
46+
val recipientReceiptSms = barbershop.getBarber<RecipientReceipt, TransactionalSmsDocument>()
47+
val recept = RecipientReceipt(
48+
"Patrick", "14€", "http://example.com", Instant.now()
49+
)
50+
val transactionalSmsDocument: TransactionalSmsDocument = recipientReceiptSms.render(recept, EN_US)
51+
transactionalSmsDocument shouldBe TransactionalSmsDocument(
52+
"Patrick sent you 14€","New transfer", "Patrick sent 14€", null, null, null, "http://example.com"
53+
)
54+
55+
shouldThrow("DocumentData [unregistered] and corresponding DocumentTemplate(s) are not installed in Barbershop") {
56+
barbershop.getBarber<UnregisteredDocumentData, TransactionalSmsDocument>()
57+
}
58+
59+
}
60+
61+
// Define DocumentData
62+
data class RecipientReceipt(
63+
val sender: String,
64+
val amount: String,
65+
val cancelUrl: String,
66+
val deposit_expected_at: Instant
67+
) : DocumentData
68+
69+
data class TransactionalSmsDocument(
70+
val subject: String,
71+
val headline: String,
72+
val short_description: String,
73+
val primary_button: String?,
74+
val primary_button_url: String?,
75+
val secondary_button: String?,
76+
val secondary_button_url: String?
77+
) : Document
78+
79+
data class UnregisteredDocumentData(
80+
val id: Long
81+
): DocumentData

kotlin-jvm/src/main/kotlin/playground/_main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fun main() {
88
* Keep the list sorted to minimize merge conflicts on pull-requests!
99
*/
1010
playground.apollo.main()
11+
playground.barber.main()
1112
playground.clikt.main(arrayOf("--language", "FR", "--greeting", "Bonjour"))
1213
playground.clikt.main(arrayOf())
1314
playground.di.kodein.main()

versions.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ version.androidx.test.ext.junit=1.1.2
9999
## # available=1.1.3-alpha06
100100
## # available=1.1.3-beta01
101101

102+
version.app.cash.barber..barber=0.3.3
103+
102104
version.app.cash.contour..contour=1.1.0
103105

104106
version.ch.tutteli.atrium..atrium-fluent-en_GB=0.16.0

0 commit comments

Comments
 (0)