Skip to content

Commit

Permalink
refactor(goast): enhance GoFullIdentListener to handle new expressions
Browse files Browse the repository at this point in the history
refactor(test): update GoFullIdentListenerTest for improved code analysis

- Updated `GoFullIdentListener` to include handling for new expression types, specifically for struct references with the '&'' operator.
- Amended `GoFullIdentListenerTest` to reflect changes in the code analysis, including the addition of new test setup and assertion adjustments.
  • Loading branch information
phodal committed Nov 12, 2024
1 parent 586467a commit ec368f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chapi.ast.goast

import chapi.ast.antlr.GoParser
import chapi.ast.antlr.GoParser.ExpressionContext
import chapi.ast.antlr.GoParser.PrimaryExprContext
import chapi.domain.core.*
import chapi.infra.Stack
Expand Down Expand Up @@ -451,10 +452,30 @@ class GoFullIdentListener(var fileName: String) : GoAstListener() {
is PrimaryExprContext -> {
return handleForPrimary(firstChild, isForLocalVar).orEmpty()
}

else -> {
if (firstChild.text == "&") {
val second = it.getChild(1)
/// new struct, like &http.Request{}
if (second is ExpressionContext) {
val expression = second.getChild(0)
if (expression is PrimaryExprContext) {
val typeDecl = expression.children.first().getChild(0)?.getChild(0)?.getChild(0)
if (typeDecl is GoParser.LiteralTypeContext) {
return typeDecl.getChild(0).text
}
}
}

return getValueFromPrintf(it)
}

return firstChild.text
}
}
}

return ""
return expressionListContext?.text ?: ""
}

override fun enterConstDecl(ctx: GoParser.ConstDeclContext?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,34 +510,46 @@ package user
import (
"context"
rcv1 "go-common/app/service/live/rc/api/liverpc/v1"
rcv1 "go-common/app/service/live/rc/api/liverpc/v1"
"net/http"
"strconv"
)
var (
_walletApiUrl = "/x/internal/livewallet/wallet/getAll"
)
// Dao user dao, wrap clients
type Dao struct {
walletUrl string
rcClient rcv1.AchvRPCClient
}
// New new user dao
func New(c *conf.Config) *Dao {
d := &Dao{
walletUrl: c.Host.LiveRpc + _walletApiUrl,
rcClient: dao.RcApi.V1Achv,
}
return d
}
// GetLiveAchieve get rc achieve by liverpc
func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err error) {
resp, err := d.rcClient.Userstatus(ctx, &rcv1.AchvUserstatusReq{})
if err != nil || resp == nil || resp.Data == nil {
log.Error("[dao.user|GetLiveAchieve] get rc achieve error(%v), uid(%d), resp(%v)", err, uid, resp)
return
}
achieve = resp.Data.Point
return
}
"""

val codeFile = GoAnalyser().analysis(code, "")
val functionCalls = codeFile.DataStructures[0].Functions[0].FunctionCalls

assertEquals(functionCalls.size, 2)
val functionCalls = codeFile.DataStructures.first().Functions.last().FunctionCalls

val getExecFunc = functionCalls[0]
println(getExecFunc)
assertEquals(getExecFunc.FunctionName, "Userstatus")
assertEquals(getExecFunc.Parameters.size, 2)

assertEquals(getExecFunc.NodeName, "Dao.rcClient")
assertEquals(getExecFunc.NodeName, "Dao")
}
}

0 comments on commit ec368f8

Please sign in to comment.