github.com/getgauge/gauge@v1.6.9/validation/suggest.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package validation 8 9 import ( 10 "fmt" 11 12 gm "github.com/getgauge/gauge-proto/go/gauge_messages" 13 "github.com/getgauge/gauge/logger" 14 ) 15 16 var message = map[gm.StepValidateResponse_ErrorType]string{ 17 gm.StepValidateResponse_STEP_IMPLEMENTATION_NOT_FOUND: "Add the following missing implementations to fix `Step implementation not found` errors.\n", 18 } 19 20 func showSuggestion(validationErrors validationErrors) { 21 if !HideSuggestion { 22 for t, errs := range groupErrors(validationErrors) { 23 logger.Info(true, getSuggestionMessage(t)) 24 suggestions := filterDuplicateSuggestions(errs) 25 for _, suggestion := range suggestions { 26 logger.Info(true, suggestion) 27 } 28 } 29 } 30 } 31 32 func filterDuplicateSuggestions(errors []StepValidationError) []string { 33 suggestionMap := make(map[string]error) 34 filteredSuggestions := make([]string, 0) 35 for _, err := range errors { 36 if _, ok := suggestionMap[err.Suggestion()]; !ok { 37 suggestionMap[err.Suggestion()] = err 38 filteredSuggestions = append(filteredSuggestions, err.Suggestion()) 39 } 40 } 41 return filteredSuggestions 42 } 43 44 func getSuggestionMessage(t gm.StepValidateResponse_ErrorType) string { 45 if msg, ok := message[t]; ok { 46 return msg 47 } 48 return fmt.Sprintf("Suggestions for fixing `%s` errors.\n", getMessage(t.String())) 49 } 50 51 func groupErrors(validationErrors validationErrors) map[gm.StepValidateResponse_ErrorType][]StepValidationError { 52 errMap := make(map[gm.StepValidateResponse_ErrorType][]StepValidationError) 53 for _, errs := range validationErrors { 54 for _, v := range errs { 55 if e, ok := v.(StepValidationError); ok && e.suggestion != "" { 56 errType := *(v.(StepValidationError).errorType) 57 errMap[errType] = append(errMap[errType], e) 58 } 59 } 60 } 61 return errMap 62 }