From a715336808b36c5dc8f590248a80f0b81510ebc7 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 25 Nov 2022 19:06:54 +0800 Subject: [PATCH] feat(go): add handle for string type --- .../chapi/ast/goast/GoFullIdentListener.kt | 42 ++++++++++++++++--- .../kotlin/chapi/ast/goast/GoAnalyserTest.kt | 9 +++- .../chapi/ast/goast/GoMethodCallTest.kt | 4 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt index 2fa3f9db..f6e9ddbb 100644 --- a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt +++ b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoFullIdentListener.kt @@ -73,14 +73,45 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array { return parametersCtx?.parameterDecl()?.map { - localVars[it.identifierList().text] = it.type_().text + val (ident, typetype) = processingType(it) + + localVars[ident] = typetype CodeProperty( - TypeValue = it.identifierList().text, - TypeType = it.type_().text + TypeValue = ident, + TypeType = typetype ) }?.toTypedArray() ?: return arrayOf() } + private fun processingType(it: GoParser.ParameterDeclContext): Pair { + val typeValue = it.identifierList()?.text ?: "" + val typeType = it.type_()?.text ?: "" + + val pair = processingStringType(typeValue, typeType) + + return Pair(pair.first, pair.second) + } + + private fun processingStringType(typeValue: String, typeType: String): Pair { + var value = typeValue + var tyType = typeType + + if (value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length - 1) + + if (tyType == "") { + tyType = "string" + } + } + + // if match ident "." ident, should have a function call + if (value.matches(Regex("[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+"))) { + tyType = "FunctionCall" + } + + return Pair(value, tyType) + } + override fun exitMethodDecl(ctx: GoParser.MethodDeclContext?) { val receiverName = this.getStructNameFromReceiver(ctx?.receiver()?.parameters()) currentFunction.addVarsFromMap(localVars) @@ -174,7 +205,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { */ private fun wrapTarget(nodeName: String): String { var sourceNode = nodeName - if(sourceNode.startsWith("*")) { + if (sourceNode.startsWith("*")) { sourceNode = sourceNode.substring(1) } @@ -194,7 +225,8 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { private fun parseArguments(child: GoParser.ArgumentsContext): Array { return child.expressionList()?.expression()?.map { - CodeProperty(TypeValue = it.text, TypeType = "") + val (value, typetype) = processingStringType(it.text, "") + CodeProperty(TypeValue = value, TypeType = typetype) }?.toTypedArray() ?: arrayOf() } diff --git a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt index 528af365..f49d3d7b 100644 --- a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt +++ b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt @@ -30,7 +30,7 @@ func main() { CodeCall( NodeName = "fmt", FunctionName = "Println", - Parameters = arrayOf(CodeProperty(TypeValue = "\"hello world\"", TypeType = "")) + Parameters = arrayOf(CodeProperty(TypeValue = "hello world", TypeType = "string")) ) ), ) @@ -59,10 +59,15 @@ func installController(g *gin.Engine) *gin.Engine { val codeContainer = GoAnalyser().analysis(helloworld, "") val value = codeContainer.DataStructures[0] val firstCall = value.Functions[0].FunctionCalls[0] - println(Json.encodeToString(value)) assertEquals(firstCall.NodeName, "*gin.Engine") assertEquals(firstCall.Package, "github.com/gin-gonic/gin") + + assertEquals(firstCall.FunctionName, "POST") + assertEquals(firstCall.Parameters[0].TypeValue, "/login") + assertEquals(firstCall.Parameters[0].TypeType, "string") + assertEquals(firstCall.Parameters[1].TypeType, "FunctionCall") + assertEquals(firstCall.Parameters[1].TypeValue, "jwtStrategy.LoginHandler") } @Test diff --git a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoMethodCallTest.kt b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoMethodCallTest.kt index d2987287..6e8de0ea 100644 --- a/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoMethodCallTest.kt +++ b/chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoMethodCallTest.kt @@ -37,7 +37,7 @@ func main() { assertEquals(rGetCall.NodeName, "gin") assertEquals(rGetCall.FunctionName, "GET") assertEquals(rGetCall.Parameters.size, 2) - assertEquals(rGetCall.Parameters[0].TypeValue, "\"/\"") + assertEquals(rGetCall.Parameters[0].TypeValue, "/") assertEquals(rGetCall.Parameters[1].TypeValue, "func(c*gin.Context){c.String(http.StatusOK,\"hello world\")\n" + "}") @@ -48,7 +48,7 @@ func main() { assertEquals(callback.FunctionName, "String") assertEquals(callback.Parameters.size, 2) assertEquals(callback.Parameters[0].TypeValue, "http.StatusOK") - assertEquals(callback.Parameters[1].TypeValue, "\"hello world\"") + assertEquals(callback.Parameters[1].TypeValue, "hello world") val rRun = codeFile.DataStructures[0].Functions[0].FunctionCalls[3] assertEquals(rRun.NodeName, "gin")