From 08caebb8cf11465e11eb81e91aa915909f1b373e Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 25 Nov 2022 18:57:01 +0800 Subject: [PATCH] feat(go): fix target error issues --- .../kotlin/chapi/ast/goast/GoAstListener.kt | 9 ------- .../chapi/ast/goast/GoFullIdentListener.kt | 23 ++++++++++++++-- .../kotlin/chapi/ast/goast/GoAnalyserTest.kt | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoAstListener.kt b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoAstListener.kt index 3bce9ee7..2096b1bf 100644 --- a/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoAstListener.kt +++ b/chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoAstListener.kt @@ -5,15 +5,6 @@ import chapi.ast.antlr.GoParserBaseListener import chapi.domain.core.CodeProperty open class GoAstListener : GoParserBaseListener() { - fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array { - return parametersCtx?.parameterDecl()?.map { - CodeProperty( - TypeValue = it.identifierList().text, - TypeType = it.type_().text - ) - }?.toTypedArray() ?: return arrayOf() - } - fun getStructNameFromReceiver(parameters: GoParser.ParametersContext?): String { parameters?.parameterDecl()?.forEach { var typeType = it.type_().text 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 fd392499..2fa3f9db 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 @@ -2,7 +2,6 @@ package chapi.ast.goast import chapi.ast.antlr.GoParser import chapi.domain.core.* -import org.antlr.v4.runtime.tree.ParseTree import org.antlr.v4.runtime.tree.TerminalNodeImpl /** @@ -72,6 +71,16 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { ) } + fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array { + return parametersCtx?.parameterDecl()?.map { + localVars[it.identifierList().text] = it.type_().text + CodeProperty( + TypeValue = it.identifierList().text, + TypeType = it.type_().text + ) + }?.toTypedArray() ?: return arrayOf() + } + override fun exitMethodDecl(ctx: GoParser.MethodDeclContext?) { val receiverName = this.getStructNameFromReceiver(ctx?.receiver()?.parameters()) currentFunction.addVarsFromMap(localVars) @@ -164,8 +173,18 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { * 2. lookup imports by [CodeContainer.Imports] */ private fun wrapTarget(nodeName: String): String { + var sourceNode = nodeName + if(sourceNode.startsWith("*")) { + sourceNode = sourceNode.substring(1) + } + + if (sourceNode.contains(".")) { + val split = sourceNode.split(".") + sourceNode = split.first() + } + codeContainer.Imports.forEach { - if (it.Source.endsWith("/${nodeName}")) { + if (it.Source.endsWith("/${sourceNode}")) { return it.Source } } 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 3918ecf0..528af365 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 @@ -38,6 +38,32 @@ func main() { ) assertEquals(Json.encodeToString(value), Json.encodeToString(expect)) } + @Test + fun analysisForRealword() { + val helloworld = """ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/chapi/pkg/middleware/auth" +) + +func installController(g *gin.Engine) *gin.Engine { + jwtStrategy, _ := newJWTAuth().(auth.JWTStrategy) + g.POST("/login", jwtStrategy.LoginHandler) + g.POST("/logout", jwtStrategy.LogoutHandler) + + return g +} +""" + 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") + } @Test @Disabled