本文整理汇总了Golang中golang.org/x/tools/go/types.Object.IsField方法的典型用法代码示例。如果您正苦于以下问题:Golang Object.IsField方法的具体用法?Golang Object.IsField怎么用?Golang Object.IsField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Object
的用法示例。
在下文中一共展示了Object.IsField方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: objectKind
func objectKind(obj types.Object) string {
switch obj := obj.(type) {
case *types.PkgName:
return "imported package name"
case *types.TypeName:
return "type"
case *types.Var:
if obj.IsField() {
return "field"
}
case *types.Func:
if obj.Type().(*types.Signature).Recv() != nil {
return "method"
}
}
// label, func, var, const
return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types."))
}
示例2: defKind
func defKind(obj types.Object) string {
switch obj := obj.(type) {
case *types.PkgName:
return definfo.Package
case *types.Const:
return definfo.Const
case *types.TypeName:
return definfo.Type
case *types.Var:
if obj.IsField() {
return definfo.Field
}
return definfo.Var
case *types.Func:
sig := obj.Type().(*types.Signature)
if sig.Recv() == nil {
return definfo.Func
} else {
return definfo.Method
}
default:
panic(fmt.Sprintf("unhandled obj type %T", obj))
}
}
示例3: NewDef
// NewDef creates a new Def.
func (g *Grapher) NewDef(obj types.Object, declIdent *ast.Ident) (*Def, error) {
// Find the AST node that declares this def.
var declNode ast.Node
_, astPath, _ := g.program.PathEnclosingInterval(declIdent.Pos(), declIdent.End())
for _, node := range astPath {
switch node.(type) {
case *ast.FuncDecl, *ast.GenDecl, *ast.ValueSpec, *ast.TypeSpec, *ast.Field, *ast.DeclStmt, *ast.AssignStmt:
declNode = node
goto found
}
}
found:
if declNode == nil {
return nil, fmt.Errorf("On ident %s at %s: no DeclNode found (using PathEnclosingInterval)", declIdent.Name, g.program.Fset.Position(declIdent.Pos()))
}
key, info, err := g.defInfo(obj)
if err != nil {
return nil, err
}
si := definfo.DefInfo{
Exported: info.exported,
PkgScope: info.pkgscope,
PkgName: obj.Pkg().Name(),
Kind: defKind(obj),
}
if typ := obj.Type(); typ != nil {
si.TypeString = typ.String()
if utyp := typ.Underlying(); utyp != nil {
si.UnderlyingTypeString = utyp.String()
}
}
switch obj := obj.(type) {
case *types.Var:
if obj.IsField() {
if fieldStruct, ok := g.structFields[obj]; ok {
if struct_, ok := fieldStruct.parent.(*types.Named); ok {
si.FieldOfStruct = struct_.Obj().Name()
}
}
}
case *types.Func:
sig := obj.Type().(*types.Signature)
if recv := sig.Recv(); recv != nil && recv.Type() != nil {
// omit package path; just get receiver type name
si.Receiver = strings.Replace(recv.Type().String(), obj.Pkg().Path()+".", "", 1)
}
}
return &Def{
Name: obj.Name(),
DefKey: key,
File: g.program.Fset.Position(declIdent.Pos()).Filename,
IdentSpan: makeSpan(g.program.Fset, declIdent),
DeclSpan: makeSpan(g.program.Fset, declNode),
DefInfo: si,
}, nil
}