Skip to content

Commit

Permalink
Go linter cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsle committed Aug 16, 2023
1 parent 5fe90d2 commit 15e4df5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 56 deletions.
20 changes: 0 additions & 20 deletions astmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -48,9 +34,3 @@ type astTrigger struct {
redirect string
previous string
}

type astObject struct {
name string
language string
code []string
}
3 changes: 1 addition & 2 deletions brain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
10 changes: 5 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
8 changes: 4 additions & 4 deletions loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ var (
reReplyArray = regexp.MustCompile(`\(@([A-Za-z0-9_]+)\)`)
reBotvars = regexp.MustCompile(`<bot (.+?)>`)
reUservars = regexp.MustCompile(`<get (.+?)>`)
reInput = regexp.MustCompile(`<input([1-9])>`)
reReply = regexp.MustCompile(`<reply([1-9])>`)
reRandom = regexp.MustCompile(`\{random\}(.+?)\{/random\}`)

// Self-contained tags like <set> that contain no nested tag.
Expand Down
18 changes: 8 additions & 10 deletions sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -116,7 +116,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string {

// Filter in bot variables.
giveup = 0
for strings.Index(pattern, "<bot ") > -1 {
for strings.Contains(pattern, "<bot ") {
giveup++
if giveup > rs.Depth {
break
Expand All @@ -135,7 +135,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string {

// Filter in user variables.
giveup = 0
for strings.Index(pattern, "<get ") > -1 {
for strings.Contains(pattern, "<get ") {
giveup++
if giveup > rs.Depth {
break
Expand All @@ -158,7 +158,7 @@ func (rs *RiveScript) triggerRegexp(username string, pattern string) string {
giveup = 0
pattern = strings.Replace(pattern, "<input>", "<input1>", -1)
pattern = strings.Replace(pattern, "<reply>", "<reply1>", -1)
for strings.Index(pattern, "<input") > -1 || strings.Index(pattern, "<reply") > -1 {
for strings.Contains(pattern, "<input") || strings.Contains(pattern, "<reply") {
giveup++
if giveup > 50 {
break
Expand All @@ -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)
}
Expand Down Expand Up @@ -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, " ")
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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!")
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 15e4df5

Please sign in to comment.