Skip to content

Commit fcf2cc8

Browse files
committed
fix: corrected ORM field name
Closes #24
1 parent bb421cd commit fcf2cc8

File tree

1 file changed

+51
-10
lines changed
  • src/main/kotlin/com/github/oldmegit/goframehelper/data/callUtil/orm

1 file changed

+51
-10
lines changed

src/main/kotlin/com/github/oldmegit/goframehelper/data/callUtil/orm/OrmUtil.kt

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package com.github.oldmegit.goframehelper.data.callUtil.orm
22

33
import com.github.oldmegit.goframehelper.data.callUtil.CallUtil
44
import com.goide.psi.*
5+
import com.goide.psi.impl.GoElementImpl
6+
import com.goide.psi.impl.GoKeyImpl
57
import com.goide.psi.impl.GoPsiUtil
8+
import com.goide.psi.impl.GoValueImpl
69
import com.intellij.psi.PsiComment
710
import com.intellij.psi.PsiElement
811
import com.intellij.psi.ResolveState
912
import com.intellij.psi.util.PsiTreeUtil
1013
import com.intellij.util.ObjectUtils
1114

12-
1315
object OrmUtil : CallUtil() {
1416
override fun getData(psiElement: PsiElement): Map<String, Set<PsiElement>> {
1517
return try {
@@ -105,24 +107,64 @@ object OrmUtil : CallUtil() {
105107

106108
// get table data by XXXColumns of type GoTypeSpec
107109
private fun getTableData(columnType: GoType): Map<String, Set<PsiElement>> {
110+
val goTypeDeclaration = PsiTreeUtil.findFirstParent(columnType) { e: PsiElement? ->
111+
e is GoTypeDeclaration
112+
} as GoTypeDeclaration
113+
val goVarDefinition = findGoVarDefinition(goTypeDeclaration)
114+
val varFields = PsiTreeUtil.findChildrenOfType(goVarDefinition, GoElementImpl::class.java)
115+
val varData = mutableMapOf<String, String>()
116+
for (field in varFields) {
117+
val fieldKey = field.firstChild
118+
if (fieldKey !is GoKeyImpl) {
119+
continue
120+
}
121+
if (field != null) {
122+
val k = PsiTreeUtil.findChildrenOfType(field, GoKeyImpl::class.java).first()
123+
val v = PsiTreeUtil.findChildrenOfType(field, GoValueImpl::class.java).first()
124+
val vText = v.text.removeQuotes()
125+
varData[k.text] = vText
126+
}
127+
}
128+
108129
val typeSpec = columnType.resolve(ResolveState.initial()) as GoTypeSpec
109-
val fields = PsiTreeUtil.findChildrenOfType(typeSpec, GoFieldDeclaration::class.java)
110-
val data = mutableMapOf<String, Set<PsiElement>>()
130+
val typeFields = PsiTreeUtil.findChildrenOfType(typeSpec, GoFieldDeclaration::class.java)
131+
val typeData = mutableMapOf<String, Set<PsiElement>>()
111132

112-
for (field in fields) {
133+
for (field in typeFields) {
113134
val fieldDefinition = field.firstChild
114135
if (fieldDefinition !is GoFieldDefinition) {
115136
continue
116137
}
117138
if (field != null) {
118-
data[fieldDefinition.text.camelToSnakeCase()] = setOf(field)
139+
typeData[fieldDefinition.text] = setOf(field)
119140
} else {
120-
data[fieldDefinition.text.camelToSnakeCase()] = hashSetOf()
141+
typeData[fieldDefinition.text] = hashSetOf()
121142
}
122143
}
144+
145+
val data = mutableMapOf<String, Set<PsiElement>>()
146+
for ((k, v) in varData) {
147+
data[v] = typeData[k]!!
148+
}
149+
123150
return data
124151
}
125152

153+
// find GoVarDefinition by PsiElement
154+
// for example:
155+
// var linkColumns = LinkColumns{
156+
// Id: "id",
157+
// Name: "name",
158+
//}
159+
private fun findGoVarDefinition(psiElement: PsiElement?): GoVarDeclaration? {
160+
val current = psiElement?.nextSibling
161+
return if (current is GoVarDeclaration) {
162+
current
163+
} else {
164+
findGoVarDefinition(current)
165+
}
166+
}
167+
126168
// handle type GoAssignmentStatement
127169
// for example:
128170
// db = db.Xxx
@@ -184,9 +226,8 @@ object OrmUtil : CallUtil() {
184226
return comment.drop(2)
185227
}
186228

187-
// camel to snake case
188-
private fun String.camelToSnakeCase(): String {
189-
val regex = "(?<=[a-zA-Z])[A-Z]".toRegex()
190-
return regex.replace(this) { "_${it.value}" }.lowercase()
229+
// remove first and last double quotes
230+
private fun String?.removeQuotes(): String {
231+
return this?.removePrefix("\"")?.removeSuffix("\"") ?: ""
191232
}
192233
}

0 commit comments

Comments
 (0)