Skip to content

Commit

Permalink
amend: refactor ResolveMultiPath enum to encapsulate url selection lo…
Browse files Browse the repository at this point in the history
…gic (#1079)
  • Loading branch information
tangcent authored Nov 22, 2023
1 parent 9a44b34 commit 8d868fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,46 @@ import com.google.inject.Inject
import com.google.inject.Singleton
import com.itangcent.common.model.Request
import com.itangcent.common.model.URL
import com.itangcent.common.utils.longest
import com.itangcent.common.utils.shortest
import com.itangcent.idea.plugin.api.export.core.ClassExportRuleKeys
import com.itangcent.idea.plugin.api.export.core.ResolveMultiPath
import com.itangcent.idea.psi.resource
import com.itangcent.intellij.config.rule.RuleComputer

/**
* The `UrlSelector` class is responsible for selecting a single URL from a given request.
* When a request contains a single URL, it is returned as-is. However, when the request contains
* multiple URLs, a resolution strategy is determined based on specified rules, and this strategy is
* used to resolve the multiple URLs to a single URL.
*
* The resolution strategy is determined based on [ClassExportRuleKeys.PATH_MULTI], and the default
* strategy is [ResolveMultiPath.FIRST].
*/
@Singleton
class UrlSelector {

@Inject
private val ruleComputer: RuleComputer? = null

/**
* Selects a URL from a given request
*
* @param request The request containing the path (URL or URLs) to be resolved.
* @return The selected URL after applying the resolution strategy.
*/
fun selectUrls(request: Request): URL {
val pathInRequest = request.path ?: return URL.nil()

// If there's only one URL, return it
if (pathInRequest.single()) {
return pathInRequest
}

// Determine the resolution strategy based on a rule, defaulting to FIRST if the rule is not set
val pathMultiResolve = ruleComputer!!.computer(ClassExportRuleKeys.PATH_MULTI, request.resource()!!)?.let {
ResolveMultiPath.valueOf(it.uppercase())
} ?: ResolveMultiPath.FIRST

when (pathMultiResolve) {
ResolveMultiPath.ALL -> {
return pathInRequest
}

ResolveMultiPath.FIRST -> {
return URL.of(pathInRequest.urls().firstOrNull())
}

ResolveMultiPath.LAST -> {
return URL.of(pathInRequest.urls().lastOrNull())
}

ResolveMultiPath.LONGEST -> {
return URL.of(pathInRequest.urls().longest())
}

ResolveMultiPath.SHORTEST -> {
return URL.of(pathInRequest.urls().shortest())
}

else -> {
return pathInRequest
}
}
// Resolve and return the URL based on the determined strategy
return pathMultiResolve.resolve(pathInRequest)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
package com.itangcent.idea.plugin.api.export.core

import com.itangcent.common.model.URL
import com.itangcent.common.utils.longest
import com.itangcent.common.utils.shortest

/**
* This enumeration defines strategies to resolve multiple URLs to a single URL,
* based on different criteria like the first URL, last URL, longest URL, or shortest URL.
* It also provides a strategy to return all URLs as-is without any resolution.
*/
enum class ResolveMultiPath {
FIRST,
LAST,
LONGEST,
SHORTEST,
ALL
FIRST {
override fun resolve(url: URL): URL = URL.of(url.urls().firstOrNull())
},
LAST {
override fun resolve(url: URL): URL = URL.of(url.urls().lastOrNull())
},
LONGEST {
override fun resolve(url: URL): URL = URL.of(url.urls().longest())
},
SHORTEST {
override fun resolve(url: URL): URL = URL.of(url.urls().shortest())
},
ALL {
override fun resolve(url: URL): URL = url
};

/**
* resolve the given URL based on the implemented strategy
*/
abstract fun resolve(url: URL): URL
}

0 comments on commit 8d868fd

Please sign in to comment.