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

support new rule field.mock #113

Merged
merged 1 commit into from
Dec 18, 2019
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 @@ -5,4 +5,6 @@ object Attrs {
const val COMMENT_ATTR = "@comment"

const val REQUIRED_ATTR = "@required"

const val MOCK_ATTR = "@mock"
}
4 changes: 2 additions & 2 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])


compile('com.itangcent:intellij-idea:0.1.9-SNAPSHOT') {
compile('com.itangcent:intellij-idea:0.1.91-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

compile('com.itangcent:intellij-kotlin-support:0.1.9-SNAPSHOT') {
compile('com.itangcent:intellij-kotlin-support:0.1.91-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 @@ -11,6 +11,7 @@ import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.intellij.file.DefaultLocalFileRepository
import com.itangcent.intellij.file.LocalFileRepository
import com.itangcent.intellij.jvm.PsiClassHelper
import com.itangcent.suv.http.ConfigurableHttpClientProvider
import com.itangcent.suv.http.HttpClientProvider

Expand All @@ -33,6 +34,8 @@ class YapiExportAction : ApiExportAction("Export Yapi") {

builder.bindInstance("file.save.default", "yapi.json")
builder.bindInstance("file.save.last.location.key", "com.itangcent.yapi.export.path")

builder.bind(PsiClassHelper::class) { it.with(YapiPsiClassHelper::class).singleton() }
}

override fun actionPerformed(actionContext: ActionContext, project: Project?, anActionEvent: AnActionEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ object ClassExportRuleKeys {
"method.default.http.method", StringRule::class,
StringRuleMode.SINGLE
)

val FIELD_MOCK: RuleKey<String> = SimpleRuleKey(
"field.mock", StringRule::class,
StringRuleMode.SINGLE
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMember
import com.intellij.psi.PsiMethod
import com.intellij.util.containers.ContainerUtil
import com.itangcent.common.model.Doc
Expand Down Expand Up @@ -476,6 +475,8 @@ class SuvApiExporter {
builder.bindInstance("file.save.default", "api.json")
builder.bindInstance("file.save.last.location.key", "com.itangcent.api.export.path")

builder.bind(PsiClassHelper::class) { it.with(YapiPsiClassHelper::class).singleton() }


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,15 @@ class YapiFormatter {
if (!required.isNullOrEmpty()) {
requireds = LinkedList()
}
var mocks: HashMap<String, Any?>? = null
try {
mocks = typedObject[Attrs.MOCK_ATTR] as HashMap<String, Any?>?
} catch (e: Throwable) {
}
typedObject.forEachValid { k, v ->
try {
val propertyInfo = parseObject(contactPath(path, k.toString()), v)
val key = k.toString()
val propertyInfo = parseObject(contactPath(path, key), v)

if (comment != null) {
val desc = comment[k]
Expand Down Expand Up @@ -383,9 +389,10 @@ class YapiFormatter {
}
}
if (required?.get(k) == true) {
requireds?.add(k.toString())
requireds?.add(key)
}
properties[k.toString()] = propertyInfo
mocks?.get(key)?.let { addMock(propertyInfo, it) }
properties[key] = propertyInfo
} catch (e: Exception) {
logger!!.warn("failed to mock for $path.$k")
}
Expand All @@ -396,14 +403,16 @@ class YapiFormatter {
}
}

//try read mock rules
val mockRules = readMockRules()
if (mockRules.isNotEmpty()) {
for (mockRule in mockRules) {
if (mockRule.pathPredict(path) &&
mockRule.typePredict(item["type"] as String?)) {
addMock(item, mockRule.mockStr)
break
if (!item.containsKey("mock")) {
//try read mock rules
val mockRules = readMockRules()
if (mockRules.isNotEmpty()) {
for (mockRule in mockRules) {
if (mockRule.pathPredict(path) &&
mockRule.typePredict(item["type"] as String?)) {
addMock(item, mockRule.mockStr)
break
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.itangcent.idea.plugin.api.export.yapi

import com.google.inject.Inject
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiType
import com.itangcent.common.constant.Attrs
import com.itangcent.common.utils.KV
import com.itangcent.idea.plugin.api.export.ClassExportRuleKeys
import com.itangcent.idea.utils.CustomizedPsiClassHelper
import com.itangcent.intellij.config.ConfigReader
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.PostConstruct
import com.itangcent.intellij.extend.toBoolean
import com.itangcent.intellij.jvm.SingleDuckType
import com.itangcent.intellij.psi.ContextSwitchListener

/**
* 1.support rule:["field.mock"]
*/
class YapiPsiClassHelper : CustomizedPsiClassHelper() {

@Inject(optional = true)
val configReader: ConfigReader? = null

var resolveProperty: Boolean = true

@PostConstruct
fun initYapiInfo() {
val contextSwitchListener: ContextSwitchListener? = ActionContext.getContext()
?.instance(ContextSwitchListener::class)
contextSwitchListener!!.onModuleChange {
val resolveProperty = configReader!!.first("field.mock.resolveProperty")
if (!resolveProperty.isNullOrBlank()) {
this.resolveProperty = resolveProperty.toBoolean() ?: true
}
}
}

@Suppress("UNCHECKED_CAST")
override fun afterParseFieldOrMethod(fieldName: String, fieldType: PsiType, fieldOrMethod: PsiElement, resourcePsiClass: PsiClass, duckType: SingleDuckType, option: Int, kv: KV<String, Any?>) {
super.afterParseFieldOrMethod(fieldName, fieldType, fieldOrMethod, resourcePsiClass, duckType, option, kv)

ruleComputer!!.computer(ClassExportRuleKeys.FIELD_MOCK, fieldOrMethod)
?.takeIf { !it.isBlank() }
?.let { if (resolveProperty) configReader!!.resolveProperty(it) else it }
?.let { mockInfo ->
var mockKV: KV<String, Any?>? = kv[Attrs.MOCK_ATTR] as KV<String, Any?>?
if (mockKV == null) {
mockKV = KV.create()
kv[Attrs.MOCK_ATTR] = mockKV
}
mockKV[fieldName] = mockInfo
}
}

@Suppress("UNCHECKED_CAST")
override fun afterParseFieldOrMethod(fieldName: String, fieldType: PsiType, fieldOrMethod: PsiElement, resourcePsiClass: PsiClass, option: Int, kv: KV<String, Any?>) {
super.afterParseFieldOrMethod(fieldName, fieldType, fieldOrMethod, resourcePsiClass, option, kv)

ruleComputer!!.computer(ClassExportRuleKeys.FIELD_MOCK, fieldOrMethod)
?.takeIf { !it.isBlank() }
?.let { if (resolveProperty) configReader!!.resolveProperty(it) else it }
?.let { required ->
var mockKV: KV<String, Any?>? = kv[Attrs.MOCK_ATTR] as KV<String, Any?>?
if (mockKV == null) {
mockKV = KV.create()
kv[Attrs.MOCK_ATTR] = mockKV
}
mockKV[fieldName] = required
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class RecommendConfigReader : ConfigReader {
return configReader!!.read(key)
}

override fun resolveProperty(property: String): String {
checkStatus()
return configReader!!.resolveProperty(property)
}

private fun checkStatus() {
while (loading) {
TimeUnit.MILLISECONDS.sleep(100)
Expand Down Expand Up @@ -126,7 +131,7 @@ class RecommendConfigReader : ConfigReader {

private const val config_name = ".recommend.easy.api.config"
// private const val config_version = ".recommend.easy.api.config.version"
private const val curr_version = "0.0.8.1"
private const val curr_version = "0.0.8.2"
//$version$content

private fun loadRecommendConfig(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.itangcent.intellij.psi.DefaultPsiClassHelper
/**
* 1.support rule:["field.required"]
*/
class CustomizedPsiClassHelper : DefaultPsiClassHelper() {
open class CustomizedPsiClassHelper : DefaultPsiClassHelper() {

@Suppress("UNCHECKED_CAST")
override fun afterParseFieldOrMethod(fieldName: String, fieldType: PsiType, fieldOrMethod: PsiElement, resourcePsiClass: PsiClass, duckType: SingleDuckType, option: Int, kv: KV<String, Any?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun Any?.toBoolean(): Boolean? {
if (this == null) return null
if (this is Boolean) return this
if (this is Number) return this.toInt() == 1
if (this is String) return this == "true"
if (this is String) return this == "true" || this == "1"
return null
}

Expand Down
3 changes: 3 additions & 0 deletions idea-plugin/src/main/resources/.recommend.easy.api.config
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ api.tag[groovy:it.containingClass().hasAnn("kotlin.Deprecated")]=deprecated
api.status[#undone]=undone
api.status[#todo]=undone

#[yapi_mock]
#yapi mock
field.mock=#mock


#[import_spring_properties]
Expand Down

This file was deleted.