-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_type.go
81 lines (67 loc) · 2.25 KB
/
func_type.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package goastutil
import (
"fmt"
"go/ast"
"github.com/samber/lo"
)
const (
FnDecl = "fn-decl" // func fnDecl(...) {}
FnMethod = "fn-method" // fnMethod(...)
FnType = "fn-type" // type fnType = func(...)
FnExpr = "fn-expr" // _, err := fnExpr(...)
)
type FuncType struct {
name string
FnType string
typeParams *ast.FieldList
*ast.FuncType
}
func NewFuncType(expr *ast.FuncType, name string, fnType string) *FuncType {
return &FuncType{FuncType: expr, name: name, FnType: fnType}
}
func NewFuncTypeWithTypeParams(expr *ast.FuncType, name string, fnType string, typeParams *ast.FieldList) *FuncType {
return &FuncType{FuncType: expr, name: name, FnType: fnType, typeParams: typeParams}
}
func (ft *FuncType) ExprType() ExprType {
return FuncExprType
}
func (ft *FuncType) ParamsNames() []string {
return lo.Map(ft.FuncParamsList().Params(), func(x *Param, _ int) string {
return x.Name()
})
}
func (ft *FuncType) ParamsTypes() Exprs {
return lo.Map(ft.FuncParamsList().Params(), func(x *Param, _ int) Expr {
return x.Type()
})
}
func (ft *FuncType) TypeParamsList() *ParamsList {
return &ParamsList{FieldList: ft.typeParams}
}
func (ft *FuncType) FuncParamsList() *ParamsList {
return &ParamsList{FieldList: ft.FuncType.Params}
}
func (ft *FuncType) FuncResultsList() *ResultsList {
return &ResultsList{FieldList: ft.FuncType.Results}
}
func (ft *FuncType) String() string {
if ft.typeParams != nil {
switch ft.FnType {
case FnDecl:
return fmt.Sprintf("%s[%s](%s) (%s)", ft.name, ft.TypeParamsList(), ft.FuncParamsList(), ft.FuncResultsList())
case FnType:
return fmt.Sprintf("type %s[%s] = func(%s) (%s)", ft.name, ft.TypeParamsList(), ft.FuncParamsList(), ft.FuncResultsList())
default:
// TODO: [AST] Before supporting type params in func type, it would never valid.
return fmt.Sprintf("%s[%s](%s) (%s)", ft.name, ft.TypeParamsList(), ft.FuncParamsList(), ft.FuncResultsList())
}
}
switch ft.FnType {
case FnDecl:
return fmt.Sprintf("%s(%s) (%s)", ft.name, ft.FuncParamsList(), ft.FuncResultsList())
case FnMethod:
return fmt.Sprintf("%s(%s) (%s)", ft.name, ft.FuncParamsList(), ft.FuncResultsList())
default:
return fmt.Sprintf("type %s = func(%s) (%s)", ft.name, ft.FuncParamsList(), ft.FuncResultsList())
}
}