Skip to content

Commit

Permalink
Usability improvements
Browse files Browse the repository at this point in the history
 - Remove `displayorder` from Group, display based on position in array
 - Update `regexp` in Group to be an array that is joined together to
   provide multiple matches per block which are easier to read than
   using a `|` in the single regexp
  • Loading branch information
coreydaley committed Mar 9, 2023
1 parent bff0bdf commit 1aef70f
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 310 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ Lets `goio` know whether to match agains the files `name` or the files `path` re
### Groups
An array of Group definitions

Each group definition is a rule that tells `goio` how you would like the `imports` in your Go files organized. Each definition represents a block of import statements, describing the order in which that block should be displayed and how to identify the items that it should contain.
Each group definition is a rule that tells `goio` how you would like the `imports` in your Go files organized. Each definition represents a block of import statements, describing how to identify the items that it should contain. Group blocks are displayed in **the order that they appear in the array**.

#### Description
A string, valid values are any valid string value

A friendly name to identify the definition by instead of trying to decipher the regular expression each time to remember what it does.

#### RegExp
A string, valid values are any valid Go regular expression.
An array of strings, valid values are any valid Go regular expression.

A well formatted Regular Expression that is used to match against. Be as specific as possible.

Note:
**Note:**

There is one keyword that is available for the RegExp value that is a special keyword, it is `%{module}%`. This keyword automatically creates a regular expression that matches the current module name as defined by the go.mod file. To ensure that it captures the correct imports you should always set the `MatchOrder` to `0` for this definition.

Expand All @@ -121,9 +121,3 @@ An integer, valid values are -n...n
Tells `goio` which order the definitions should be matched against in. Lower numbers are first, higher numbers are last.

It is important to ensure the correct `matchorder` is used, expecially if any of your `regexp` have any kind of overlap, such as having a module name of `github.com/example/mymodule` and a group definition for `github.com/example`. You would want to make sure that your `module` definition was matched first or those imports would get rolled into the `github.com/example` one because it is less specific.


#### DisplayOrder
An integer, valid values are -n...n

The order in which the blocks of imports should be displayed in each file. Lower numbers are first, higher numbers are last
16 changes: 8 additions & 8 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ excludes:
- matchtype: name
regexp: ^vendor$
groups:
- description: module
displayorder: 2
matchorder: 0
regexp: "%{module}%"
- description: standard
displayorder: 0
matchorder: 1
regexp: ^[a-zA-Z0-9\/]+$
regexp:
- ^[a-zA-Z0-9\/]+$
- description: other
displayorder: 1
matchorder: 2
regexp: '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
regexp:
- '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
- description: module
matchorder: 0
regexp:
- "%{module}%"
24 changes: 12 additions & 12 deletions examples/openshift_kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ excludes:
regexp: ^vendor$
groups:
- description: standard
displayorder: 0
matchorder: 0
regexp: ^[a-zA-Z0-9\/]+$
regexp:
- ^[a-zA-Z0-9\/]+$
- description: kubernetes
displayorder: 2
matchorder: 1
regexp: ^k8s\.io
- description: module
displayorder: 4
matchorder: 2
regexp: "%{module}%"
regexp:
- ^k8s\.io
- description: openshift
displayorder: 3
matchorder: 3
regexp: ^github\.com\/openshift
regexp:
- ^github\.com\/openshift
- description: other
displayorder: 1
matchorder: 4
regexp: '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
regexp:
- '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
- description: module
matchorder: 2
regexp:
- "%{module}%"
16 changes: 8 additions & 8 deletions goio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ excludes:
- matchtype: name
regexp: ^testdata$
groups:
- description: module
displayorder: 2
matchorder: 0
regexp: "%{module}%"
- description: standard
displayorder: 0
matchorder: 1
regexp: ^[a-zA-Z0-9\/]+$
regexp:
- ^[a-zA-Z0-9\/]+$
- description: other
displayorder: 1
matchorder: 2
regexp: '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
regexp:
- '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
- description: module
matchorder: 0
regexp:
- "%{module}%"
9 changes: 4 additions & 5 deletions pkg/api/v1/types.go → pkg/api/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 v1
package v1alpha1

