Skip to content

linter for Go source code that checks whether imports break encapsulation

License

Notifications You must be signed in to change notification settings

Syuparn/pkgaffinity

Repository files navigation

pkgaffinity

Test Go Report Card MIT License

linter for Go source code that checks whether imports break encapsulation

Usage

Define Rules

pkgaffinity checks whether your code follows package import rules.

You can define rules in config files (see .pkgaffinity.yaml for details).

Anti-Affinity Rules

Rules in pkgaffinity are described as package path anti-affinity in namespace. Any package in anti-affinity rule are not allowed to import any other package in the rule.

Anti-Affinity List

Anti-affinity list forbids a package under a path prefix import packages under any other path prefixes.

# package tree
- github.com/foo/bar
  - baz
    - quux
  - pkg
    - hoge
      - fuga
version: v1alpha1
antiAffinityRules:
  lists:
    - label: separate_hoge_and_baz
      pathPrefixes:
        - github.com/foo/bar/baz
        - github.com/foo/bar/pkg/hoge

Each path prefix is treated as import boundary. Any import across the boundaries is forbidden (ex: import github.com/foo/bar/baz/quux from github.com/foo/bar/pkg/hoge/fuga).

Anti-Affinity Group

Anti-affinity group defines anti-affinity rules to all packages under group path prefix.

# package tree
- github.com/syuparn/pkgaffinity
  - pkg
    - config
      - domain
      - usecase
    - importchecker
      - domain
      - usecase
version: v1alpha1
antiAffinityRules:
  groups:
    - pathPrefix: github.com/syuparn/pkgaffinity/pkg

Group github.com/syuparn/pkgaffinity/pkg treats subpackages directly under group (config and importchecker) as import boundaries. Any import across the boundaries is forbidden (ex: import github.com/syuparn/pkgaffinity/pkg/config/usecase from github.com/syuparn/pkgaffinity/pkg/importchecker/domain). This is suitable for microservices or modular monolith.

Run command

This linter works as a single binary.

# install
$ go install github.com/syuparn/pkgaffinity/cmd/pkgaffinity@latest

If any violation found, it shows the reason.

# NOTE: this is only an example. Actuallly pkgaffinity follows all rules.
$ ./cmd/pkgaffinity/pkgaffinity ./...
package github.com/syuparn/pkgaffinity/pkg/config/domain: import "github.com/syuparn/pkgaffinity/pkg/importchecker/domain" breaks anti-affinity group rule `github.com/syuparn/pkgaffinity/pkg`
package github.com/syuparn/pkgaffinity/pkg/config/domain: import "github.com/syuparn/pkgaffinity/pkg/importchecker/domain" breaks anti-affinity group rule `github.com/syuparn/pkgaffinity/pkg`
pkgaffinity: check failed: violations found
pkgaffinity: check failed: violations found

$ echo $?
1

Contributions

Any contributions are welcome!

About

linter for Go source code that checks whether imports break encapsulation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages