From 1fd1f70537e114845c1ef673897007a7555fde3b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 6 Mar 2017 23:57:02 +0100 Subject: [PATCH] fix bug where package name suffix increased with every use --- internal/tests/nameclash/input.go | 1 + internal/tests/nameclash/output.go | 12 ++++++++++++ typeutil.go | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/tests/nameclash/input.go b/internal/tests/nameclash/input.go index 4e997d6..81a239b 100644 --- a/internal/tests/nameclash/input.go +++ b/internal/tests/nameclash/input.go @@ -24,6 +24,7 @@ type dec struct{} type Y struct { Foo json.Foo `optional:"true"` + Foo2 json.Foo `optional:"true"` Bar errors.Foo `optional:"true"` Gazonk YJSON `optional:"true"` Over int `optional:"true"` diff --git a/internal/tests/nameclash/output.go b/internal/tests/nameclash/output.go index 42d6cb6..89b267c 100644 --- a/internal/tests/nameclash/output.go +++ b/internal/tests/nameclash/output.go @@ -12,12 +12,14 @@ import ( func (y *Y) MarshalJSON() ([]byte, error) { type YJSON0 struct { Foo *json0.Foo `optional:"true"` + Foo2 *json0.Foo `optional:"true"` Bar *errors0.Foo `optional:"true"` Gazonk *YJSON `optional:"true"` Over *enc `optional:"true"` } var enc0 YJSON0 enc0.Foo = &y.Foo + enc0.Foo2 = &y.Foo2 enc0.Bar = &y.Bar enc0.Gazonk = &y.Gazonk enc0.Over = (*enc)(&y.Over) @@ -27,6 +29,7 @@ func (y *Y) MarshalJSON() ([]byte, error) { func (y *Y) UnmarshalJSON(input []byte) error { type YJSON0 struct { Foo *json0.Foo `optional:"true"` + Foo2 *json0.Foo `optional:"true"` Bar *errors0.Foo `optional:"true"` Gazonk *YJSON `optional:"true"` Over *enc `optional:"true"` @@ -39,6 +42,9 @@ func (y *Y) UnmarshalJSON(input []byte) error { if dec.Foo != nil { x.Foo = *dec.Foo } + if dec.Foo2 != nil { + x.Foo2 = *dec.Foo2 + } if dec.Bar != nil { x.Bar = *dec.Bar } @@ -55,12 +61,14 @@ func (y *Y) UnmarshalJSON(input []byte) error { func (y *Y) MarshalYAML() (interface{}, error) { type YYAML struct { Foo *json0.Foo `optional:"true"` + Foo2 *json0.Foo `optional:"true"` Bar *errors0.Foo `optional:"true"` Gazonk *YJSON `optional:"true"` Over *enc `optional:"true"` } var enc0 YYAML enc0.Foo = &y.Foo + enc0.Foo2 = &y.Foo2 enc0.Bar = &y.Bar enc0.Gazonk = &y.Gazonk enc0.Over = (*enc)(&y.Over) @@ -70,6 +78,7 @@ func (y *Y) MarshalYAML() (interface{}, error) { func (y *Y) UnmarshalYAML(unmarshal func(interface{}) error) error { type YYAML struct { Foo *json0.Foo `optional:"true"` + Foo2 *json0.Foo `optional:"true"` Bar *errors0.Foo `optional:"true"` Gazonk *YJSON `optional:"true"` Over *enc `optional:"true"` @@ -82,6 +91,9 @@ func (y *Y) UnmarshalYAML(unmarshal func(interface{}) error) error { if dec.Foo != nil { x.Foo = *dec.Foo } + if dec.Foo2 != nil { + x.Foo2 = *dec.Foo2 + } if dec.Bar != nil { x.Bar = *dec.Bar } diff --git a/typeutil.go b/typeutil.go index 99337f4..197a4a4 100644 --- a/typeutil.go +++ b/typeutil.go @@ -166,7 +166,7 @@ func (s *fileScope) addReferences(typ types.Type) { // rebuild the import name cache. func (s *fileScope) insertImport(pkg *types.Package) { i := sort.Search(len(s.imports), func(i int) bool { - return s.imports[i].Path() > pkg.Path() + return s.imports[i].Path() >= pkg.Path() }) if i < len(s.imports) && s.imports[i] == pkg { return