dsc6517 2015-11-20 21:27
浏览 82
已采纳

解析go src,尝试将* ast.GenDecl转换为types.Interface

I am trying to parse go source files that contain interfaces and find the interfaces defined methods / signatures. I am using ast to parse the file. I am able to get some high level declarations, such as *ast.GenDecl, but I am not able to get to the next level of determining if this type is an interface and what its methods are.

This is for a scaffolding type problem I am trying to solve where a user defines the interface for a service and a tool will build out the skeleton of the service

package main

import (
        "fmt"
        "go/ast"
        "go/parser"
        "go/token"
        "reflect"
)

func main() {
        fset := token.NewFileSet()
        f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service

        type ServiceInterface interface {
                        Create(NewServiceRequest) (JsonResponse, error)
                        Delete(DelServiceRequest) (JsonResponse, error)
        }`, 0)
        for _, v := range f.Decls {

            switch t := v.(type) {
            case *ast.FuncDecl:
                    fmt.Println("func ", t.Name.Name)
            case *ast.GenDecl:
                    switch x := t.Specs[0].(type) {
                    default:
                            fmt.Println(x, reflect.TypeOf(x))
                    }
            default:
                    fmt.Printf("skipping %t
", t)
            }

    }
}

Results in but I cant seem to find anything about the internals of interface declaration at all.

&{<nil> ServiceInterface 0x8202d8260 <nil>} *ast.TypeSpec
  • 写回答

1条回答 默认 最新

  • duan2477 2015-11-21 03:23
    关注

    When working with the AST, I find it helpful to dump an example using the spew package:

        fset := token.NewFileSet()
        f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service ....`)
        spew.Dump(f)
    

    I find it easy to write the required code from the spew output.

    Here's some code to get you started. It prints interface and method names:

    package main
    
    import (
      "fmt"
      "go/ast"
      "go/parser"
      "go/token"
    )
    
    func main() {
      fset := token.NewFileSet()
      f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service
    
        type ServiceInterface interface {
                        Create(NewServiceRequest) (JsonResponse, error)
                        Delete(DelServiceRequest) (JsonResponse, error)
        }`, 0)
      for _, x := range f.Decls {
        if x, ok := x.(*ast.GenDecl); ok {
            if x.Tok != token.TYPE {
                continue
            }
            for _, x := range x.Specs {
                if x, ok := x.(*ast.TypeSpec); ok {
                    iname := x.Name
                    if x, ok := x.Type.(*ast.InterfaceType); ok {
                        for _, x := range x.Methods.List {
                            if len(x.Names) == 0 {
                                continue
                            }
                            mname := x.Names[0].Name
                            fmt.Println("interface:", iname, "method:", mname)
    
                        }
                    }
                }
            }
        }
      }
    }
    

    http://play.golang.org/p/eNyB7O6FIc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)