Fix imports for sub test.#1078
Conversation
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: n3wscott The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/assign @grantr I think my IDE did this for me when I moved the file out of v1alpha1. |
|
The following is the coverage report on pkg/.
|
|
/test pull-knative-eventing-integration-tests |
| duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" | ||
| "github.com/knative/pkg/controller" | ||
| logtesting "github.com/knative/pkg/logging/testing" | ||
|
|
There was a problem hiding this comment.
working as intended. I have been separating the . imports from the others.
There was a problem hiding this comment.
what you are commenting on: . "github.com/knative/eventing/pkg/reconciler/testing"
There was a problem hiding this comment.
So ". import" is one without an explicit alias? E.g. "github.com/foo/bar" as opposed to "qux github.com/foo/bar"?
There was a problem hiding this comment.
Also you can do this: import _ "github.com/foo/goo" and it will not import anything but run the init func and maybe that mutates global state.
There was a problem hiding this comment.
The project should have some well defined standard ordering. The one I have been using is standard libraries -> new line -> all others. I can see the advantage of moving ". imports" and possibly "_ imports" into separate structures, do we want the k8s stuff separate too? If we do that, then do we want the knative stuff separate as well?
There was a problem hiding this comment.
Most go projects will separate in the following ways:
- system stuff
- blocks of dep imports that are from the same host *n
- project stuff
There was a problem hiding this comment.
serving tends to do this, k8s.io gets its own block: https://github.com/knative/serving/blob/master/cmd/controller/main.go#L23
There was a problem hiding this comment.
I think dot imports should be separated from other imports, since the reader must know about them to read the code. I don't have strong opinions on import ordering otherwise. gofmt already enforces the block of standard lib, so that's settled. I could understand having separate blocks for vendored code versus in-repo code.
IMO dot imports reduce readability of the code by adding magic. I'd prefer that these not be used, but I won't block the PR for them. Correct me if I'm wrong @n3wscott, but I believe they're used here to work around the fact that when functional options are used, each functional option requires a reference to its package. Without dot imports this would be verbose.
fixtures.NewSubscription(subscriptionName, testNS,
fixtures.WithSubscriptionChannel(channelGVK, channelName),
fixtures.WithSubscriptionSubscriberRef(subscriberGVK, subscriberName),
)I prefer builders for specifying fixtures. Builders require only one package reference. When using a builder, my IDE can offer concise suggestions of which builder methods can be called next. With functional options, this is more difficult because any function in scope could conceivably be called (I'm not sure if any IDE is sophisticated enough to filter only the functions with the correct signature).
fixtures.NewSubscription(subscriptionName, testNS).
WithSubscriptionChannel(channelGVK, channelName).
WithSubscriptionSubscriberRef(subscriberGVK, subscriberName)Builders have the disadvantage of not returning a concrete struct of their built type, but this can be worked around by embedding the built struct into the builder struct.
Functional options have the advantage of allowing certain options to act on any runtime.Object (as pointed out by @n3wscott), which is cool, but seems even harder to document.
|
/lgtm /hold @Harwayne What do you think about this convention? import (
// standard lib, enforced by gofmt
"log"
// vendored code. These packages cannot be changed by the developer.
corev1 "k8s.io/api/core/v1"
// in-repo code. These packages can be changed by the developer.
"github.com/knative/eventing/pkg/broker"
// dot imports. These are separate because the developer must recognize them in advance
// to understand the code.
. "github.com/knative/pkg/controller/testing"
)Perhaps an interesting side project for when someone has a free day to tinker with go reformatting... |
|
I think it vendored code gets grouped by host: |
|
I'm lazy, so my preference is to do as little work as possible: import (
// standard lib, enforced by gofmt
"log"
// dot imports. These are separate because the developer must recognize them in advance
// to understand the code. These are also quite rare.
. "github.com/knative/pkg/controller/testing"
// All 'normal' imports that aren't part of the standard lib, regardless of whether they are in repo or vendored.
corev1 "k8s.io/api/core/v1"
"github.com/knative/eventing/pkg/broker"
)But at the end of the day, I just want to not have to think about this stuff, both when writing and reviewing. So whatever an automated tool does is fine for me. |
|
Opened #1082 to track the imports discussion. Now that that's separated, I think this PR can be unheld. /hold cancel |
🤖 Triggering CI on branch 'release-next' after synching to upstream/main
Fixes #1068
Proposed Changes
Release Note