@@ -2,14 +2,16 @@ package com.github.oldmegit.goframehelper.data.callUtil.orm
2
2
3
3
import com.github.oldmegit.goframehelper.data.callUtil.CallUtil
4
4
import com.goide.psi.*
5
+ import com.goide.psi.impl.GoElementImpl
6
+ import com.goide.psi.impl.GoKeyImpl
5
7
import com.goide.psi.impl.GoPsiUtil
8
+ import com.goide.psi.impl.GoValueImpl
6
9
import com.intellij.psi.PsiComment
7
10
import com.intellij.psi.PsiElement
8
11
import com.intellij.psi.ResolveState
9
12
import com.intellij.psi.util.PsiTreeUtil
10
13
import com.intellij.util.ObjectUtils
11
14
12
-
13
15
object OrmUtil : CallUtil() {
14
16
override fun getData (psiElement : PsiElement ): Map <String , Set <PsiElement >> {
15
17
return try {
@@ -105,24 +107,64 @@ object OrmUtil : CallUtil() {
105
107
106
108
// get table data by XXXColumns of type GoTypeSpec
107
109
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
+
108
129
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 >>()
111
132
112
- for (field in fields ) {
133
+ for (field in typeFields ) {
113
134
val fieldDefinition = field.firstChild
114
135
if (fieldDefinition !is GoFieldDefinition ) {
115
136
continue
116
137
}
117
138
if (field != null ) {
118
- data [fieldDefinition.text.camelToSnakeCase() ] = setOf (field)
139
+ typeData [fieldDefinition.text] = setOf (field)
119
140
} else {
120
- data [fieldDefinition.text.camelToSnakeCase() ] = hashSetOf()
141
+ typeData [fieldDefinition.text] = hashSetOf()
121
142
}
122
143
}
144
+
145
+ val data = mutableMapOf<String , Set <PsiElement >>()
146
+ for ((k, v) in varData) {
147
+ data[v] = typeData[k]!!
148
+ }
149
+
123
150
return data
124
151
}
125
152
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
+
126
168
// handle type GoAssignmentStatement
127
169
// for example:
128
170
// db = db.Xxx
@@ -184,9 +226,8 @@ object OrmUtil : CallUtil() {
184
226
return comment.drop(2 )
185
227
}
186
228
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(" \" " ) ? : " "
191
232
}
192
233
}
0 commit comments