From 7b35f91cec5814b29803a7d9fd5517f638761e67 Mon Sep 17 00:00:00 2001 From: Victor Starodub Date: Mon, 25 Apr 2016 02:11:43 +0300 Subject: [PATCH] Closes #21: File-unique auxiliary function names. --- bootstrap/bootstrap.go | 2 +- gen/decoder.go | 2 +- gen/encoder.go | 2 +- gen/generator.go | 21 ++++++++++++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index d7cd30b..e9a1d21 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -95,7 +95,7 @@ func (g *Generator) writeMain() (path string, err error) { fmt.Fprintln(f, ")") fmt.Fprintln(f) fmt.Fprintln(f, "func main() {") - fmt.Fprintln(f, " g := gen.NewGenerator()") + fmt.Fprintf(f, " g := gen.NewGenerator(%q)\n", filepath.Base(g.OutName)) fmt.Fprintf(f, " g.SetPkg(%q, %q)\n", g.PkgName, g.PkgPath) if g.BuildTags != "" { fmt.Fprintf(f, " g.SetBuildTags(%q)\n", g.BuildTags) diff --git a/gen/decoder.go b/gen/decoder.go index 09c6e89..852caa7 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -14,7 +14,7 @@ import ( const minSliceBytes = 64 func (g *Generator) getStructDecoderName(t reflect.Type) string { - return g.functionName("easyjson_decode_", t) + return g.functionName("decode_", t) } var primitiveDecoders = map[reflect.Kind]string{ diff --git a/gen/encoder.go b/gen/encoder.go index 026958b..dfe21f0 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -11,7 +11,7 @@ import ( ) func (g *Generator) getStructEncoderName(t reflect.Type) string { - return g.functionName("easyjson_encode_", t) + return g.functionName("encode_", t) } var primitiveEncoders = map[reflect.Kind]string{ diff --git a/gen/generator.go b/gen/generator.go index 5c3362f..07e19fc 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -3,6 +3,7 @@ package gen import ( "bytes" "fmt" + "hash/fnv" "io" "path" "reflect" @@ -23,9 +24,10 @@ type FieldNamer interface { type Generator struct { out *bytes.Buffer - pkgName string - pkgPath string - buildTags string + pkgName string + pkgPath string + buildTags string + funcPrefix string varCounter int @@ -51,8 +53,8 @@ type Generator struct { } // NewGenerator initializes and returns a Generator. -func NewGenerator() *Generator { - return &Generator{ +func NewGenerator(filename string) *Generator { + ret := &Generator{ imports: map[string]string{ pkgWriter: "jwriter", pkgLexer: "jlexer", @@ -63,6 +65,14 @@ func NewGenerator() *Generator { typesSeen: make(map[reflect.Type]bool), functionNames: make(map[string]reflect.Type), } + + // Use a file-unique prefix on all auxiliary functions to avoid + // name clashes. + hash := fnv.New32() + hash.Write([]byte(filename)) + ret.funcPrefix = fmt.Sprintf("easyjson_%x_", hash.Sum32()) + + return ret } // SetPkg sets the name and path of output package. @@ -257,6 +267,7 @@ func safeName(t reflect.Type) string { // // Method is used to track encoder/decoder names for the type. func (g *Generator) functionName(prefix string, t reflect.Type) string { + prefix = g.funcPrefix + prefix name := prefix + safeName(t) // Most of the names will be unique, try a shortcut first.