diff --git a/Makefile b/Makefile index b98f8b1..1867f28 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,11 @@ generate: root build .root/src/$(PKG)/tests/snake.go \ .root/src/$(PKG)/tests/data.go \ .root/src/$(PKG)/tests/omitempty.go \ + .root/src/$(PKG)/tests/nothing.go \ .root/src/$(PKG)/tests/camel_case_functions.go .root/bin/easyjson -all .root/src/$(PKG)/tests/data.go + .root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go .root/bin/easyjson -snake_case .root/src/$(PKG)/tests/snake.go .root/bin/easyjson -omit_empty .root/src/$(PKG)/tests/omitempty.go .root/bin/easyjson -camel_case_functions .root/src/$(PKG)/tests/camel_case_functions.go diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 8bca39a..39b706f 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -51,20 +51,23 @@ func (g *Generator) writeStub() error { fmt.Fprintln(f, "// compilable during generation.") fmt.Fprintln(f) fmt.Fprintln(f, "package ", g.PkgName) - fmt.Fprintln(f) - fmt.Fprintln(f, "import (") - fmt.Fprintln(f, ` "`+pkgWriter+`"`) - fmt.Fprintln(f, ` "`+pkgLexer+`"`) - fmt.Fprintln(f, ")") + + if len(g.Types) > 0 { + fmt.Fprintln(f) + fmt.Fprintln(f, "import (") + fmt.Fprintln(f, ` "`+pkgWriter+`"`) + fmt.Fprintln(f, ` "`+pkgLexer+`"`) + fmt.Fprintln(f, ")") + } for _, t := range g.Types { fmt.Fprintln(f) if !g.NoStdMarshalers { - fmt.Fprintln(f, "func (*", t, ") MarshalJSON() ([]byte, error) { return nil, nil }") + fmt.Fprintln(f, "func (", t, ") MarshalJSON() ([]byte, error) { return nil, nil }") fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }") } - fmt.Fprintln(f, "func (*", t, ") MarshalEasyJSON(w *jwriter.Writer) {}") + fmt.Fprintln(f, "func (", t, ") MarshalEasyJSON(w *jwriter.Writer) {}") fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}") fmt.Fprintln(f) fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t) @@ -91,8 +94,10 @@ func (g *Generator) writeMain() (path string, err error) { fmt.Fprintln(f, ` "os"`) fmt.Fprintln(f) fmt.Fprintf(f, " %q\n", genPackage) - fmt.Fprintln(f) - fmt.Fprintf(f, " pkg %q\n", g.PkgPath) + if len(g.Types) > 0 { + fmt.Fprintln(f) + fmt.Fprintf(f, " pkg %q\n", g.PkgPath) + } fmt.Fprintln(f, ")") fmt.Fprintln(f) fmt.Fprintln(f, "func main() {") diff --git a/gen/generator.go b/gen/generator.go index 227d5e7..fe8af99 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -174,7 +174,12 @@ func (g *Generator) printHeader() { fmt.Println(")") fmt.Println("") - fmt.Println("var _ = json.RawMessage{} // suppress unused package warning") + fmt.Println("// suppress unused package warning") + fmt.Println("var (") + fmt.Println(" _ = json.RawMessage{}") + fmt.Println(" _ = jlexer.Lexer{}") + fmt.Println(" _ = jwriter.Writer{}") + fmt.Println(")") fmt.Println() } diff --git a/tests/data.go b/tests/data.go index d0e1e7a..1c4ae78 100644 --- a/tests/data.go +++ b/tests/data.go @@ -437,8 +437,9 @@ var mapsString = `{` + `}` type DeepNest struct { - SliceMap map[Str][]Str - MapSlice []map[Str]Str + SliceMap map[Str][]Str + SliceMap1 map[Str][]Str + MapSlice []map[Str]Str } var deepNestValue = DeepNest{ @@ -447,6 +448,8 @@ var deepNestValue = DeepNest{ "0", "1", }, + }, + SliceMap1: map[Str][]Str{ "testSliceMap2": nil, }, MapSlice: []map[Str]Str{ @@ -458,7 +461,9 @@ var deepNestValue = DeepNest{ var deepNestString = `{` + `"SliceMap":{` + - `"testSliceMap1":["0","1"],` + + `"testSliceMap1":["0","1"]` + + `},` + + `"SliceMap1":{` + `"testSliceMap2":[]` + `},` + `"MapSlice":[` + diff --git a/tests/nothing.go b/tests/nothing.go new file mode 100644 index 0000000..35334f5 --- /dev/null +++ b/tests/nothing.go @@ -0,0 +1,3 @@ +package tests + +// No structs in this file