Skip to content

Commit

Permalink
Replace container.Contains with slices.Contains
Browse files Browse the repository at this point in the history
slices will be moving from exp to standard library in 1.21, so let's use it since we are including it already.
  • Loading branch information
asmaloney committed Jul 22, 2023
1 parent e5453c0 commit 644d2f6
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 56 deletions.
4 changes: 2 additions & 2 deletions actr/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"strings"

"github.com/asmaloney/gactar/util/container"
"golang.org/x/exp/slices"
)

// validBufferStates is a list of the valid buffer states to use with the _status chunk
Expand Down Expand Up @@ -75,7 +75,7 @@ func (b List) Names() (names []string) {
func (b List) Has(name string) bool {
names := b.Names()

return container.Contains(name, names)
return slices.Contains(names, name)
}

// At returns the buffer at "index" or nil if out of range
Expand Down
10 changes: 7 additions & 3 deletions actr/chunk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package actr

import "github.com/asmaloney/gactar/util/container"
import (
"golang.org/x/exp/slices"

"github.com/asmaloney/gactar/util/container"
)

// See "Default Chunks" pg. 80 of ACT-R manual.
var reservedChunkNames = []string{"busy", "clear", "empty", "error", "failure", "free", "full", "requested", "unrequested"}
Expand All @@ -20,7 +24,7 @@ func IsInternalChunkType(name string) bool {
// IsReservedType checks if the slot name is reserved.
// See "Default Chunks" pg. 80 of ACT-R manual.
func IsReservedType(name string) bool {
return container.Contains(name, reservedChunkNames)
return slices.Contains(reservedChunkNames, name)
}

// LookupChunk looks up the chunk (by type name) in the model and returns it (or nil if it does not exist).
Expand All @@ -45,7 +49,7 @@ func (c Chunk) IsInternal() bool {

// HasSlot checks if the slot name exists on this chunk.
func (chunk Chunk) HasSlot(slot string) bool {
return container.Contains(slot, chunk.SlotNames)
return slices.Contains(chunk.SlotNames, slot)
}

// SlotIndex returns the slot index (indexed from 1) of the slot name or -1 if not found.
Expand Down
4 changes: 2 additions & 2 deletions actr/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package actr

import "github.com/asmaloney/gactar/util/container"
import "golang.org/x/exp/slices"

type ACTRLogLevel string

Expand All @@ -11,5 +11,5 @@ var ACTRLoggingLevels = []string{
}

func ValidLogLevel(e string) bool {
return container.Contains(e, ACTRLoggingLevels)
return slices.Contains(ACTRLoggingLevels, e)
}
6 changes: 2 additions & 4 deletions actr/params/params.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Package params implements parsed parameter information.
package params

import (
"github.com/asmaloney/gactar/util/container"
)
import "golang.org/x/exp/slices"

var boolean = []string{
"true",
Expand All @@ -25,7 +23,7 @@ type Param struct {
}

func (v Value) AsBool() (bool, error) {
if (v.ID == nil) || !container.Contains(*v.ID, boolean) {
if (v.ID == nil) || !slices.Contains(boolean, *v.ID) {
return false, ErrInvalidType{ExpectedType: Boolean}
}

Expand Down
5 changes: 3 additions & 2 deletions amod/amod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"fmt"
"strings"

"golang.org/x/exp/slices"

"github.com/alecthomas/participle/v2"

"github.com/asmaloney/gactar/actr"
"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/modules"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/issues"
)

Expand Down Expand Up @@ -335,7 +336,7 @@ func addChunks(model *actr.Model, log *issueLog, chunks []*chunkDecl) {
func addInitializers(model *actr.Model, log *issueLog, module modules.ModuleInterface, buffer buffer.Interface, init *namedInitializer) {
// Check for duplicate initializer names.
// Note that this can't be checked in validateInitialization because model.ExplicitChunks is not filled in yet.
if init.ChunkName != nil && container.Contains(*init.ChunkName, model.ExplicitChunks) {
if init.ChunkName != nil && slices.Contains(model.ExplicitChunks, *init.ChunkName) {
log.errorTR(init.Tokens, 0, 1, "duplicate chunk name %q found in initialization", *init.ChunkName)
return
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"strings"

"golang.org/x/exp/slices"

"github.com/jwalton/gchalk"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/asmaloney/gactar/modes/defaultmode"
"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/filesystem"
"github.com/asmaloney/gactar/util/frameworkutil"
"github.com/asmaloney/gactar/util/version"
Expand Down Expand Up @@ -284,7 +285,7 @@ func createFrameworks(settings *cli.Settings, flags *pflag.FlagSet) (frameworks

// If the user asked for "all", then clear the list.
// frameworkutil.CreateFrameworks() will create all valid ones.
if container.Contains("all", list) {
if slices.Contains(list, "all") {
list = []string{}
}

Expand Down
5 changes: 3 additions & 2 deletions framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package framework
import (
"time"

"golang.org/x/exp/slices"

"github.com/asmaloney/gactar/actr"

"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/issues"
"github.com/asmaloney/gactar/util/version"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func (l List) Exists(framework string) bool {

// IsValidFramework returns if the framework name is in our list of valid ones or not.
func IsValidFramework(frameworkName string) bool {
return container.Contains(frameworkName, ValidFrameworks)
return slices.Contains(ValidFrameworks, frameworkName)
}

// ValidNamedFrameworks returns the list of all valid framework names without "all".
Expand Down
4 changes: 3 additions & 1 deletion modes/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"sync"

"golang.org/x/exp/slices"

"github.com/jwalton/gchalk"
"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
Expand Down Expand Up @@ -206,7 +208,7 @@ func (w Web) runModelHandler(rw http.ResponseWriter, req *http.Request) {
func (w Web) normalizeFrameworkList(list []string) (normalized []string) {
normalized = list

if list == nil || container.Contains("all", list) {
if list == nil || slices.Contains(list, "all") {
normalized = w.settings.Frameworks.Names()
}

Expand Down
16 changes: 0 additions & 16 deletions util/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@ import (
"sort"
)

// Contains returns whether the "value" exists in the "list".
// Case-sensitive.
func Contains(value string, list []string) bool {
if value == "" {
return false
}

for _, v := range list {
if v == value {
return true
}
}

return false
}

// GetIndex1 returns the index (indexed from 1) of "value" or -1 if not found.
// Case-sensitive.
func GetIndex1(value string, list []string) int {
Expand Down
22 changes: 0 additions & 22 deletions util/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@ package container

import "testing"

func TestContains(t *testing.T) {
t.Parallel()

list := []string{"a", "b", "c", "d"}

contains := Contains("a", list)
if !contains {
t.Errorf("Incorrect return: expected true")
}

contains = Contains("", list)
if contains {
t.Errorf("Incorrect return: expected false")
}

// Check case-sensitivity
contains = Contains("C", list)
if contains {
t.Errorf("Incorrect return: expected false")
}
}

func TestGetIndex1(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 644d2f6

Please sign in to comment.