This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add DAP.parse * Change DAP to Dap (etc) to match web5-kt naming * use kotlin.test similar to web5-kt * update URN parsing
- Loading branch information
1 parent
e9ca147
commit f52a634
Showing
11 changed files
with
169 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package xyz.block.dap | ||
|
||
import java.util.regex.Pattern | ||
|
||
data class Dap( | ||
val handle: String, | ||
val domain: String | ||
) { | ||
override fun toString(): String { | ||
return "$PREFIX$handle$SEPARATOR$domain'" | ||
} | ||
|
||
companion object { | ||
const val PREFIX = "@" | ||
const val SEPARATOR = "/" | ||
|
||
private const val DAP_REGEX = """^$PREFIX([^$PREFIX$SEPARATOR]+)$SEPARATOR([^$PREFIX$SEPARATOR]+)$""" | ||
private val DAP_PATTERN = Pattern.compile(DAP_REGEX) | ||
|
||
fun parse(dap: String): Dap { | ||
val matcher = DAP_PATTERN.matcher(dap) | ||
matcher.find() | ||
if (!matcher.matches()) { | ||
throw InvalidDapException | ||
} | ||
val handle = matcher.group(1) | ||
val domain = matcher.group(2) | ||
|
||
return Dap(handle, domain) | ||
} | ||
} | ||
} | ||
|
||
object InvalidDapException : Throwable(message = "Invalid DAP") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package xyz.block.maddr.urn | ||
|
||
import java.util.regex.Pattern | ||
|
||
data class Urn( | ||
val nid: String, | ||
val nss: String | ||
) { | ||
private val urn: String = "$PREFIX$nid:$nss" | ||
|
||
override fun toString(): String { | ||
return urn | ||
} | ||
|
||
companion object { | ||
const val PREFIX = "urn:" | ||
private const val SEPARATOR = ":" | ||
|
||
private val URN_PATTERN = Pattern.compile("""^${PREFIX}([^$SEPARATOR]+)$SEPARATOR(.+)$""") | ||
|
||
fun parse(urn: String): Urn { | ||
val matcher = URN_PATTERN.matcher(urn) | ||
matcher.find() | ||
if (!matcher.matches()) { | ||
throw InvalidUrnException | ||
} | ||
val nid = matcher.group(1) | ||
val nss = matcher.group(2) | ||
|
||
return Urn(nid, nss) | ||
} | ||
} | ||
} | ||
|
||
object InvalidUrnException : Throwable("Invalid URN") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package xyz.block.dap | ||
|
||
import org.junit.jupiter.api.assertThrows | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DapTest { | ||
|
||
@Test | ||
fun testDapToString() { | ||
val dap = Dap("handle", "domain.com") | ||
assertEquals("@handle/domain.com'", dap.toString()) | ||
} | ||
|
||
@Test | ||
fun testParseDap() { | ||
val dap = Dap.parse("@handle/domain.com") | ||
assertEquals("handle", dap.handle) | ||
assertEquals("domain.com", dap.domain) | ||
} | ||
|
||
@Test | ||
fun testParseInvalidDaps() { | ||
val invalidDaps = listOf( | ||
"", | ||
"a", | ||
"@handle", | ||
"@handle/", | ||
"@handle@/domain.com", | ||
"@handle//domain.com", | ||
"@handle/@domain.com", | ||
"@handle/domain.com@", | ||
"@handle/domain.com/", | ||
) | ||
for (dap in invalidDaps) { | ||
val exception = assertThrows<InvalidDapException> { | ||
Dap.parse(dap) | ||
} | ||
assertEquals("Invalid DAP", exception.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package xyz.block.maddr.urn | ||
|
||
import org.junit.jupiter.api.assertThrows | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class UrnTest { | ||
|
||
@Test | ||
fun testUrnParsing() { | ||
val urn = Urn.parse("urn:nid:nss") | ||
assertEquals("urn:nid:nss", urn.toString()) | ||
assertEquals("nid", urn.nid) | ||
assertEquals("nss", urn.nss) | ||
} | ||
|
||
@Test | ||
fun testUrnParsingWithNssParts() { | ||
val urn = Urn.parse("urn:nid:nss1:nss2:nss3") | ||
assertEquals("urn:nid:nss1:nss2:nss3", urn.toString()) | ||
assertEquals("nid", urn.nid) | ||
assertEquals("nss1:nss2:nss3", urn.nss) | ||
} | ||
|
||
@Test | ||
fun testParseInvalidUrn() { | ||
val invalidUrns = listOf( | ||
"", | ||
"invalid:nid:nss", | ||
"invalid::nss", | ||
"invalid:nid:", | ||
"invalid:nid", | ||
"invalid:", | ||
"invalid::", | ||
) | ||
for (urn in invalidUrns) { | ||
val exception = assertThrows<InvalidUrnException> { | ||
Urn.parse(urn) | ||
} | ||
assertEquals("Invalid URN", exception.message) | ||
} | ||
} | ||
} |