-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (162 loc) · 3.28 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"flag"
"go/build"
"go/format"
"html/template"
"log"
"os"
"path/filepath"
"strings"
)
const (
tmpl = `// This file is generated and should not be modified directly.
// Regenerate using ps2bs: 'go get github.com/codemodus/ps2bs; ps2bs'
{{ if .IsLibrary }}// Package {{ .PkgName }} provides functions which return pointers to
// built-ins and other commonly used standard library types.{{ end }}
package {{ .PkgName }}
{{- range $val := .Types }}
{{ if $.IsExported }}
// To{{ $val | TypeName | TitleCase }} returns a pointer to the type '{{ $val }}'.
{{- end }}
func {{ if $.IsExported }}To{{ else }}to{{ end }}{{ $val | TypeName | TitleCase }}({{ $val | TypeName | FirstAsLC }} {{ $val }}) *{{ $val }} {
return &{{ $val | TypeName | FirstAsLC }}
}{{ end }}
`
)
var (
types = []string{
"bool",
"byte",
"complex128",
"complex64",
"error",
"float32",
"float64",
"int",
"int16",
"int32",
"int64",
"int8",
"rune",
"string",
"uint",
"uint16",
"uint32",
"uint64",
"uint8",
"uintptr",
"time.Time",
}
fileName = "ps2bs_gen.go"
)
type options struct {
directory string
exported bool
}
type tmplContext struct {
PkgName string
IsLibrary bool
IsExported bool
Types []string
}
func newTmplContext(opts *options) (*tmplContext, error) {
destPkgName, isExisting, err := pkgInfo(opts.directory)
if err != nil {
return nil, err
}
ctx := &tmplContext{
PkgName: destPkgName,
IsLibrary: !isExisting,
IsExported: opts.exported,
Types: types,
}
return ctx, nil
}
func execTmpl(ctx *tmplContext) ([]byte, error) {
fs := funcMap()
bb := &bytes.Buffer{}
t, err := template.New("").Funcs(fs).Parse(tmpl)
if err != nil {
return nil, err
}
if err = t.Execute(bb, ctx); err != nil {
return nil, err
}
return bb.Bytes(), nil
}
func main() {
log.SetFlags(0)
o := &options{}
{
flag.StringVar(&o.directory, "dir", ".", "destination directory")
flag.BoolVar(&o.exported, "e", false, "export functions")
}
flag.Parse()
if err := run(o); err != nil {
log.Fatalln(err)
}
}
func run(o *options) error {
ctx, err := newTmplContext(o)
if err != nil {
return err
}
tbs, err := execTmpl(ctx)
if err != nil {
return err
}
bs, err := format.Source(tbs)
if err != nil {
return err
}
var aErr error
func() {
var f *os.File
f, aErr = os.Create(filepath.Join(o.directory, fileName))
if aErr != nil {
return
}
defer func() {
if err := f.Close(); err != nil {
if aErr == nil {
aErr = err
}
return
}
}()
if _, aErr = f.Write(bs); aErr != nil {
return
}
}()
return aErr
}
func pkgInfo(dir string) (name string, isExisting bool, err error) {
fs, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
return "", false, err
}
if len(fs) == 0 || len(fs) == 1 && filepath.Base(fs[0]) == fileName {
return filepath.Base(dir), false, nil
}
pkg, err := build.ImportDir(dir, 0)
if err != nil {
return "", false, err
}
return pkg.Name, true, nil
}
func firstAsLC(s string) string {
return strings.ToLower(s[0:1])
}
func typeName(s string) string {
ss := strings.Split(s, ".")
return ss[len(ss)-1]
}
func funcMap() template.FuncMap {
return template.FuncMap{
"TitleCase": strings.Title,
"FirstAsLC": firstAsLC,
"TypeName": typeName,
}
}