Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importas: allow repeated aliases #1960

Merged
merged 2 commits into from May 8, 2021
Merged

Conversation

cbandy
Copy link
Contributor

@cbandy cbandy commented May 7, 2021

As described in #1959, the standalone importas tool does not restrict an alias to a single package. This aligns our configuration with the standalone tool.

It is a breaking change, but another breaking change to this linter's configuration is waiting to be released in ffe8061.

@ldez ldez self-requested a review May 7, 2021 13:20
@ldez
Copy link
Member

ldez commented May 7, 2021

Could you explain why do you want to use several times the same alias for different imports?

@ldez ldez added the feedback required Requires additional feedback label May 7, 2021
@cbandy
Copy link
Contributor Author

cbandy commented May 7, 2021

The way the file works now, I cannot configure these aliases with regex because both are $1$2:

k8s.io/api/apps/v1       -> appsv1
k8s.io/api/batch/v1      -> batchv1
k8s.io/api/batch/v1beta1 -> batchv1beta1
k8s.io/api/core/v1       -> corev1
k8s.io/api/events/v1     -> eventsv1
…

knative.dev/serving/pkg/apis/autoscaling/v1 -> autoscalingv1
knative.dev/serving/pkg/apis/serving/v1     -> servingv1
  importas:
    alias:
      "$1$2": 'k8s.io/api/(\w+)/(v[\w\d]+)'
      "$1$2": 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)'

@ldez
Copy link
Member

ldez commented May 7, 2021

Ok now I understand 👍

I would suggest something like that:

  importas:
    # if set to `true`, force to use alias.
    no-unaliased: true
    # List of aliases
    alias:
      # using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
      - pkg: knative.dev/serving/pkg/apis/serving/v1
        alias: servingv1
      # using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
      - pkg: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
        alias: autoscalingv1alpha1
      # You can specify the package path by regular expression,
      # and alias by regular expression expansion syntax like below.
      # see https://github.com/julz/importas#use-regular-expression for details
      - pkg: knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)
        alias: $1$2
type ImportAsSettings struct {
	Alias       []ImportAsEntry
	NoUnaliased bool `mapstructure:"no-unaliased"`
}

type ImportAsEntry struct {
	Pkg   string
	Alias string
}

@ldez ldez added linter: update version Update version of linter bug Something isn't working and removed feedback required Requires additional feedback linter: update version Update version of linter labels May 7, 2021
The standalone importas takes arguments in the form "{package}:{alias}"
and stores them in a map in which package is the key. The intent is to
describe the one alias that is appropriate for a unique package path.

We cannot use the package path as a map key due to Viper's dot notation.
Instead use a list with package/alias pairs.
@cbandy
Copy link
Contributor Author

cbandy commented May 8, 2021

Done.

Copy link
Member

@ldez ldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ldez ldez changed the title Allow repeated importas aliases importas: allow repeated aliases May 8, 2021
@ldez ldez merged commit 5c6adb6 into golangci:master May 8, 2021
@golangci-automator
Copy link

Hey, @cbandy — we just merged your PR to golangci-lint! 🔥🚀

golangci-lint is built by awesome people like you. Let us say “thanks”: we just invited you to join the GolangCI organization on GitHub.
This will add you to our team of maintainers. Accept the invite by visiting this link.

By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.
More information about contributing is here.

Thanks again!

@cbandy cbandy deleted the importas-config-list branch May 9, 2021 06:02
@ldez ldez added this to the v1.40 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants