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
|
package printer
import (
"bytes"
"fmt"
"hash/crc32"
"io"
"math"
"os"
"path/filepath"
"strings"
"github.com/tinylib/msgp/gen"
"github.com/tinylib/msgp/parse"
"golang.org/x/tools/imports"
)
var Logf func(s string, v ...any)
// PrintFile prints the methods for the provided list
// of elements to the given file name and canonical
// package path.
func PrintFile(file string, f *parse.FileSet, mode gen.Method) error {
out, tests, err := generate(file, f, mode)
if err != nil {
return err
}
// we'll run goimports on the main file
// in another goroutine, and run it here
// for the test file. empirically, this
// takes about the same amount of time as
// doing them in serial when GOMAXPROCS=1,
// and faster otherwise.
res := goformat(file, out.Bytes())
if tests != nil {
testfile := strings.TrimSuffix(file, ".go") + "_test.go"
err = format(testfile, tests.Bytes())
if err != nil {
return err
}
if Logf != nil {
Logf("Wrote and formatted \"%s\"\n", testfile)
}
}
err = <-res
if err != nil {
os.WriteFile(file+".broken", out.Bytes(), os.ModePerm)
if Logf != nil {
Logf("Error: %s. Wrote broken output to %s\n", err, file+".broken")
}
return err
}
return nil
}
func format(file string, data []byte) error {
out, err := imports.Process(file, data, nil)
if err != nil {
return err
}
return os.WriteFile(file, out, 0o600)
}
func goformat(file string, data []byte) <-chan error {
out := make(chan error, 1)
go func(file string, data []byte, end chan error) {
end <- format(file, data)
if Logf != nil {
Logf("Wrote and formatted \"%s\"\n", file)
}
}(file, data, out)
return out
}
func dedupImports(imp []string) []string {
m := make(map[string]struct{})
for i := range imp {
m[imp[i]] = struct{}{}
}
r := []string{}
for k := range m {
r = append(r, k)
}
return r
}
func generate(file string, f *parse.FileSet, mode gen.Method) (*bytes.Buffer, *bytes.Buffer, error) {
outbuf := bytes.NewBuffer(make([]byte, 0, 4096))
writePkgHeader(outbuf, f.Package)
myImports := []string{"github.com/tinylib/msgp/msgp"}
for _, imp := range f.Imports {
if imp.Name != nil {
// have an alias, include it.
myImports = append(myImports, imp.Name.Name+` `+imp.Path.Value)
} else {
myImports = append(myImports, imp.Path.Value)
}
}
dedup := dedupImports(myImports)
writeImportHeader(outbuf, dedup...)
writeLimitConstants(outbuf, file, f)
var testbuf *bytes.Buffer
var testwr io.Writer
if mode&gen.Test == gen.Test {
testbuf = bytes.NewBuffer(make([]byte, 0, 4096))
writePkgHeader(testbuf, f.Package)
if mode&(gen.Encode|gen.Decode) != 0 {
writeImportHeader(testbuf, "bytes", "github.com/tinylib/msgp/msgp", "testing")
} else {
writeImportHeader(testbuf, "github.com/tinylib/msgp/msgp", "testing")
}
testwr = testbuf
}
return outbuf, testbuf, f.PrintTo(gen.NewPrinter(mode, outbuf, testwr))
}
func writePkgHeader(b *bytes.Buffer, name string) {
// write generated code marker
// https://github.com/tinylib/msgp/issues/229
// https://golang.org/s/generatedcode
b.WriteString("// Code generated by github.com/tinylib/msgp DO NOT EDIT.\n\n")
b.WriteString("package ")
b.WriteString(name)
b.WriteString("\n\n")
}
func writeImportHeader(b *bytes.Buffer, imports ...string) {
b.WriteString("import (\n")
for _, im := range imports {
if im[len(im)-1] == '"' {
// support aliased imports
fmt.Fprintf(b, "\t%s\n", im)
} else {
fmt.Fprintf(b, "\t%q\n", im)
}
}
b.WriteString(")\n\n")
}
// generateFilePrefix creates a deterministic, unique prefix for constants based on the file name
func generateFilePrefix(filename string) string {
base := filepath.Base(filename)
hash := crc32.ChecksumIEEE([]byte(base))
return fmt.Sprintf("z%08x", hash)
}
func writeLimitConstants(b *bytes.Buffer, file string, f *parse.FileSet) {
if f.ArrayLimit != math.MaxUint32 || f.MapLimit != math.MaxUint32 {
prefix := generateFilePrefix(file)
b.WriteString("// Size limits for msgp deserialization\n")
b.WriteString("const (\n")
if f.ArrayLimit != math.MaxUint32 {
fmt.Fprintf(b, "\t%slimitArrays = %d\n", prefix, f.ArrayLimit)
}
if f.MapLimit != math.MaxUint32 {
fmt.Fprintf(b, "\t%slimitMaps = %d\n", prefix, f.MapLimit)
}
b.WriteString(")\n\n")
// Store the prefix in FileSet so generators can use it
f.LimitPrefix = prefix
}
}
|