-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathfunction.go
More file actions
160 lines (141 loc) · 3.77 KB
/
function.go
File metadata and controls
160 lines (141 loc) · 3.77 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright 2025 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"io"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/cucumber/godog"
"github.com/expr-lang/expr/builtin"
"github.com/linuxsuren/api-testing/pkg/render"
"github.com/spf13/cobra"
)
func createFunctionCmd() (c *cobra.Command) {
opt := &funcPrinterOption{}
c = &cobra.Command{
Use: "func",
Short: "Print all the supported functions",
RunE: opt.runE,
}
flags := c.Flags()
flags.StringVarP(&opt.feature, "feature", "", "", "The feature query")
return
}
type funcPrinterOption struct {
feature string
}
func (o *funcPrinterOption) runE(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
name := args[0]
filterAndPrint(cmd, name)
} else if o.feature != "" {
ctx := context.WithValue(cmd.Context(), render.ContextBufferKey, cmd.OutOrStdout())
suite := godog.TestSuite{
ScenarioInitializer: initializeScenario,
Options: &godog.Options{
Format: "pretty",
ShowStepDefinitions: false,
FeatureContents: []godog.Feature{{
Name: "test",
Contents: []byte(fmt.Sprintf(`Feature: title
Scenario: generate
Given %s`, o.feature)),
}},
DefaultContext: ctx,
Output: io.Discard,
},
}
if suite.Run() != 0 {
err = fmt.Errorf("failed to query the feature")
} else {
cmd.Println()
}
} else {
filterAndPrint(cmd, "")
}
return
}
func filterAndPrint(writer printer, name string) {
if name == "" {
writer.Println("All template functions:")
for name, fn := range render.FuncMap() {
writer.Println(name, reflect.TypeOf(fn))
}
} else {
if fn, ok := render.FuncMap()[name]; ok {
writer.Println(reflect.TypeOf(fn))
desc := FuncDescription(fn)
if desc != "" {
writer.Println(desc)
}
} else {
writer.Println("No such function")
}
}
writer.Println("")
writer.Println("All expr functions:")
for _, item := range builtin.Builtins {
if strings.Contains(item.Name, name) {
writer.Println(item.Name, reflect.TypeOf(item.Func))
}
}
}
func initializeScenario(ctx *godog.ScenarioContext) {
funcs := render.GetAdvancedFuncs()
for _, fn := range funcs {
if fn.GoDogExper != "" && fn.Generator != nil {
ctx.Step(fn.GoDogExper, fn.Generator)
}
}
}
// Get the name and path of a func
func FuncPathAndName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// Get the name of a func (with package path)
func FuncName(f interface{}) string {
splitFuncName := strings.Split(FuncPathAndName(f), ".")
return splitFuncName[len(splitFuncName)-1]
}
// Get description of a func
func FuncDescription(f interface{}) (desc string) {
fileName, _ := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).FileLine(0)
funcName := FuncName(f)
fset := token.NewFileSet()
// Parse src
parsedAst, err := parser.ParseFile(fset, fileName, nil, parser.ParseComments)
if err == nil {
pkg := &ast.Package{
Name: "Any",
Files: make(map[string]*ast.File),
}
pkg.Files[fileName] = parsedAst
importPath, _ := filepath.Abs("/")
myDoc := doc.New(pkg, importPath, doc.AllDecls)
for _, theFunc := range myDoc.Funcs {
if theFunc.Name == funcName {
desc = theFunc.Doc
break
}
}
}
return
}