From 959d0183a1cf17e787bc827069c58525405f0a20 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 25 Feb 2019 14:51:47 +0900 Subject: [PATCH] Fix lint rules - `noRelationTables` -> `unrelatedTable` - Add `exclude` --- config/lint.go | 49 +++++++++++++++++++++++------------- config/lint_test.go | 5 ++-- testdata/additional_data.yml | 3 +-- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/config/lint.go b/config/lint.go index f366274c0..876f491ce 100644 --- a/config/lint.go +++ b/config/lint.go @@ -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"` } @@ -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 @@ -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), }) @@ -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 @@ -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), }) @@ -75,32 +77,35 @@ 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 @@ -108,8 +113,9 @@ func (r NoRelationTables) Check(s *schema.Schema) []RuleWarn { // 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 @@ -122,7 +128,7 @@ 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), }) @@ -130,3 +136,12 @@ func (r ColumnCount) Check(s *schema.Schema) []RuleWarn { } return warns } + +func contains(s []string, e string) bool { + for _, v := range s { + if e == v { + return true + } + } + return false +} diff --git a/config/lint_test.go b/config/lint_test.go index e683a79ad..887a5f1a0 100644 --- a/config/lint_test.go +++ b/config/lint_test.go @@ -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) diff --git a/testdata/additional_data.yml b/testdata/additional_data.yml index 6fdaee8d8..1f0ccb324 100644 --- a/testdata/additional_data.yml +++ b/testdata/additional_data.yml @@ -4,9 +4,8 @@ lint: enabled: true requireColumnComment: enabled: true - noRelationTables: + unrelatedTable: enabled: true - max: 2 columnCount: enabled: true max: 10