import (
"regexp"
Expand All @@ -35,10 +35,9 @@ type Exclude struct {
}

type Group struct {
MatchOrder int
DisplayOrder int
Description string
RegExp string
MatchOrder int
Description string
RegExp []string
}

const (
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (

"gopkg.in/yaml.v3"

v1 "github.com/go-imports-organizer/goio/pkg/api/v1"
v1alpha1 "github.com/go-imports-organizer/goio/pkg/api/v1alpha1"
)

func Load(file string) (v1.Config, error) {
func Load(file string) (v1alpha1.Config, error) {
var configFile []byte
var err error
if configFile, err = os.ReadFile(file); err != nil {
return v1.Config{}, fmt.Errorf("unable to read configuration file %s: %s", file, err.Error())
return v1alpha1.Config{}, fmt.Errorf("unable to read configuration file %s: %s", file, err.Error())
}

var config v1.Config
var config v1alpha1.Config
if err = yaml.Unmarshal(configFile, &config); err != nil {
return v1.Config{}, fmt.Errorf("unable to unmarshal file %s: %s", file, err.Error())
return v1alpha1.Config{}, fmt.Errorf("unable to unmarshal file %s: %s", file, err.Error())
}

return config, nil
Expand Down
35 changes: 16 additions & 19 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

v1 "github.com/go-imports-organizer/goio/pkg/api/v1"
v1alpha1 "github.com/go-imports-organizer/goio/pkg/api/v1alpha1"
)

func TestLoad(t *testing.T) {
Expand All @@ -15,7 +15,7 @@ func TestLoad(t *testing.T) {
tests := []struct {
name string
args args
want v1.Config
want v1alpha1.Config
wantErr bool
wantErrMsg string
}{
Expand All @@ -24,8 +24,8 @@ func TestLoad(t *testing.T) {
args: args{
file: "../../test/testdata/config/works.yaml",
},
want: v1.Config{
Excludes: []v1.Exclude{
want: v1alpha1.Config{
Excludes: []v1alpha1.Exclude{
{
MatchType: "name",
RegExp: "^\\.git$",
Expand All @@ -35,24 +35,21 @@ func TestLoad(t *testing.T) {
RegExp: "^vendor$",
},
},
Groups: []v1.Group{
Groups: []v1alpha1.Group{
{
MatchOrder: 0,
DisplayOrder: 2,
Description: "module",
RegExp: "%{module}%",
MatchOrder: 0,
Description: "module",
RegExp: []string{"%{module}%"},
},
{
MatchOrder: 1,
DisplayOrder: 0,
Description: "standard",
RegExp: "^[a-zA-Z0-9\\/]+$",
MatchOrder: 1,
Description: "standard",
RegExp: []string{"^[a-zA-Z0-9\\/]+$"},
},
{
MatchOrder: 2,
DisplayOrder: 1,
Description: "other",
RegExp: "[a-zA-Z0-9]+\\.[a-zA-Z0-9]+/",
MatchOrder: 2,
Description: "other",
RegExp: []string{"[a-zA-Z0-9]+\\.[a-zA-Z0-9]+/"},
},
},
},
Expand All @@ -64,7 +61,7 @@ func TestLoad(t *testing.T) {
args: args{
file: "../../test/testdata/config/malformed.yaml",
},
want: v1.Config{},
want: v1alpha1.Config{},
wantErr: true,
wantErrMsg: "unable to unmarshal file",
},
Expand All @@ -73,7 +70,7 @@ func TestLoad(t *testing.T) {
args: args{
file: "../../test/testdata/config/notexist.yaml",
},
want: v1.Config{},
want: v1alpha1.Config{},
wantErr: true,
wantErrMsg: "unable to read configuration file",
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/excludes/excludes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"regexp"
"strings"

v1 "github.com/go-imports-organizer/goio/pkg/api/v1"
v1alpha1 "github.com/go-imports-organizer/goio/pkg/api/v1alpha1"
)

func Build(excludes []v1.Exclude) (*regexp.Regexp, *regexp.Regexp) {
func Build(excludes []v1alpha1.Exclude) (*regexp.Regexp, *regexp.Regexp) {
var excludeByPath []string
var excludeByName []string

for _, exclude := range excludes {
switch exclude.MatchType {
case v1.ExcludeMatchTypeName:
case v1alpha1.ExcludeMatchTypeName:
excludeByName = append(excludeByName, exclude.RegExp)
case v1.ExcludeMatchTypeRelativePath:
case v1alpha1.ExcludeMatchTypeRelativePath:
excludeByPath = append(excludeByPath, exclude.RegExp)
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/excludes/excludes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"regexp"
"testing"

v1 "github.com/go-imports-organizer/goio/pkg/api/v1"
v1alpha1 "github.com/go-imports-organizer/goio/pkg/api/v1alpha1"
)

func TestBuild(t *testing.T) {
type args struct {
excludes []v1.Exclude
excludes []v1alpha1.Exclude
}
tests := []struct {
name string
Expand All @@ -21,7 +21,7 @@ func TestBuild(t *testing.T) {
{
name: "only name excludes",
args: args{
excludes: []v1.Exclude{
excludes: []v1alpha1.Exclude{
{
MatchType: "name",
RegExp: "^name-one$",
Expand All @@ -38,7 +38,7 @@ func TestBuild(t *testing.T) {
{
name: "only path excludes",
args: args{
excludes: []v1.Exclude{
excludes: []v1alpha1.Exclude{
{
MatchType: "path",
RegExp: "^path-one$",
Expand All @@ -55,7 +55,7 @@ func TestBuild(t *testing.T) {
{
name: "name and path excludes",
args: args{
excludes: []v1.Exclude{
excludes: []v1alpha1.Exclude{
{
MatchType: "name",
RegExp: "^name-one$",
Expand Down
16 changes: 7 additions & 9 deletions pkg/groups/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@ import (
"sort"
"strings"

v1 "github.com/go-imports-organizer/goio/pkg/api/v1"
v1alpha1 "github.com/go-imports-organizer/goio/pkg/api/v1alpha1"
"github.com/go-imports-organizer/goio/pkg/sorter"
)

func Build(groups []v1.Group, goModuleName string) ([]v1.RegExpMatcher, []string) {
groupRegExpMatchers := []v1.RegExpMatcher{}
func Build(groups []v1alpha1.Group, goModuleName string) ([]v1alpha1.RegExpMatcher, []string) {
groupRegExpMatchers := []v1alpha1.RegExpMatcher{}
displayOrder := []string{}

sort.Sort(sorter.SortGroupsByDisplayOrder(groups))
for _, group := range groups {
displayOrder = append(displayOrder, group.Description)
}

sort.Sort(sorter.SortGroupsByMatchOrder(groups))

for i := range groups {
if groups[i].RegExp == `%{module}%` {
groups[i].RegExp = fmt.Sprintf("^%s", strings.ReplaceAll(strings.ReplaceAll(goModuleName, `.`, `\.`), `/`, `\/`))
}
groupRegExpMatchers = append(groupRegExpMatchers, v1.RegExpMatcher{
r := strings.Join(groups[i].RegExp, "|")
r = strings.Replace(r, `%{module}%`, fmt.Sprintf("^%s", strings.ReplaceAll(strings.ReplaceAll(goModuleName, `.`, `\.`), `/`, `\/`)), -1)
groupRegExpMatchers = append(groupRegExpMatchers, v1alpha1.RegExpMatcher{
Bucket: groups[i].Description,
RegExp: regexp.MustCompile(groups[i].RegExp),
RegExp: regexp.MustCompile(r),
},
)
}
Expand Down
Loading

0 comments on commit 1aef70f

Please sign in to comment.