-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotypeconverter.go
166 lines (141 loc) · 3.79 KB
/
gotypeconverter.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
159
160
161
162
163
164
165
166
package gotypeconverter
import (
"flag"
"fmt"
"go/types"
"io"
"os"
"regexp"
"strings"
ana "github.com/fuji8/gotypeconverter/analysis"
"github.com/fuji8/gotypeconverter/ui"
"golang.org/x/tools/go/packages"
)
const (
doc = "gotypeconverter generates a function that converts two different named types."
)
var (
flagOutput string
FlagVersion bool
flagSrc, flagDst, FlagStructTag string
)
func init() {
Gen.Flags.StringVar(&flagOutput, "o", "", "output file; if nil, output stdout")
Gen.Flags.StringVar(&flagSrc, "s", "", "source type")
Gen.Flags.StringVar(&flagDst, "d", "", "destination type")
Gen.Flags.BoolVar(&FlagVersion, "v", false, "version")
Gen.Flags.StringVar(&FlagStructTag, "structTag", "cvt", "")
}
// A Generator describes a code generator function and its options.
type Generator struct {
// The Name of the generator must be a valid Go identifier
// as it may appear in command-line flags, URLs, and so on.
Name string
// Doc is the documentation for the generator.
// The part before the first "\n\n" is the title
// (no capital or period, max ~60 letters).
Doc string
// Flags defines any flags accepted by the generator.
// The manner in which these flags are exposed to the user
// depends on the driver which runs the generator.
Flags flag.FlagSet
// Run applies the generator to a package.
// It returns an error if the generator failed.
//
// To pass analysis results of depended analyzers between packages (and thus
// potentially between address spaces), use Facts, which are
// serializable.
Run func([]*packages.Package) (string, error)
Output func(pkg *types.Package) io.Writer
}
var Gen = &Generator{
Name: "gotypeconverter",
Doc: doc,
Run: run,
}
func TypeOf4(pkg *types.Package, pkgname, typename string) types.Type {
if typename == "" {
return nil
}
if typename[0] == '*' {
obj := TypeOf4(pkg, pkgname, typename[1:])
if obj == nil {
return nil
}
return types.NewPointer(obj)
}
if typename[0] == '[' {
obj := TypeOf4(pkg, pkgname, typename[2:])
if obj == nil {
return nil
}
return types.NewSlice(obj)
}
if pkgname == "" {
obj := pkg.Scope().Lookup(typename)
return obj.Type()
}
for i := 0; i < pkg.Scope().NumChildren(); i++ {
pkgN, ok := pkg.Scope().Child(i).Lookup(pkgname).(*types.PkgName)
if ok {
obj := pkgN.Imported().Scope().Lookup(typename)
if obj == nil {
return nil
}
return obj.Type()
}
}
return nil
}
func splitToPkgAndType(s string) (string, string) {
if idx := strings.LastIndex(s, "."); idx > 0 {
pkgname := s[:idx]
typename := s[idx+1:]
jdxs := regexp.MustCompile(`^[\[\]\*]+`).FindStringIndex(pkgname)
if jdxs != nil {
typename = pkgname[:jdxs[1]] + typename
pkgname = pkgname[jdxs[1]:]
}
return pkgname, typename
}
return "", s
}
func run(pkgs []*packages.Package) (string, error) {
var (
srcType, dstType types.Type
)
pkgIdx := 0
for i, pkg := range pkgs {
srcPkgName, srcTypeName := splitToPkgAndType(flagSrc)
dstPkgName, dstTypeName := splitToPkgAndType(flagDst)
srcType = TypeOf4(pkg.Types, srcPkgName, srcTypeName)
dstType = TypeOf4(pkg.Types, dstPkgName, dstTypeName)
if srcType != nil && dstType != nil {
pkgIdx = i
break
}
}
if srcType == nil || dstType == nil {
return "", fmt.Errorf("srcType or dstType is nil, srcType: %s, dstType: %s", srcType, dstType)
}
funcMaker := ana.InitFuncMaker(pkgs[pkgIdx].Types)
funcMaker.MakeFunc(ana.InitType(dstType, flagDst), ana.InitType(srcType, flagSrc), true)
if flagOutput == "" {
src, err := ui.NoInfoGeneration(funcMaker)
if err != nil {
return "", err
}
return src, nil
}
src, err := ui.FileNameGeneration(funcMaker, flagOutput)
if err != nil {
return "", err
}
f, err := os.Create(flagOutput)
if err != nil {
return "", err
}
defer f.Close()
fmt.Fprint(f, src)
return "", nil
}