Skip to content

Commit

Permalink
feat: add support to wrap target
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 18, 2022
1 parent e15fa0c commit ac664f6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
}

override fun enterMethodDecl(ctx: GoParser.MethodDeclContext?) {

currentFunction = CodeFunction(
Name = ctx!!.IDENTIFIER().text,
MultipleReturns = buildReturnTypeFromSignature(ctx.signature()),
Expand Down Expand Up @@ -125,25 +124,40 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
when (val firstChild = ctx!!.getChild(0)) {
is GoParser.PrimaryExprContext -> {
firstChild.getChild(1)?.let {
this.buildPrimaryExprCtx(firstChild)
this.parsePrimaryExprCtx(firstChild)
}
}
}
}

private fun buildPrimaryExprCtx(primaryExprCtx: GoParser.PrimaryExprContext) {
private fun parsePrimaryExprCtx(primaryExprCtx: GoParser.PrimaryExprContext) {
when (val child = primaryExprCtx.getChild(1)) {
is GoParser.ArgumentsContext -> {
val codeCall = codeCallFromExprList(primaryExprCtx)
codeCall.Parameters = parseArguments(child)
codeCall.Package = wrapTarget(codeCall.NodeName)

currentFunction.FunctionCalls += codeCall
}

else -> {
println("${child.javaClass} not implemented")
println("${child.javaClass} not implemented ${child.text}")
}
}
}

/**
* 1. lookup local vars
* 2. lookup imports
*/
private fun wrapTarget(nodeName: String): String {
codeContainer.Imports.forEach {
if (it.Source.endsWith("/${nodeName}")) {
return it.Source
}
}

return ""
}

private fun parseArguments(child: GoParser.ArgumentsContext): Array<CodeProperty> {
Expand Down Expand Up @@ -194,7 +208,7 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
}

else -> {
println("${child.javaClass} not implemented")
println("${child.javaClass} not implemented -> ${child.text}")
CodeCall(NodeName = child.text)
}
}
Expand Down
41 changes: 41 additions & 0 deletions chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoMethodCallTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package chapi.ast.goast

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class GoMethodCallTest {
@Test
fun shouldIdentifyMethodCall() {
val code = """package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello world")
})
r.Run()
}"""

val codeFile = GoAnalyser().analysis(code, "")
assertEquals(codeFile.DataStructures.size, 1)
assertEquals(codeFile.DataStructures[0].Functions.size, 1)
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls.size, 4)

assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[0].NodeName, "gin")
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[0].FunctionName, "Default")
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[0].Package, "github.com/gin-gonic/gin")

assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[1].NodeName, "gin.Default()")
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[2].NodeName, "c")
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[2].Parameters.size, 2)
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[3].NodeName, "gin.Default()")
assertEquals(codeFile.DataStructures[0].Functions[0].FunctionCalls[3].FunctionName, "Run")
}
}
16 changes: 16 additions & 0 deletions chapi-ast-go/src/test/resources/gin/helloworld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

func main() {
r := gin.Default()

r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello world")
})

r.Run()
}

0 comments on commit ac664f6

Please sign in to comment.