Skip to content

Commit

Permalink
Merge pull request #77 from k1LoW/fix-lint-rules
Browse files Browse the repository at this point in the history
Fix lint rules
  • Loading branch information
k1LoW committed Feb 25, 2019
2 parents bcdfe25 + 231ce83 commit bec5702
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
49 changes: 32 additions & 17 deletions config/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Lint struct {
RequireTableComment RequireTableComment `yaml:"requireTableComment"`
RequireColumnComment RequireColumnComment `yaml:"requireColumnComment"`
NoRelationTables NoRelationTables `yaml:"noRelationTables"`
UnrelatedTable UnrelatedTable `yaml:"unrelatedTable"`
ColumnCount ColumnCount `yaml:"columnCount"`
}

Expand All @@ -27,7 +27,8 @@ type Rule interface {

// RequireTableComment check table comment
type RequireTableComment struct {
Enabled bool `yaml:"enabled"`
Enabled bool `yaml:"enabled"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
Expand All @@ -40,7 +41,7 @@ func (r RequireTableComment) Check(s *schema.Schema) []RuleWarn {
msgFmt := "%s: table comment required."
warns := []RuleWarn{}
for _, t := range s.Tables {
if t.Comment == "" {
if !contains(r.Exclude, t.Name) && t.Comment == "" {
warns = append(warns, RuleWarn{
Message: fmt.Sprintf(msgFmt, t.Name),
})
Expand All @@ -51,7 +52,8 @@ func (r RequireTableComment) Check(s *schema.Schema) []RuleWarn {

// RequireColumnComment check column comment
type RequireColumnComment struct {
Enabled bool `yaml:"enabled"`
Enabled bool `yaml:"enabled"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
Expand All @@ -65,7 +67,7 @@ func (r RequireColumnComment) Check(s *schema.Schema) []RuleWarn {
warns := []RuleWarn{}
for _, t := range s.Tables {
for _, c := range t.Columns {
if c.Comment == "" {
if !contains(r.Exclude, c.Name) && c.Comment == "" {
warns = append(warns, RuleWarn{
Message: fmt.Sprintf(msgFmt, t.Name, c.Name),
})
Expand All @@ -75,41 +77,45 @@ func (r RequireColumnComment) Check(s *schema.Schema) []RuleWarn {
return warns
}

// NoRelationTables check no relation table
type NoRelationTables struct {
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
// UnrelatedTable check isolated table
type UnrelatedTable struct {
Enabled bool `yaml:"enabled"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
func (r NoRelationTables) IsEnabled() bool {
func (r UnrelatedTable) IsEnabled() bool {
return r.Enabled
}

// Check table relation
func (r NoRelationTables) Check(s *schema.Schema) []RuleWarn {
msgFmt := "schema has too many no relation tables. [%d/%d]"
func (r UnrelatedTable) Check(s *schema.Schema) []RuleWarn {
msgFmt := "unrelated (isolated) table exists. [%d]"
warns := []RuleWarn{}
tableMap := map[string]*schema.Table{}
for _, t := range s.Tables {
if contains(r.Exclude, t.Name) {
continue
}
tableMap[t.Name] = t
}
for _, rl := range s.Relations {
delete(tableMap, rl.Table.Name)
delete(tableMap, rl.ParentTable.Name)
}
if len(tableMap) > r.Max {
if len(tableMap) > 0 {
warns = append(warns, RuleWarn{
Message: fmt.Sprintf(msgFmt, len(tableMap), r.Max),
Message: fmt.Sprintf(msgFmt, len(tableMap)),
})
}
return warns
}

// ColumnCount check table column count
type ColumnCount struct {
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
Exclude []string `yaml:"exclude"`
}

// IsEnabled return Rule is enabled or not
Expand All @@ -122,11 +128,20 @@ func (r ColumnCount) Check(s *schema.Schema) []RuleWarn {
msgFmt := "%s has too many columns. [%d/%d]"
warns := []RuleWarn{}
for _, t := range s.Tables {
if len(t.Columns) > r.Max {
if !contains(r.Exclude, t.Name) && len(t.Columns) > r.Max {
warns = append(warns, RuleWarn{
Message: fmt.Sprintf(msgFmt, t.Name, len(t.Columns), r.Max),
})
}
}
return warns
}

func contains(s []string, e string) bool {
for _, v := range s {
if e == v {
return true
}
}
return false
}
5 changes: 2 additions & 3 deletions config/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func TestRequireColumnComment(t *testing.T) {
}
}

func TestNoRelationTables(t *testing.T) {
r := NoRelationTables{
func TestUnrelatedTable(t *testing.T) {
r := UnrelatedTable{
Enabled: true,
Max: 0,
}
s := newTestSchema()
warns := r.Check(s)
Expand Down
3 changes: 1 addition & 2 deletions testdata/additional_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ lint:
enabled: true
requireColumnComment:
enabled: true
noRelationTables:
unrelatedTable:
enabled: true
max: 2
columnCount:
enabled: true
max: 10
Expand Down

0 comments on commit bec5702

Please # to comment.