Repository: caicloud/nirvana
Pull request #417 (2020-12-15 11:58:47)

[release-0.3] fix(generator): sort type names to make the package aliases deterministic
This is an automated cherry-pick of #416

/assign iawia002

caicloud-bot: @caicloud-bot: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our [release note process](https://git.k8s.io/community/contributors/guide/release-notes.md) to remove it.

<details>

Instructions for interacting with me using PR comments are available [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md).  If you have questions or suggestions related to my behavior, please file an issue against the [caicloud/tools](https://github.com/caicloud/tools/issues/new?title=Prow%20issue:) repository.
</details>
iawia002: /release-note-none
/lgtm
caicloud-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/caicloud/nirvana/pull/417#issuecomment-745243522" title="LGTM">iawia002</a>*

The full list of commands accepted by this bot can be found [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#bot-commands).

The pull request process is described [here](https://github.com/caicloud/engineering/blob/master/guidelines/caicloud_bot.md#typical-workflow)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)~~ [iawia002]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/utils/generators/golang/helper.go b/utils/generators/golang/helper.go
index 9536f58..f1a8a92 100644
--- a/utils/generators/golang/helper.go
+++ b/utils/generators/golang/helper.go
@@ -541,8 +541,15 @@ func newTypeNamer(rootPkg string, types map[api.TypeName]*api.Type) (*typeNamer,
 		comments: make(map[api.TypeName]string),
 		rootPkg:  rootPkg,
 	}
-	for tn := range n.types {
-		if _, err := n.parse(tn); err != nil {
+
+	typeNames := make([]string, 0, len(types))
+	for name := range types {
+		typeNames = append(typeNames, string(name))
+	}
+	sort.Strings(typeNames)
+
+	for _, tn := range typeNames {
+		if _, err := n.parse(api.TypeName(tn)); err != nil {
 			return nil, err
 		}
 	}


Test patch:
diff --git a/utils/generators/golang/helper_test.go b/utils/generators/golang/helper_test.go
new file mode 100644
index 0000000..f2e4790
--- /dev/null
+++ b/utils/generators/golang/helper_test.go
@@ -0,0 +1,58 @@
+/*
+Copyright 2020 Caicloud Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package golang
+
+import (
+	"reflect"
+	"testing"
+
+	"github.com/caicloud/nirvana/utils/api"
+)
+
+func TestDeterministicClientPackageAlias(t *testing.T) {
+	const (
+		rounds  = 20
+		rootPkg = "github.com/caicloud/nirvana"
+	)
+	types := map[api.TypeName]*api.Type{
+		api.TypeName("github.com/caicloud/repo/v1.Foo"): {
+			Name:    "Foo",
+			PkgPath: "github.com/caicloud/repo/v1",
+			Kind:    reflect.Struct,
+		},
+		api.TypeName("github.com/caicloud/repo/pkg/v1.Bar"): {
+			Name:    "Bar",
+			PkgPath: "github.com/caicloud/repo/pkg/v1",
+			Kind:    reflect.Struct,
+		},
+	}
+	namer, err := newTypeNamer(rootPkg, types)
+	if err != nil {
+		t.Fatal(err)
+	}
+	expected := namer.packages
+
+	for i := 0; i < rounds; i++ {
+		namer, err := newTypeNamer(rootPkg, types)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if !reflect.DeepEqual(expected, namer.packages) {
+			t.Fatalf("Package alias changed:\nExpected: %#v\nGot: %#v\n", expected, namer.packages)
+		}
+	}
+}