gitee.com/mirrors/gauge@v1.0.6/validation/suggest.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package validation 19 20 import ( 21 "fmt" 22 23 gm "github.com/getgauge/gauge/gauge_messages" 24 ) 25 26 var message = map[gm.StepValidateResponse_ErrorType]string{ 27 gm.StepValidateResponse_STEP_IMPLEMENTATION_NOT_FOUND: "Add the following missing implementations to fix `Step implementation not found` errors.\n", 28 } 29 30 func showSuggestion(validationErrors validationErrors) { 31 if !HideSuggestion { 32 for t, errs := range groupErrors(validationErrors) { 33 fmt.Println(getSuggestionMessage(t)) 34 suggestions := filterDuplicateSuggestions(errs) 35 for _, suggestion := range suggestions { 36 fmt.Println(suggestion) 37 } 38 } 39 } 40 } 41 42 func filterDuplicateSuggestions(errors []StepValidationError) []string { 43 suggestionMap := make(map[string]error) 44 filteredSuggestions := make([]string, 0) 45 for _, err := range errors { 46 if _, ok := suggestionMap[err.Suggestion()]; !ok { 47 suggestionMap[err.Suggestion()] = err 48 filteredSuggestions = append(filteredSuggestions, err.Suggestion()) 49 } 50 } 51 return filteredSuggestions 52 } 53 54 func getSuggestionMessage(t gm.StepValidateResponse_ErrorType) string { 55 if msg, ok := message[t]; ok { 56 return msg 57 } 58 return fmt.Sprintf("Suggestions for fixing `%s` errors.\n", getMessage(t.String())) 59 } 60 61 func groupErrors(validationErrors validationErrors) map[gm.StepValidateResponse_ErrorType][]StepValidationError { 62 errMap := make(map[gm.StepValidateResponse_ErrorType][]StepValidationError) 63 for _, errs := range validationErrors { 64 for _, v := range errs { 65 if e, ok := v.(StepValidationError); ok && e.suggestion != "" { 66 errType := *(v.(StepValidationError).errorType) 67 errMap[errType] = append(errMap[errType], e) 68 } 69 } 70 } 71 return errMap 72 }