Skip to content

Commit

Permalink
fix: parse
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Nov 18, 2021
1 parent 857ddf8 commit 582f870
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
18 changes: 3 additions & 15 deletions src/main/kotlin/io/github/gnuf0rce/github/Parser.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
package io.github.gnuf0rce.github

import io.github.gnuf0rce.github.model.*
import io.ktor.http.*


internal val REPO_REGEX = """([\w-]+)/([\w-]+)""".toRegex()

fun GitHubClient.parse(url: Url): GitHubMapper {
return when {
url.encodedPath == "/" -> current()
url.encodedPath.startsWith("/users/") -> user(name = url.encodedPath.substringAfter("/users/"))
url.encodedPath.startsWith("/repos/") -> repo(full = url.encodedPath.substringAfter("/repos/"))
else -> throw IllegalArgumentException("无法解析 $url")
}
}
internal val FULL_REGEX = """([\w-]+)/([\w-]+)""".toRegex()

fun GitHubClient.repo(owner: String, repo: String) = GitHubRepo(owner = owner, repo = repo, github = this)

fun GitHubClient.repo(full: String): GitHubRepo {
val (owner, repo) = requireNotNull(REPO_REGEX.find(full)) { "Not Found FullName." }.destructured
val (owner, repo) = requireNotNull(FULL_REGEX.find(full)) { "Not Found FullName." }.destructured
return GitHubRepo(owner = owner, repo = repo, github = this)
}

fun GitHubClient.user(name: String) = GitHubUser(user = name, github = this)
fun GitHubClient.user(login: String) = GitHubUser(user = login, github = this)

fun GitHubClient.current() = GitHubCurrent(github = this)
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class GitHubSubscriber<T>(private val name: String, parent: CoroutineSc
}
}

protected open val regex: Regex? = REPO_REGEX
protected open val regex: Regex? = FULL_REGEX

fun add(id: String, contact: Long) {
check(regex?.matches(id) ?: true) { "$id not matches $regex" }
Expand Down

0 comments on commit 582f870

Please sign in to comment.