Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kiit-codes/src/commonMain/kotlin/kiit/codes/Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ sealed class Passed : Status {
val OMITTED =
Excluded(
"OMITTED",
"The item was left out.",
"The item was excluded from the result.",
origin = StatusConstants.KIIT,
)

Expand Down
99 changes: 99 additions & 0 deletions samples/sample-kotlin/src/main/kotlin/sample/SampleApp.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package sample

import kiit.codes.Checked
import kiit.codes.CodesToHttp
import kiit.codes.Err
import kiit.codes.Excluded
import kiit.codes.Failed
import kiit.codes.Information
import kiit.codes.Invalid
import kiit.codes.Passed
import kiit.codes.Pending
import kiit.codes.Rejected
import kiit.codes.Restricted
import kiit.codes.Status
import kiit.codes.StatusException
import kiit.codes.Succeeded
import kiit.codes.Unserved
import kotlin.random.Random

private val http = CodesToHttp()

fun main() {
test1()
test2()
test3()
}

fun test1() {
val service = UserService()

report("create alice", service.create("alice", "alice@example.com"))
Expand All @@ -34,3 +51,85 @@ private fun report(label: String, status: Status) {
}
println("$label -> ${status.name} ($outcome, http=${http.toCode(status)})")
}

fun test2() {
val value = Random.nextInt(1, 8)
val result: Status = when (value) {
1 -> Succeeded.SUCCESS
2 -> Pending.ACCEPTED
3 -> Excluded.SKIPPED
4 -> Information.NOTICE
5 -> Restricted.DENIED
6 -> Invalid.INVALID_VALUE
7 -> Rejected.RULE_VIOLATION
else -> Unserved.UNEXPECTED
}

val name = when(result) {
is Passed -> {
when(result) {
is Passed.Succeeded -> "Succeeded"
is Passed.Pending -> "Pending"
is Passed.Excluded -> "Excluded"
is Passed.Information -> "Information"
}
}
is Failed -> {
when(result) {
is Failed.Restricted -> "Restricted"
is Failed.Invalid -> "Invalid"
is Failed.Rejected -> "Rejected"
is Failed.Unserved -> "Unserved"
}
}
}
println("${name.padEnd(11)}: ${result.name}")
}


fun test3() {
val check1 = validatePhone("")
val check2 = validatePhone("12345678901")
val check3 = validatePhone("1111111111")
val check4 = validatePhone("1234567890")
val check5 = validatePhone("9876543210")
val check6 = validatePhone("1234567890", "admin")
println(check1)
println(check2)
println(check3)
println(check4)
println(check5)
println(check6)
}

fun validatePhone(phone: String, caller: String = "guest"): Checked {
return when {
phone.isEmpty() -> {
Checked.failure(
Invalid.INVALID_VALUE,
listOf(Err.on("phone", phone, "Too short")),
)
}
phone.length > 10 -> {
Checked.failure(
Invalid.INVALID_VALUE,
listOf(Err.on("phone", phone, "Too long")),
)
}
phone == "1111111111" -> {
Checked.failure(
Rejected.RULE_VIOLATION,
listOf(Err.on("phone", phone, "Reserved phone for testing")),
)
}
phone.startsWith("123") && caller != "admin" -> {
Checked.failure(
Restricted.DENIED,
listOf(Err.on("phone", phone, "Only admins can validate internal-use numbers")),
)
}
else -> {
Checked.success()
}
}
}
Loading