Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amend: refactor ResolveMultiPath enum to encapsulate url selection logic #1079

Merged
merged 1 commit into from
Nov 22, 2023
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
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
}
Loading