prebuild.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // +build prebuild
  2. package main
  3. // prebuild.go generates sort implementations for
  4. // various slice types and combination slice+reflect.Value types.
  5. //
  6. // The combination slice+reflect.Value types are used
  7. // during canonical encode, and the others are used during fast-path
  8. // encoding of map keys.
  9. import (
  10. "bytes"
  11. "go/format"
  12. "io/ioutil"
  13. "os"
  14. "strings"
  15. "text/template"
  16. )
  17. // genInternalSortableTypes returns the types
  18. // that are used for fast-path canonical's encoding of maps.
  19. //
  20. // For now, we only support the highest sizes for
  21. // int64, uint64, float64, bool, string, bytes.
  22. func genInternalSortableTypes() []string {
  23. return []string{
  24. "string",
  25. // "float32",
  26. "float64",
  27. // "uint",
  28. // "uint8",
  29. // "uint16",
  30. // "uint32",
  31. "uint64",
  32. "uintptr",
  33. // "int",
  34. // "int8",
  35. // "int16",
  36. // "int32",
  37. "int64",
  38. "bool",
  39. "time",
  40. "bytes",
  41. }
  42. }
  43. // genInternalSortablePlusTypes returns the types
  44. // that are used for reflection-based canonical's encoding of maps.
  45. //
  46. // For now, we only support the highest sizes for
  47. // int64, uint64, float64, bool, string, bytes.
  48. func genInternalSortablePlusTypes() []string {
  49. return []string{
  50. "string",
  51. "float64",
  52. "uint64",
  53. "uintptr",
  54. "int64",
  55. "bool",
  56. "time",
  57. "bytes",
  58. }
  59. }
  60. func genTypeForShortName(s string) string {
  61. switch s {
  62. case "time":
  63. return "time.Time"
  64. case "bytes":
  65. return "[]byte"
  66. }
  67. return s
  68. }
  69. func genArgs(args ...interface{}) map[string]interface{} {
  70. m := make(map[string]interface{}, len(args)/2)
  71. for i := 0; i < len(args); {
  72. m[args[i].(string)] = args[i+1]
  73. i += 2
  74. }
  75. return m
  76. }
  77. func genEndsWith(s0 string, sn ...string) bool {
  78. for _, s := range sn {
  79. if strings.HasSuffix(s0, s) {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. func chkerr(err error) {
  86. if err != nil {
  87. panic(err)
  88. }
  89. }
  90. func run(fnameIn, fnameOut string) {
  91. var err error
  92. funcs := make(template.FuncMap)
  93. funcs["sortables"] = genInternalSortableTypes
  94. funcs["sortablesplus"] = genInternalSortablePlusTypes
  95. funcs["tshort"] = genTypeForShortName
  96. funcs["endswith"] = genEndsWith
  97. funcs["args"] = genArgs
  98. t := template.New("").Funcs(funcs)
  99. fin, err := os.Open(fnameIn)
  100. chkerr(err)
  101. defer fin.Close()
  102. fout, err := os.Create(fnameOut)
  103. chkerr(err)
  104. defer fout.Close()
  105. tmplstr, err := ioutil.ReadAll(fin)
  106. chkerr(err)
  107. t, err = t.Parse(string(tmplstr))
  108. chkerr(err)
  109. var out bytes.Buffer
  110. err = t.Execute(&out, 0)
  111. chkerr(err)
  112. bout, err := format.Source(out.Bytes())
  113. if err != nil {
  114. fout.Write(out.Bytes()) // write out if error, so we can still see.
  115. }
  116. chkerr(err)
  117. // write out if error, as much as possible, so we can still see.
  118. _, err = fout.Write(bout)
  119. chkerr(err)
  120. }
  121. func main() {
  122. run("sort-slice.go.tmpl", "sort-slice.generated.go")
  123. }