Skip to content

Commit

Permalink
feat(go): fix target error issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 25, 2022
1 parent 1c6a410 commit 08caebb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
9 changes: 0 additions & 9 deletions chapi-ast-go/src/main/kotlin/chapi/ast/goast/GoAstListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import chapi.ast.antlr.GoParserBaseListener
import chapi.domain.core.CodeProperty

open class GoAstListener : GoParserBaseListener() {
fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array<CodeProperty> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -72,6 +71,16 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
)
}

fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array<CodeProperty> {
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)
Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 26 additions & 0 deletions chapi-ast-go/src/test/kotlin/chapi/ast/goast/GoAnalyserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 08caebb

Please sign in to comment.