Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix lint rules #77

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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