gen.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. // codecgen generates codec.Selfer implementations for a set of types.
  4. package main
  5. import (
  6. "bufio"
  7. "bytes"
  8. "errors"
  9. "flag"
  10. "fmt"
  11. "go/ast"
  12. "go/build"
  13. "go/parser"
  14. "go/token"
  15. "math/rand"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "regexp"
  20. "strconv"
  21. "text/template"
  22. "time"
  23. )
  24. const genCodecPkg = "codec1978" // keep this in sync with codec.genCodecPkg
  25. const genFrunMainTmpl = `//+build ignore
  26. package main
  27. {{ if .Types }}import "{{ .ImportPath }}"{{ end }}
  28. func main() {
  29. {{ $.PackageName }}.CodecGenTempWrite{{ .RandString }}()
  30. }
  31. `
  32. // const genFrunPkgTmpl = `//+build codecgen
  33. const genFrunPkgTmpl = `
  34. package {{ $.PackageName }}
  35. import (
  36. {{ if not .CodecPkgFiles }}{{ .CodecPkgName }} "{{ .CodecImportPath }}"{{ end }}
  37. "os"
  38. "reflect"
  39. "bytes"
  40. "strings"
  41. "go/format"
  42. )
  43. func CodecGenTempWrite{{ .RandString }}() {
  44. fout, err := os.Create("{{ .OutFile }}")
  45. if err != nil {
  46. panic(err)
  47. }
  48. defer fout.Close()
  49. var out bytes.Buffer
  50. var typs []reflect.Type
  51. {{ range $index, $element := .Types }}
  52. var t{{ $index }} {{ . }}
  53. typs = append(typs, reflect.TypeOf(t{{ $index }}))
  54. {{ end }}
  55. {{ if not .CodecPkgFiles }}{{ .CodecPkgName }}.{{ end }}Gen(&out, "{{ .BuildTag }}", "{{ .PackageName }}", "{{ .RandString }}", {{ .UseUnsafe }}, {{ if not .CodecPkgFiles }}{{ .CodecPkgName }}.{{ end }}NewTypeInfos(strings.Split("{{ .StructTags }}", ",")), typs...)
  56. bout, err := format.Source(out.Bytes())
  57. if err != nil {
  58. fout.Write(out.Bytes())
  59. panic(err)
  60. }
  61. fout.Write(bout)
  62. }
  63. `
  64. // Generate is given a list of *.go files to parse, and an output file (fout).
  65. //
  66. // It finds all types T in the files, and it creates 2 tmp files (frun).
  67. // - main package file passed to 'go run'
  68. // - package level file which calls *genRunner.Selfer to write Selfer impls for each T.
  69. // We use a package level file so that it can reference unexported types in the package being worked on.
  70. // Tool then executes: "go run __frun__" which creates fout.
  71. // fout contains Codec(En|De)codeSelf implementations for every type T.
  72. //
  73. func Generate(outfile, buildTag, codecPkgPath string, uid int64, useUnsafe bool, goRunTag string,
  74. st string, regexName *regexp.Regexp, notRegexName *regexp.Regexp, deleteTempFile bool, infiles ...string) (err error) {
  75. // For each file, grab AST, find each type, and write a call to it.
  76. if len(infiles) == 0 {
  77. return
  78. }
  79. if outfile == "" || codecPkgPath == "" {
  80. err = errors.New("outfile and codec package path cannot be blank")
  81. return
  82. }
  83. if uid < 0 {
  84. uid = -uid
  85. }
  86. if uid == 0 {
  87. rr := rand.New(rand.NewSource(time.Now().UnixNano()))
  88. uid = 101 + rr.Int63n(9777)
  89. }
  90. // We have to parse dir for package, before opening the temp file for writing (else ImportDir fails).
  91. // Also, ImportDir(...) must take an absolute path.
  92. lastdir := filepath.Dir(outfile)
  93. absdir, err := filepath.Abs(lastdir)
  94. if err != nil {
  95. return
  96. }
  97. pkg, err := build.Default.ImportDir(absdir, build.AllowBinary)
  98. if err != nil {
  99. return
  100. }
  101. type tmplT struct {
  102. CodecPkgName string
  103. CodecImportPath string
  104. ImportPath string
  105. OutFile string
  106. PackageName string
  107. RandString string
  108. BuildTag string
  109. StructTags string
  110. Types []string
  111. CodecPkgFiles bool
  112. UseUnsafe bool
  113. }
  114. tv := tmplT{
  115. CodecPkgName: genCodecPkg,
  116. OutFile: outfile,
  117. CodecImportPath: codecPkgPath,
  118. BuildTag: buildTag,
  119. UseUnsafe: useUnsafe,
  120. RandString: strconv.FormatInt(uid, 10),
  121. StructTags: st,
  122. }
  123. tv.ImportPath = pkg.ImportPath
  124. if tv.ImportPath == tv.CodecImportPath {
  125. tv.CodecPkgFiles = true
  126. tv.CodecPkgName = "codec"
  127. }
  128. astfiles := make([]*ast.File, len(infiles))
  129. for i, infile := range infiles {
  130. if filepath.Dir(infile) != lastdir {
  131. err = errors.New("in files must all be in same directory as outfile")
  132. return
  133. }
  134. fset := token.NewFileSet()
  135. astfiles[i], err = parser.ParseFile(fset, infile, nil, 0)
  136. if err != nil {
  137. return
  138. }
  139. if i == 0 {
  140. tv.PackageName = astfiles[i].Name.Name
  141. if tv.PackageName == "main" {
  142. // codecgen cannot be run on types in the 'main' package.
  143. // A temporary 'main' package must be created, and should reference the fully built
  144. // package containing the types.
  145. // Also, the temporary main package will conflict with the main package which already has a main method.
  146. err = errors.New("codecgen cannot be run on types in the 'main' package")
  147. return
  148. }
  149. }
  150. }
  151. for _, f := range astfiles {
  152. for _, d := range f.Decls {
  153. if gd, ok := d.(*ast.GenDecl); ok {
  154. for _, dd := range gd.Specs {
  155. if td, ok := dd.(*ast.TypeSpec); ok {
  156. // if len(td.Name.Name) == 0 || td.Name.Name[0] > 'Z' || td.Name.Name[0] < 'A' {
  157. if len(td.Name.Name) == 0 {
  158. continue
  159. }
  160. // only generate for:
  161. // struct: StructType
  162. // primitives (numbers, bool, string): Ident
  163. // map: MapType
  164. // slice, array: ArrayType
  165. // chan: ChanType
  166. // do not generate:
  167. // FuncType, InterfaceType, StarExpr (ptr), etc
  168. switch td.Type.(type) {
  169. case *ast.StructType, *ast.Ident, *ast.MapType, *ast.ArrayType, *ast.ChanType:
  170. if regexName.FindStringIndex(td.Name.Name) != nil && notRegexName.FindStringIndex(td.Name.Name) == nil {
  171. tv.Types = append(tv.Types, td.Name.Name)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. if len(tv.Types) == 0 {
  180. return
  181. }
  182. // we cannot use ioutil.TempFile, because we cannot guarantee the file suffix (.go).
  183. // Also, we cannot create file in temp directory,
  184. // because go run will not work (as it needs to see the types here).
  185. // Consequently, create the temp file in the current directory, and remove when done.
  186. // frun, err = ioutil.TempFile("", "codecgen-")
  187. // frunName := filepath.Join(os.TempDir(), "codecgen-"+strconv.FormatInt(time.Now().UnixNano(), 10)+".go")
  188. frunMainName := "codecgen-main-" + tv.RandString + ".generated.go"
  189. frunPkgName := "codecgen-pkg-" + tv.RandString + ".generated.go"
  190. if deleteTempFile {
  191. defer os.Remove(frunMainName)
  192. defer os.Remove(frunPkgName)
  193. }
  194. // var frunMain, frunPkg *os.File
  195. if _, err = gen1(frunMainName, genFrunMainTmpl, &tv); err != nil {
  196. return
  197. }
  198. if _, err = gen1(frunPkgName, genFrunPkgTmpl, &tv); err != nil {
  199. return
  200. }
  201. // remove outfile, so "go run ..." will not think that types in outfile already exist.
  202. os.Remove(outfile)
  203. // execute go run frun
  204. cmd := exec.Command("go", "run", "-tags="+goRunTag, frunMainName) //, frunPkg.Name())
  205. var buf bytes.Buffer
  206. cmd.Stdout = &buf
  207. cmd.Stderr = &buf
  208. if err = cmd.Run(); err != nil {
  209. err = fmt.Errorf("error running 'go run %s': %v, console: %s",
  210. frunMainName, err, buf.Bytes())
  211. return
  212. }
  213. os.Stdout.Write(buf.Bytes())
  214. return
  215. }
  216. func gen1(frunName, tmplStr string, tv interface{}) (frun *os.File, err error) {
  217. os.Remove(frunName)
  218. if frun, err = os.Create(frunName); err != nil {
  219. return
  220. }
  221. defer frun.Close()
  222. t := template.New("")
  223. if t, err = t.Parse(tmplStr); err != nil {
  224. return
  225. }
  226. bw := bufio.NewWriter(frun)
  227. if err = t.Execute(bw, tv); err != nil {
  228. return
  229. }
  230. if err = bw.Flush(); err != nil {
  231. return
  232. }
  233. return
  234. }
  235. func main() {
  236. o := flag.String("o", "", "out file")
  237. c := flag.String("c", genCodecPath, "codec path")
  238. t := flag.String("t", "", "build tag to put in file")
  239. r := flag.String("r", ".*", "regex for type name to match")
  240. nr := flag.String("nr", "^$", "regex for type name to exclude")
  241. rt := flag.String("rt", "", "tags for go run")
  242. st := flag.String("st", "codec,json", "struct tag keys to introspect")
  243. x := flag.Bool("x", false, "keep temp file")
  244. u := flag.Bool("u", false, "Use unsafe, e.g. to avoid unnecessary allocation on []byte->string")
  245. d := flag.Int64("d", 0, "random identifier for use in generated code")
  246. flag.Parse()
  247. if err := Generate(*o, *t, *c, *d, *u, *rt, *st,
  248. regexp.MustCompile(*r), regexp.MustCompile(*nr), !*x, flag.Args()...); err != nil {
  249. fmt.Fprintf(os.Stderr, "codecgen error: %v\n", err)
  250. os.Exit(1)
  251. }
  252. }