gpt4 book ai didi

go - 如何从* types.Named获取类型

转载 作者:行者123 更新时间:2023-12-01 22:15:57 25 4
gpt4 key购买 nike

我正在尝试查找以func作为第一个参数的context.Context的函数调用。

我已经能够执行下面显示的操作,但是我仍然坚持从*types.Named获取基础类型。我怎样才能做到这一点?

package main

import (
"bytes"
"context"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/singlechecker"
)

var Analyzer = &analysis.Analyzer{
Name: "addlint",
Doc: "reports integer additions",
Run: run,
}

func main() {
singlechecker.Main(Analyzer)
}

func funcHasContextContextAsFirstParam(pass *analysis.Pass, expr ast.Expr) bool {
t := pass.TypesInfo.TypeOf(expr)
if t == nil {
return false
}

bt, ok := t.Underlying().(*types.Signature)
if !ok {
return false
}

fmt.Printf("signature: %+v - %T\n", bt, bt)

params := bt.Params()
for i := 0; i < params.Len(); i++ {
v := params.At(i)
fmt.Printf("Type : %T\n", v.Type())

if named, ok := v.Type().(*types.Named); ok {
// fmt.Printf("named : %v - %T\n", named.Obj(), named.Obj())
fmt.Printf("named : %T\n", named)
fmt.Printf("named.Obj() : %T\n", named.Obj())

typ := named.Underlying()
fmt.Printf("typ: %T\n", typ.Underlying())

if _, ok = typ.(context.Context); ok {
fmt.Printf("context.Context type!\n")
}
}
}
return true
}

func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
be, ok := n.(*ast.CallExpr)
if !ok {
return true
}

fmt.Printf("call expression %+v\n", be)
funcHasContextContextAsFirstParam(pass, be.Fun)

return true
})
}

return nil, nil
}

那就是我得到的输出:
call expression &{Fun:foo Lparen:6160580 Args:[c 0xc0003c5780 0xc0003c57c0] Ellipsis:0 Rparen:6160596}
signature: func(ctx context.Context, n int, str string) - *types.Signature
Type : *types.Named
named : *types.Named
named.Obj() : *types.TypeName
typ: *types.Interface

最佳答案

我最终得到了这样的东西:

func funcHasContextContextAsFirstParam(pass *analysis.Pass, expr ast.Expr) bool {
t := pass.TypesInfo.TypeOf(expr)
if t == nil {
return false
}

bt, ok := t.Underlying().(*types.Signature)
if !ok {
return false
}

params := bt.Params()

if params.Len() < 1 {
return false
}

param := params.At(0)
named, ok := param.Type().(*types.Named)
if !ok {
return false
}

namedObj := named.Obj()
if namedObj.Name() != "Context" || namedObj.Pkg().Name() != "context" {
return false
}

return true
}

关于go - 如何从* types.Named获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60266582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com