Skip to content

Commit

Permalink
Merge pull request #6 from mweb/config
Browse files Browse the repository at this point in the history
Parameters for the layer names
  • Loading branch information
roblaszczak authored Apr 3, 2020
2 parents 2047675 + a5d86d0 commit 39a72cf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ For example

### Allowed `LAYER_NAME`:

The default layer names are as followed. It is possible to set different names
by command line parameters see -domain/-application/-interfaces/-infrastructure
bellow.

var LayersAliases = map[string]Layer{
// Domain
"domain": LayerDomain,
Expand Down Expand Up @@ -117,6 +121,16 @@ you can use

go-cleanarch -ignore-package=github.com/roblaszczak/go-cleanarch/examples/ignore-package/app

### layer names

The layer names can be set to a specific value with the following parameters. Each
parameter stands for on layer.

go-cleanarch -domain dom -application appli -interfaces int -infrastructure outer

This would only allow the domain name to be dom, application hast to be appli,
interafces must be int and infrastructure must be outer.

## Running the tests

make test
Expand Down
37 changes: 9 additions & 28 deletions cleanarch/cleanarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,13 @@ var layersHierarchy = map[Layer]int{
LayerInfrastructure: 4,
}

var layersAliases = map[string]Layer{
// Domain
"domain": LayerDomain,
"entities": LayerDomain,

// Application
"app": LayerApplication,
"application": LayerApplication,
"usecases": LayerApplication,
"usecase": LayerApplication,
"use_cases": LayerApplication,

// Interfaces
"interfaces": LayerInterfaces,
"interface": LayerInterfaces,
"adapters": LayerInterfaces,
"adapter": LayerInterfaces,

// Infrastructure
"infrastructure": LayerInfrastructure,
"infra": LayerInfrastructure,
}

// NewValidator creates new Validator.
func NewValidator() *Validator {
func NewValidator(alias map[string]Layer) *Validator {
filesMetadata := make(map[string]LayerMetadata, 0)
return &Validator{filesMetadata: filesMetadata}
return &Validator{
filesMetadata: filesMetadata,
alias: alias,
}
}

// ValidationError represents error when Clean Architecture rule is not keep.
Expand All @@ -77,6 +57,7 @@ type ValidationError error
// Validator is responsible for Clean Architecture validation.
type Validator struct {
filesMetadata map[string]LayerMetadata
alias map[string]Layer
}

// Validate validates provided path for Clean Architecture rules.
Expand Down Expand Up @@ -185,7 +166,7 @@ func (v *Validator) fileMetadata(path string) LayerMetadata {
return metadata
}

v.filesMetadata[path] = ParseLayerMetadata(path)
v.filesMetadata[path] = ParseLayerMetadata(path, v.alias)
return v.filesMetadata[path]
}

Expand All @@ -196,7 +177,7 @@ type LayerMetadata struct {
}

// ParseLayerMetadata parses metadata of provided path.
func ParseLayerMetadata(path string) LayerMetadata {
func ParseLayerMetadata(path string, alias map[string]Layer) LayerMetadata {
pathParts := strings.Split(path, "/")

metadata := LayerMetadata{}
Expand All @@ -210,7 +191,7 @@ func ParseLayerMetadata(path string) LayerMetadata {
break
}

for alias, layer := range layersAliases {
for alias, layer := range alias {
if pathPart == alias {
metadata.Layer = layer
continue
Expand Down
27 changes: 25 additions & 2 deletions cleanarch/cleanarch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import (
"github.com/roblaszczak/go-cleanarch/cleanarch"
)

var layersAliases = map[string]cleanarch.Layer{
// Domain
"domain": cleanarch.LayerDomain,
"entities": cleanarch.LayerDomain,

// Application
"app": cleanarch.LayerApplication,
"application": cleanarch.LayerApplication,
"usecases": cleanarch.LayerApplication,
"usecase": cleanarch.LayerApplication,
"use_cases": cleanarch.LayerApplication,

// Interfaces
"interfaces": cleanarch.LayerInterfaces,
"interface": cleanarch.LayerInterfaces,
"adapters": cleanarch.LayerInterfaces,
"adapter": cleanarch.LayerInterfaces,

// Infrastructure
"infrastructure": cleanarch.LayerInfrastructure,
"infra": cleanarch.LayerInfrastructure,
}

func init() {
cleanarch.Log.SetOutput(os.Stderr)
}
Expand Down Expand Up @@ -41,7 +64,7 @@ func TestValidator_Validate(t *testing.T) {

for _, c := range testCases {
t.Run(c.Path, func(t *testing.T) {
validator := cleanarch.NewValidator()
validator := cleanarch.NewValidator(layersAliases)
valid, errors, err := validator.Validate(c.Path, c.IgnoreTests, c.IgnoredPackages)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -110,7 +133,7 @@ func TestParseLayerMetadata(t *testing.T) {

for _, c := range testCases {
t.Run(c.Path, func(t *testing.T) {
metadata := cleanarch.ParseLayerMetadata(c.Path)
metadata := cleanarch.ParseLayerMetadata(c.Path, layersAliases)

if !reflect.DeepEqual(metadata, c.ExpectedFileMetadata) {
t.Errorf("invalid metadata: %+v, expected %+v", metadata, c.ExpectedFileMetadata)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/roblaszczak/go-cleanarch

go 1.12
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/roblaszczak/go-cleanarch/cleanarch"
)

var domainAliases = []string{"domain", "entities"}
var applicationAliases = []string{"app", "application", "usecases", "usecase", "use_cases"}
var interfacesAliases = []string{"interfaces", "interface", "adapters", "adapter"}
var infrastructureAliases = []string{"infrastructure", "infra"}

func main() {
ignoredPackages := sliceFlag{}

Expand All @@ -21,10 +26,21 @@ func main() {
"for example you can use`-ignore-package github.com/roblaszczak/go-cleanarch/infrastructure` to import "+
"this package to the domain",
)
domain := flag.String("domain", "", "name of the domain layer")
application := flag.String("application", "", "name of the application layer")
interfaces := flag.String("interfaces", "", "name of the interfaces layer")
infrastructure := flag.String("infrastructure", "", "name of the infrastructure layer")

flag.Parse()
var path string

aliases := make(map[string]cleanarch.Layer)

addAliases(aliases, *domain, domainAliases, cleanarch.LayerDomain)
addAliases(aliases, *application, applicationAliases, cleanarch.LayerApplication)
addAliases(aliases, *interfaces, interfacesAliases, cleanarch.LayerInterfaces)
addAliases(aliases, *infrastructure, infrastructureAliases, cleanarch.LayerInfrastructure)

if *debug {
cleanarch.Log.SetOutput(os.Stderr)
}
Expand All @@ -41,7 +57,7 @@ func main() {

fmt.Printf("[cleanarch] checking %s\n", path)

validator := cleanarch.NewValidator()
validator := cleanarch.NewValidator(aliases)
isValid, errors, err := validator.Validate(path, *ignoreTests, ignoredPackages)
if err != nil {
panic(err)
Expand All @@ -58,3 +74,13 @@ func main() {

os.Exit(0)
}

func addAliases(aliases map[string]cleanarch.Layer, name string, names []string, layer cleanarch.Layer) {
if len(name) > 0 {
aliases[name] = layer
} else {
for _, n := range names {
aliases[n] = layer
}
}
}

0 comments on commit 39a72cf

Please sign in to comment.