-
Notifications
You must be signed in to change notification settings - Fork 66
/
reflect_interface.go
158 lines (140 loc) · 4.55 KB
/
reflect_interface.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
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
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package codegen
import (
"bytes"
"encoding/gob"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"text/template"
"github.com/golang/mock/mockgen/model"
"github.com/pkg/errors"
)
type reflectData struct {
PathAliasMap map[string]string
PathSymbolMap map[string]string
}
// ReflectInterface uses reflection to obtain interface information for each path symbol pair in the pathSympolMap
// projRoot is the root dir where mockgen is installed as a vendor package
func ReflectInterface(projRoot string, pathSymbolMap map[string]string) (map[string]*model.Package, error) {
// We use TempDir instead of TempFile so we can control the filename.
tmpDir, err := ioutil.TempDir("./", "gomock_reflect_")
if err != nil {
return nil, err
}
defer func() { _ = os.RemoveAll(tmpDir) }()
const progSource = "prog.go"
var progBinary = "prog.bin"
if runtime.GOOS == "windows" {
// Windows won't execute a program unless it has a ".exe" suffix.
progBinary += ".exe"
}
// Generate program
paths := make(map[string]bool, len(pathSymbolMap))
for p := range pathSymbolMap {
paths[p] = true
}
data := reflectData{
PathAliasMap: uniqueAlias(paths),
PathSymbolMap: pathSymbolMap,
}
var program bytes.Buffer
if err := reflectProgram.Execute(&program, &data); err != nil {
return nil, err
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, progSource), program.Bytes(), 0600); err != nil {
return nil, err
}
// Build it
var buildStdout, buildStderr bytes.Buffer
build := exec.Command("go", "build", "-o", progBinary, progSource)
build.Dir = tmpDir
build.Stdout = &buildStdout
build.Stderr = &buildStderr
if err := build.Run(); err != nil {
return nil, errors.Wrap(err, buildStderr.String())
}
progPath := filepath.Join(tmpDir, progBinary)
// Run it
var stdout, stderr bytes.Buffer
cmd := exec.Command(progPath)
cmd.Dir = projRoot
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, stderr.String())
}
var pkgs map[string]*model.Package
if err := gob.NewDecoder(&stdout).Decode(&pkgs); err != nil {
return nil, err
}
return pkgs, nil
}
// This program reflects on an interface value, and prints the
// gob encoding of a model.Package to standard output.
// JSON doesn't work because of the model.Type interface.
var reflectProgram = template.Must(template.New("program").Parse(`
{{$pathAliasMap := .PathAliasMap}}
{{$pathSymbolMap := .PathSymbolMap}}
package main
import (
"encoding/gob"
"fmt"
"os"
"reflect"
"github.com/golang/mock/mockgen/model"
{{range $importPath, $alias := $pathAliasMap}}
{{$alias}} "{{$importPath}}"
{{end}}
)
func main() {
its := []struct{
path, sym string
typ reflect.Type
}{
{{range $importPath, $symbol := $pathSymbolMap}}
{"{{$importPath}}", "{{$symbol}}", reflect.TypeOf((*{{index $pathAliasMap $importPath}}.{{$symbol}})(nil)).Elem()},
{{end}}
}
pkgs := make(map[string]*model.Package, {{len $pathSymbolMap}})
{{range $importPath, $symbol := $pathSymbolMap}}
pkgs["{{$importPath}}"] = &model.Package{
Name: "{{index $pathAliasMap $importPath}}",
}
{{end}}
stderr := os.Stderr
for _, it := range its {
intf, err := model.InterfaceFromInterfaceType(it.typ)
if err != nil {
fmt.Fprintf(stderr, "Reflection: %v\n", err)
os.Exit(1)
}
intf.Name = it.sym
pkgs[it.path].Interfaces = []*model.Interface{intf}
}
if err := gob.NewEncoder(os.Stdout).Encode(pkgs); err != nil {
fmt.Fprintf(stderr, "gob encode: %v\n", err)
os.Exit(1)
}
}
`))