Skip to content

Commit

Permalink
Merge pull request #33 from takke/add_aturi
Browse files Browse the repository at this point in the history
Add `ATUri`
  • Loading branch information
uakihir0 authored Mar 4, 2024
2 parents dfecfa1 + 74cd3df commit ec09338
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
7 changes: 7 additions & 0 deletions core/src/commonMain/kotlin/work/socialhub/kbsky/util/ATUri.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package work.socialhub.kbsky.util

/**
* ATProtocol URI
* at://{did}/{recordType}/{key}
*/
data class ATUri(val did: String, val recordType: String, val rkey: String)
36 changes: 20 additions & 16 deletions core/src/commonMain/kotlin/work/socialhub/kbsky/util/ATUriParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,44 @@ package work.socialhub.kbsky.util
* at://{did}/{recordType}/{key}
*/
object ATUriParser {

/**
* Parse the URI of the record to get the DID.
* Parse the URI of the record.
*/
fun getDid(uri: String): String {
return uri
fun parse(uri: String): ATUri {
val array = uri
.split("://".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[1]
.split("/".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[0]
.toTypedArray()

return ATUri(
array.getOrNull(0) ?: "",
array.getOrNull(1) ?: "",
array.getOrNull(2) ?: ""
)
}

/**
* Parse the URI of the record to get the DID.
*/
fun getDid(uri: String): String {
return parse(uri).did
}

/**
* Parse the URI of the record to get the RecordType.
*/
fun getRecordType(uri: String): String {
return uri.split("://".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[1]
.split("/".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[1]
return parse(uri).recordType
}

/**
* Parse the URI of the record to get the rkey.
*/
fun getRKey(uri: String): String {
return uri.split("://".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[1]
.split("/".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()[2]
return parse(uri).rkey
}
}

0 comments on commit ec09338

Please sign in to comment.