From 15e4df5b5dcb0a89116f8f320506a908e2aa81d0 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 15 Aug 2023 20:32:22 -0700 Subject: [PATCH] Go linter cleanup --- astmap.go | 20 -------------------- brain.go | 3 +-- errors.go | 10 +++++----- loading.go | 8 ++++---- parser.go | 6 +++--- regexp.go | 2 -- sorting.go | 18 ++++++++---------- tags.go | 16 ++++++++-------- utils.go | 4 ++-- 9 files changed, 31 insertions(+), 56 deletions(-) diff --git a/astmap.go b/astmap.go index fec8614..95d7c14 100644 --- a/astmap.go +++ b/astmap.go @@ -23,20 +23,6 @@ An example of how unwieldy the code would be if I use the direct AST types: If the ast package structs are updated, update the mappings in this package too. */ -type astRoot struct { - begin astBegin - topics map[string]*astTopic - objects []*astObject -} - -type astBegin struct { - global map[string]string - vars map[string]string - sub map[string]string - person map[string]string - array map[string][]string // Map of string (names) to arrays-of-strings -} - type astTopic struct { triggers []*astTrigger } @@ -48,9 +34,3 @@ type astTrigger struct { redirect string previous string } - -type astObject struct { - name string - language string - code []string -} diff --git a/brain.go b/brain.go index a71d171..7f03fb0 100644 --- a/brain.go +++ b/brain.go @@ -39,7 +39,7 @@ func (rs *RiveScript) Reply(username, message string) (string, error) { } // OK to continue? - if strings.Index(begin, "{ok}") > -1 { + if strings.Contains(begin, "{ok}") { reply, err = rs.getReply(username, message, false, 0) if err != nil { return "", err @@ -360,7 +360,6 @@ func (rs *RiveScript) getReply(username string, message string, isBegin bool, st if len(bucket) > 0 { reply = bucket[rs.randomInt(len(bucket))] } - break } } diff --git a/errors.go b/errors.go index 49a0640..bdbd4cd 100644 --- a/errors.go +++ b/errors.go @@ -4,9 +4,9 @@ import "errors" // The types of errors returned by RiveScript. var ( - ErrDeepRecursion = errors.New("Deep Recursion Detected") - ErrRepliesNotSorted = errors.New("Replies Not Sorted") - ErrNoDefaultTopic = errors.New("No default topic 'random' was found") - ErrNoTriggerMatched = errors.New("No Trigger Matched") - ErrNoReplyFound = errors.New("The trigger matched but yielded no reply") + ErrDeepRecursion = errors.New("deep recursion detected") + ErrRepliesNotSorted = errors.New("replies not sorted") + ErrNoDefaultTopic = errors.New("no default topic 'random' was found") + ErrNoTriggerMatched = errors.New("no trigger matched") + ErrNoReplyFound = errors.New("the trigger matched but yielded no reply") ) diff --git a/loading.go b/loading.go index 20dd7f5..8883237 100644 --- a/loading.go +++ b/loading.go @@ -22,7 +22,7 @@ func (rs *RiveScript) LoadFile(path string) error { fh, err := os.Open(path) if err != nil { - return fmt.Errorf("Failed to open file %s: %s", path, err) + return fmt.Errorf("failed to open file %s: %s", path, err) } defer fh.Close() @@ -53,12 +53,12 @@ func (rs *RiveScript) LoadDirectory(path string, extensions ...string) error { files, err := filepath.Glob(fmt.Sprintf("%s/*", path)) if err != nil { - return fmt.Errorf("Failed to open folder %s: %s", path, err) + return fmt.Errorf("failed to open folder %s: %s", path, err) } // No files matched? if len(files) == 0 { - return fmt.Errorf("No RiveScript source files were found in %s", path) + return fmt.Errorf("no RiveScript source files were found in %s", path) } var anyValid bool @@ -82,7 +82,7 @@ func (rs *RiveScript) LoadDirectory(path string, extensions ...string) error { } if !anyValid { - return fmt.Errorf("No RiveScript source files were found in %s", path) + return fmt.Errorf("no RiveScript source files were found in %s", path) } return nil diff --git a/parser.go b/parser.go index 2ffb760..063ada4 100644 --- a/parser.go +++ b/parser.go @@ -83,7 +83,7 @@ func (rs *RiveScript) parse(path string, lines []string) error { break } } - if foundcond == false { + if !foundcond { previous.condition = append(previous.condition, cond) } } @@ -95,14 +95,14 @@ func (rs *RiveScript) parse(path string, lines []string) error { break } } - if newreply == true { + if newreply { previous.reply = append(previous.reply, reply) } } rs.say("Found previous trigger: %s == %s", trig.Trigger, previous.trigger) } } - if foundtrigger == false { + if !foundtrigger { trigger := new(astTrigger) trigger.trigger = trig.Trigger trigger.reply = trig.Reply diff --git a/regexp.go b/regexp.go index 3799412..656cc81 100644 --- a/regexp.go +++ b/regexp.go @@ -15,8 +15,6 @@ var ( reReplyArray = regexp.MustCompile(`\(@([A-Za-z0-9_]+)\)`) reBotvars = regexp.MustCompile(``) reUservars = regexp.MustCompile(``) - reInput = regexp.MustCompile(``) - reReply = regexp.MustCompile(``) reRandom = regexp.MustCompile(`\{random\}(.+?)\{/random\}`) // Self-contained tags like that contain no nested tag. diff --git a/sorting.go b/sorting.go index 701e341..5d05f47 100644 --- a/sorting.go +++ b/sorting.go @@ -93,9 +93,9 @@ sortTriggerSet sorts a group of triggers in an optimal sorting order. This function has two use cases: -1. Create a sort buffer for "normal" (matchable) triggers, which are triggers - that are NOT accompanied by a %Previous tag. -2. Create a sort buffer for triggers that had %Previous tags. + 1. Create a sort buffer for "normal" (matchable) triggers, which are triggers + that are NOT accompanied by a %Previous tag. + 2. Create a sort buffer for triggers that had %Previous tags. Use the `excludePrevious` parameter to control which one is being done. This function will return a list of sortedTriggerEntry items, and it's intended to @@ -179,7 +179,7 @@ func (rs *RiveScript) sortTriggerSet(triggers []sortedTriggerEntry, excludePrevi } // Start inspecting the trigger's contents. - if strings.Index(pattern, "_") > -1 { + if strings.Contains(pattern, "_") { // Alphabetic wildcard included. cnt := wordCount(pattern, false) rs.say("Has a _ wildcard with %d words", cnt) @@ -191,7 +191,7 @@ func (rs *RiveScript) sortTriggerSet(triggers []sortedTriggerEntry, excludePrevi } else { track[inherits].under = append(track[inherits].under, trig) } - } else if strings.Index(pattern, "#") > -1 { + } else if strings.Contains(pattern, "#") { // Numeric wildcard included. cnt := wordCount(pattern, false) rs.say("Has a # wildcard with %d words", cnt) @@ -203,7 +203,7 @@ func (rs *RiveScript) sortTriggerSet(triggers []sortedTriggerEntry, excludePrevi } else { track[inherits].pound = append(track[inherits].pound, trig) } - } else if strings.Index(pattern, "*") > -1 { + } else if strings.Contains(pattern, "*") { // Wildcard included. cnt := wordCount(pattern, false) rs.say("Has a * wildcard with %d words", cnt) @@ -215,7 +215,7 @@ func (rs *RiveScript) sortTriggerSet(triggers []sortedTriggerEntry, excludePrevi } else { track[inherits].star = append(track[inherits].star, trig) } - } else if strings.Index(pattern, "[") > -1 { + } else if strings.Contains(pattern, "[") { // Optionals included. cnt := wordCount(pattern, false) rs.say("Has optionals with %d words", cnt) @@ -293,9 +293,7 @@ func sortList(dict map[string]string) []string { // Sort the strings of this word-count by their lengths. sortedLengths := track[cnt] sort.Sort(sort.Reverse(byLength(sortedLengths))) - for _, item := range sortedLengths { - output = append(output, item) - } + output = append(output, sortedLengths...) } return output diff --git a/tags.go b/tags.go index 968bd9d..c2e802c 100644 --- a/tags.go +++ b/tags.go @@ -97,7 +97,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string { // Filter in arrays. giveup = 0 - for strings.Index(pattern, "@") > -1 { + for strings.Contains(pattern, "@") { giveup++ if giveup > rs.Depth { break @@ -116,7 +116,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string { // Filter in bot variables. giveup = 0 - for strings.Index(pattern, " -1 { + for strings.Contains(pattern, " rs.Depth { break @@ -135,7 +135,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string { // Filter in user variables. giveup = 0 - for strings.Index(pattern, " -1 { + for strings.Contains(pattern, " rs.Depth { break @@ -158,7 +158,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string { giveup = 0 pattern = strings.Replace(pattern, "", "", -1) pattern = strings.Replace(pattern, "", "", -1) - for strings.Index(pattern, " -1 || strings.Index(pattern, " -1 { + for strings.Contains(pattern, " 50 { break @@ -179,7 +179,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string { } // Recover escaped Unicode symbols. - if rs.UTF8 && strings.Index(pattern, `\u`) > -1 { + if rs.UTF8 && strings.Contains(pattern, `\u`) { // TODO: make this more general pattern = strings.Replace(pattern, `\u0040`, "@", -1) } @@ -286,7 +286,7 @@ func (rs *RiveScript) processTags(username string, message string, reply string, var random []string text := match[1] - if strings.Index(text, "|") > -1 { + if strings.Contains(text, "|") { random = strings.Split(text, "|") } else { random = strings.Split(text, " ") @@ -358,7 +358,7 @@ func (rs *RiveScript) processTags(username string, message string, reply string, target = rs.global } - if strings.Index(data, "=") > -1 { + if strings.Contains(data, "=") { // Assigning the value. parts := strings.Split(data, "=") rs.say("Assign %s variable %s = %s", tag, parts[0], parts[1]) @@ -559,7 +559,7 @@ func (rs *RiveScript) substitute(message string, subs map[string]string, sorted // Convert the placeholders back in. var tries uint - for strings.Index(message, "\x00") > -1 { + for strings.Contains(message, "\x00") { tries++ if tries > rs.Depth { rs.warn("Too many loops in substitution placeholders!") diff --git a/utils.go b/utils.go index 6ba8669..a98a602 100644 --- a/utils.go +++ b/utils.go @@ -46,7 +46,7 @@ func isAtomic(pattern string) bool { // will do. specials := []string{"*", "#", "_", "(", "[", "<", "@"} for _, special := range specials { - if strings.Index(pattern, special) > -1 { + if strings.Contains(pattern, special) { return false } } @@ -114,7 +114,7 @@ func regSplit(text string, delimiter string) []string { result[i] = text[lastStart:element[0]] lastStart = element[1] } - result[len(indexes)] = text[lastStart:len(text)] + result[len(indexes)] = text[lastStart:] return result }