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 8de0fbbb..a625defc 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 @@ -160,18 +160,29 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { is GoParser.PrimaryExprContext -> { CodeCall(NodeName = child.text) - when(val subchild = child.getChild(1)) { - // primaryExpr '.' IDENTIFIER + when (child.getChild(1)) { is TerminalNodeImpl -> { - // todo: verify child1 - val child1 = child.getChild(0) - val identifier = child.getChild(2).text + // primaryExpr '.' IDENTIFIER + val nodeName = when (val first = child.getChild(0)) { + is GoParser.OperandContext -> { + first.text + } + + is GoParser.PrimaryExprContext -> { + localVars.getOrDefault(first.text, first.text) + } + + else -> { + first.text + } + } CodeCall( - NodeName = child1.text, - FunctionName = identifier + NodeName = nodeName, + FunctionName = child.getChild(2).text ) } + else -> { CodeCall(NodeName = child.text) } @@ -196,7 +207,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() { override fun enterShortVarDecl(ctx: GoParser.ShortVarDeclContext?) { ctx?.identifierList()?.IDENTIFIER()?.forEach { - localVars[it.text] = "" + localVars[it.text] = ctx.expressionList()?.text ?: "" } } 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 67c3f6c2..ef33ab17 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 @@ -58,5 +58,17 @@ func main() { }""" val container = GoAnalyser().analysis(helloworldApi, "") println(Json.encodeToString(container)) + val codeFunction = container.DataStructures[0].Functions[0] + assertEquals(codeFunction.FunctionCalls[0].NodeName, "gin") + assertEquals(codeFunction.FunctionCalls[0].FunctionName, "Default") + + assertEquals(codeFunction.FunctionCalls[1].NodeName, "gin.Default()") + assertEquals(codeFunction.FunctionCalls[1].FunctionName, "GET") + + assertEquals(codeFunction.FunctionCalls[2].NodeName, "c") + assertEquals(codeFunction.FunctionCalls[2].FunctionName, "String") + + assertEquals(codeFunction.FunctionCalls[3].NodeName, "gin.Default()") + assertEquals(codeFunction.FunctionCalls[3].FunctionName, "Run") } }