Skip to content

Commit

Permalink
feat: [generic] auto fix http method
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Feb 27, 2022
1 parent 2b9f7b7 commit 448c1fb
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion common-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation('com.itangcent:commons:1.1.4') {
implementation('com.itangcent:commons:1.1.51-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,31 @@ object HttpMethod {
const val OPTIONS = "OPTIONS"
const val TRACE = "TRACE"
const val HEAD = "HEAD"

val ALL_METHODS = arrayOf(GET, POST, DELETE, PUT,
PATCH, OPTIONS, TRACE, HEAD)

/**
* fix method to match the standard
*/
fun preferMethod(method: String): String {
if (method.isBlank()) {
return NO_METHOD
}
val standardMethod = method.toUpperCase()
if (ALL_METHODS.contains(standardMethod)) {
return standardMethod
}
for (me in ALL_METHODS) {
if (standardMethod.contains(".$me")) {
return me
}
}
for (me in ALL_METHODS) {
if (standardMethod.contains(me)) {
return me
}
}
return NO_METHOD
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.itangcent.common.constant

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

/**
* Test case for [HttpMethod]
*/
internal class HttpMethodTest {

@Test
fun preferMethod() {

Assertions.assertEquals("ALL", HttpMethod.preferMethod(""))

Assertions.assertEquals("GET", HttpMethod.preferMethod("GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("XXX.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("XXX.POST"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("POST.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("GET.POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("POST_GET"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("GET_POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET]"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("[POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET][POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[POST][GET]"))
}
}
8 changes: 4 additions & 4 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ dependencies {
// implementation fileTree(dir: 'libs', include: ['*.jar'])


implementation('com.itangcent:intellij-idea:1.1.4') {
implementation('com.itangcent:intellij-idea:1.1.51-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

implementation('com.itangcent:intellij-kotlin-support:1.1.4') {
implementation('com.itangcent:intellij-kotlin-support:1.1.51-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

// implementation('com.itangcent:intellij-scala-support:1.1.4') {
// implementation('com.itangcent:intellij-scala-support:1.1.51-SNAPSHOT') {
// exclude group: 'com.google.inject'
// exclude group: 'com.google.code.gson'
// }
Expand Down Expand Up @@ -122,7 +122,7 @@ dependencies {
// https://mvnrepository.com/artifact/org.mockito/mockito-inline
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.11.0'

testImplementation('com.itangcent:intellij-idea-test:1.1.4') {
testImplementation('com.itangcent:intellij-idea-test:1.1.51-SNAPSHOT') {
exclude group: 'com.nhaarman.mockitokotlin2', module: 'mockito-kotlin'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class GenericRequestClassExporter : RequestClassExporter() {
override fun processMethodParameter(
request: Request,
parameterExportContext: ParameterExportContext,
paramDesc: String?
paramDesc: String?,
) {

//RequestBody(json)
Expand Down Expand Up @@ -253,7 +253,7 @@ open class GenericRequestClassExporter : RequestClassExporter() {

private fun getUltimateComment(
paramDesc: String?,
parameterExportContext: ParameterExportContext
parameterExportContext: ParameterExportContext,
): String {
var ultimateComment = (paramDesc ?: "")
parameterExportContext.element().getType()?.let { duckType ->
Expand All @@ -275,7 +275,7 @@ open class GenericRequestClassExporter : RequestClassExporter() {
if (httpMethod == HttpMethod.NO_METHOD && ctrlHttpMethod != HttpMethod.NO_METHOD) {
httpMethod = ctrlHttpMethod!!
}
request.method = httpMethod
httpMethod?.let { request.method = HttpMethod.preferMethod(it) }

val httpPath = basePath.concat(URL.of(pathAndMethod.first))
requestBuilderListener.setPath(methodExportContext, request, httpPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ open class SuvApiExporter {
private val logger: Logger? = null

@Inject
private val actionContext: ActionContext? = null
private lateinit var actionContext: ActionContext

@Inject
private val classExporter: ClassExporter? = null
Expand All @@ -87,7 +87,7 @@ open class SuvApiExporter {
SelectedHelper.Builder()
.fileFilter { FileType.acceptable(it.name) }
.classHandle {
actionContext!!.checkStatus()
actionContext.checkStatus()
classExporter!!.export(it) { doc ->
docs.add(DocWrapper(doc))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RecommendConfigReader : ConfigReader, Initializable {
private val recommendConfigSettingsHelper: RecommendConfigSettingsHelper? = null

@Inject
private val contextSwitchListener: ContextSwitchListener? = null
private lateinit var contextSwitchListener: ContextSwitchListener

@Inject
private val logger: Logger? = null
Expand Down Expand Up @@ -77,7 +77,6 @@ class RecommendConfigReader : ConfigReader, Initializable {
override fun init() {

if (configReader is MutableConfigReader) {
contextSwitchListener!!.clear()
contextSwitchListener.onModuleChange { module ->
synchronized(this)
{
Expand Down

0 comments on commit 448c1fb

Please sign in to comment.