-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_decl.go
50 lines (41 loc) · 1.19 KB
/
func_decl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package goastutil
import (
"fmt"
"go/ast"
)
type FuncDecl struct {
*ast.FuncDecl
}
func NewFuncDecl(decl *ast.FuncDecl) *FuncDecl {
return &FuncDecl{FuncDecl: decl}
}
func (ft *FuncDecl) DeclType() DeclType {
return FuncDeclType
}
func (ft *FuncDecl) Recv() *ReceiverExpr {
return NewReceiverExpr(ft.FuncDecl.Recv)
}
func (ft *FuncDecl) Name() *Ident {
return NewIdent(ft.FuncDecl.Name)
}
func (ft *FuncDecl) Body() *BlockStmt {
return NewBlockStmt(nil, ft.FuncDecl.Body)
}
func (ft *FuncDecl) Type() *FuncType {
if ft.FuncDecl.Recv == nil {
return NewFuncType(ft.FuncDecl.Type, ft.Name().String(), FnDecl)
}
return NewFuncType(ft.FuncDecl.Type, ft.Name().String(), FnMethod)
}
// String returns a string representation of the FuncDecl.
//
// It returns a string that represents the FuncDecl. If the FuncDecl's Recv
// field is nil, it formats the string as "func Name() Type Body". If the
// FuncDecl's Recv field is not nil, it formats the string as
// "func Recv.Name() Type Body".
func (ft *FuncDecl) String() string {
if ft.FuncDecl.Recv == nil {
return fmt.Sprintf("func %s %s", ft.Type(), ft.Body())
}
return fmt.Sprintf("func %s %s %s", ft.Recv().String(), ft.Type(), ft.Body())
}