From 6dea6bee786bb07e3243bdc4e139cee47b88c73e Mon Sep 17 00:00:00 2001 From: tangcent Date: Sat, 25 Apr 2020 20:48:29 +0800 Subject: [PATCH 1/2] chore: polish URL --- common-api/build.gradle | 13 +++ .../kotlin/com/itangcent/common/model/URL.kt | 103 +++++++++++++++--- .../kotlin/com/itangcent/http/RequestUtils.kt | 4 +- .../test/kotlin/com/itangcent/test/URLTest.kt | 34 ++++++ .../api/export/SpringRequestClassExporter.kt | 4 +- 5 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 common-api/src/test/kotlin/com/itangcent/test/URLTest.kt diff --git a/common-api/build.gradle b/common-api/build.gradle index 958bfabe7..bd6f000c1 100644 --- a/common-api/build.gradle +++ b/common-api/build.gradle @@ -39,6 +39,19 @@ dependencies { // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient compileOnly group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.10' + testCompile group: 'junit', name: 'junit', version: '4.12' + + // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api + testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2' + + // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine + testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2' + +} + + +test { + useJUnitPlatform() } compileKotlin { diff --git a/common-api/src/main/kotlin/com/itangcent/common/model/URL.kt b/common-api/src/main/kotlin/com/itangcent/common/model/URL.kt index ff60c5e10..26cf819ed 100644 --- a/common-api/src/main/kotlin/com/itangcent/common/model/URL.kt +++ b/common-api/src/main/kotlin/com/itangcent/common/model/URL.kt @@ -2,19 +2,58 @@ package com.itangcent.common.model import com.itangcent.common.utils.notNullOrEmpty import com.itangcent.http.RequestUtils +import java.util.* /** * Represents a url path. */ interface URL { + /** + * One url in [URL] + */ fun url(): String? + /** + * All url in [URL] as list. + */ fun urls(): List + /** + * Return true if only one url be contained at this [URL]. + */ fun single(): Boolean - fun contract(url: URL): URL + /** + * Concat the special url to this url. + * + * {""} concat {/b} ==> {/b} + * {"/a"} concat {"/b"} ==> {"/a/b"} + */ + fun concat(url: URL): URL + + /** + * Union the special url + * {""} union {"/b"} ==> {"","/b"} + * {"/a"} union {"/b"} ==> {"/a","/b"} + */ + fun union(url: URL): URL + + /** + * Returns a new [URL] of applying the given [transform] function + * to each url. + * + * @param transform function to transform url. + */ + fun map(transform: (String) -> String?): URL + + /** + * Returns a new [URL] of applying the given [transform] function + * to each url. + * + * @param transform function to transform url. + */ + fun flatMap(transform: (String) -> URL?): URL companion object { private val NULL_URL: NullURL = NullURL() @@ -67,10 +106,22 @@ private class NullURL : URL { return true } - override fun contract(url: URL): URL { + override fun concat(url: URL): URL { return url } + override fun union(url: URL): URL { + return url + } + + override fun map(transform: (String) -> String?): URL { + return URL.of(transform("")) + } + + override fun flatMap(transform: (String) -> URL?): URL { + return transform("") ?: URL.nil() + } + override fun toString(): String { return "" } @@ -93,12 +144,23 @@ private class SingleURL(private val url: String) : URL { return true } - override fun contract(url: URL): URL { - return when { - url == URL.nil() -> this - url.single() -> URL.of(RequestUtils.contractPath(this.url, url.url())) - else -> URL.of(url.urls().mapNotNull { RequestUtils.contractPath(this.url, it) }) - } + override fun concat(url: URL): URL { + return url.map { RequestUtils.contractPath(this.url, it) } + } + + override fun union(url: URL): URL { + val urls = LinkedList() + urls.add(this.url) + urls.addAll(url.urls()) + return URL.of(urls) + } + + override fun map(transform: (String) -> String?): URL { + return URL.of(transform(this.url)) + } + + override fun flatMap(transform: (String) -> URL?): URL { + return transform(url) ?: URL.nil() } override fun toString(): String { @@ -123,14 +185,23 @@ private class MultiURL(private val urls: List) : URL { return false } - override fun contract(url: URL): URL { - return when { - url == URL.nil() -> this - url.single() -> URL.of(this.urls().mapNotNull { RequestUtils.contractPath(it, url.url()) }) - else -> URL.of(this.urls().flatMap { prefixPath -> - url.urls().mapNotNull { RequestUtils.contractPath(prefixPath, it) } - }) - } + override fun concat(url: URL): URL { + return this.flatMap { URL.of(it).concat(url) } + } + + override fun map(transform: (String) -> String?): URL { + return URL.of(this.urls().mapNotNull(transform)) + } + + override fun flatMap(transform: (String) -> URL?): URL { + return this.urls.mapNotNull(transform).reduce { a, b -> a.union(b) } + } + + override fun union(url: URL): URL { + val urls = LinkedList() + urls.addAll(this.urls) + urls.addAll(url.urls()) + return URL.of(urls) } override fun toString(): String { diff --git a/common-api/src/main/kotlin/com/itangcent/http/RequestUtils.kt b/common-api/src/main/kotlin/com/itangcent/http/RequestUtils.kt index 465b0dcf8..b639406e2 100644 --- a/common-api/src/main/kotlin/com/itangcent/http/RequestUtils.kt +++ b/common-api/src/main/kotlin/com/itangcent/http/RequestUtils.kt @@ -38,8 +38,8 @@ object RequestUtils { } fun contractPath(pathPre: String?, pathAfter: String?): String? { - if (pathPre == null) return pathAfter - if (pathAfter == null) return pathPre + if (pathPre.isNullOrBlank()) return pathAfter + if (pathAfter.isNullOrBlank()) return pathPre return pathPre.removeSuffix("/") + "/" + pathAfter.removePrefix("/") } diff --git a/common-api/src/test/kotlin/com/itangcent/test/URLTest.kt b/common-api/src/test/kotlin/com/itangcent/test/URLTest.kt new file mode 100644 index 000000000..ef92c8b88 --- /dev/null +++ b/common-api/src/test/kotlin/com/itangcent/test/URLTest.kt @@ -0,0 +1,34 @@ +package com.itangcent.test + +import com.itangcent.common.model.URL +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class URLTest { + + @Test + fun testURLConcatAndUnion() { + val nil = URL.of() + val url1 = URL.of("/a") + val url2 = URL.of("/b") + val url3 = URL.of("/c", "/d") + assertEquals("/a", nil.concat(url1).toString()) + assertEquals("/a/b", url1.concat(url2).toString()) + assertEquals("/b/c,/b/d", url2.concat(url3).toString()) + assertEquals("/a", nil.union(url1).toString()) + assertEquals("/a,/b", url1.union(url2).toString()) + assertEquals("/b,/c,/d", url2.union(url3).toString()) + } + + @Test + fun testURLMap() { + val nil = URL.of() + val url1 = URL.of("/a") + val url2 = URL.of("/b") + val url3 = URL.of("/c", "/d") + assertEquals("/x", nil.map { "$it/x" }.toString()) + assertEquals("/a/x", url1.map { "$it/x" }.toString()) + assertEquals("/b/x", url2.map { "$it/x" }.toString()) + assertEquals("/c/x,/d/x", url3.map { "$it/x" }.toString()) + } +} \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/SpringRequestClassExporter.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/SpringRequestClassExporter.kt index 28c0d2d37..a81a174d3 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/SpringRequestClassExporter.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/SpringRequestClassExporter.kt @@ -31,7 +31,7 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() { var basePath: URL = findHttpPath(ctrlRequestMappingAnn) val prefixPath = ruleComputer!!.computer(ClassExportRuleKeys.CLASS_PREFIX_PATH, cls) if (prefixPath.notNullOrBlank()) { - basePath = URL.of(prefixPath).contract(basePath) + basePath = URL.of(prefixPath).concat(basePath) } val ctrlHttpMethod = findHttpMethod(ctrlRequestMappingAnn) @@ -191,7 +191,7 @@ open class SpringRequestClassExporter : AbstractRequestClassExporter() { } request.method = httpMethod - val httpPath = basePath.contract(findHttpPath(requestMapping)) + val httpPath = basePath.concat(findHttpPath(requestMapping)) requestHelper!!.setPath(request, httpPath) } From d0737427bff5414f6e54784c5cc34d0fac11e42f Mon Sep 17 00:00:00 2001 From: tangcent Date: Sat, 25 Apr 2020 23:08:12 +0800 Subject: [PATCH 2/2] release v1.9.2.183.0 --- IDEA_CHANGELOG.md | 10 +++++++++- build.gradle | 2 +- gradle.properties | 2 +- idea-plugin/parts/pluginChanges.html | 18 +++++++++++++----- .../src/main/resources/META-INF/plugin.xml | 2 +- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/IDEA_CHANGELOG.md b/IDEA_CHANGELOG.md index 5be859e8f..0589295f2 100644 --- a/IDEA_CHANGELOG.md +++ b/IDEA_CHANGELOG.md @@ -108,4 +108,12 @@ * new rule tool: localStorage [(#268)](https://github.com/tangcent/easy-api/pull/268) - \ No newline at end of file +* 1.9.0 ~ + + * fix: support `java`/`kt`/`scala` in all action. [(#271)](https://github.com/tangcent/easy-api/pull/271 + + * support new method 'method/declaration' of 'arg' [(#273)](https://github.com/tangcent/easy-api/pull/273) + + * opti: support rule `folder.name` [(#274)](https://github.com/tangcent/easy-api/pull/274 + + * support new rule `path.multi` [(#275)](https://github.com/tangcent/easy-api/pull/275) \ No newline at end of file diff --git a/build.gradle b/build.gradle index 053d6f096..9b6f61f81 100644 --- a/build.gradle +++ b/build.gradle @@ -1,2 +1,2 @@ group 'com.itangcent' -version '1.9.1.183.0' \ No newline at end of file +version '1.9.2.183.0' \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index bc380842e..3da0e28e4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.daemon=true org.gradle.workers.max=8 idea_version=2017.3.5 plugin_name=EasyApi -plugin_version=1.9.1.183.0 +plugin_version=1.9.2.183.0 descriptionFile=parts/pluginDescription.html changesFile=parts/pluginChanges.html \ No newline at end of file diff --git a/idea-plugin/parts/pluginChanges.html b/idea-plugin/parts/pluginChanges.html index 7d9a0d7ba..b61318057 100644 --- a/idea-plugin/parts/pluginChanges.html +++ b/idea-plugin/parts/pluginChanges.html @@ -1,13 +1,21 @@ -v1.9.1.183.0(2020-03-31) +v1.9.2.183.0(2020-04-25)
Full Changelog
    bug-fix: -
  • fix HttpRequest querys(#267) +
  • fix HttpRequfix: support `java`/`kt`/`scala` in all action(#271)
    enhancement: -
  • new rule tool: localStorage(#268) +
  • support new method 'method/declaration' of 'arg' (#273) +
  • + +
  • opti: support rule `folder.name` (#274) +
  • + +
  • opti: support new rule `path.multi` (#275)
\ No newline at end of file diff --git a/idea-plugin/src/main/resources/META-INF/plugin.xml b/idea-plugin/src/main/resources/META-INF/plugin.xml index a51f73eda..fa83379b7 100755 --- a/idea-plugin/src/main/resources/META-INF/plugin.xml +++ b/idea-plugin/src/main/resources/META-INF/plugin.xml @@ -1,7 +1,7 @@ com.itangcent.idea.plugin.easy-api EasyApi - 1.9.1.183.0 + 1.9.2.183.0 Tangcent