Skip to content

Commit

Permalink
feat(go): add handle for string type
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 25, 2022
1 parent 08caebb commit a715336
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,45 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {

fun buildParameters(parametersCtx: GoParser.ParametersContext?): Array<CodeProperty> {
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<String, String> {
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<String, String> {
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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -194,7 +225,8 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {

private fun parseArguments(child: GoParser.ArgumentsContext): Array<CodeProperty> {
return child.expressionList()?.expression()?.map {
CodeProperty(TypeValue = it.text, TypeType = "")
val (value, typetype) = processingStringType(it.text, "")
CodeProperty(TypeValue = value, TypeType = typetype)
}?.toTypedArray() ?: arrayOf()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
),
)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
"}")

Expand All @@ -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")
Expand Down

0 comments on commit a715336

Please sign in to comment.