Skip to content

refactor: use slices.Contains to simplify code #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions x/wasm/migrations/v2/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package v2
import (
"encoding/json"
"fmt"
"slices"

"github.com/cosmos/gogoproto/jsonpb"

Expand Down Expand Up @@ -60,10 +61,8 @@ func validateAccessType(i interface{}) error {
if a == AccessTypeUnspecified {
return errorsmod.Wrap(types.ErrEmpty, "type")
}
for _, v := range AllAccessTypes {
if v == a {
return nil
}
if slices.Contains(AllAccessTypes, a) {
return nil
}
return errorsmod.Wrapf(types.ErrInvalid, "unknown type: %q", a)
}
Expand Down
7 changes: 3 additions & 4 deletions x/wasm/types/json_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"slices"
)

// isJSONObjectWithTopLevelKey returns true if the given bytes are a valid JSON object
Expand All @@ -22,10 +23,8 @@ func isJSONObjectWithTopLevelKey(jsonBytes RawContractMessage, allowedKeys []str

// Loop is executed exactly once
for topLevelKey := range document {
for _, allowedKey := range allowedKeys {
if allowedKey == topLevelKey {
return true, nil
}
if slices.Contains(allowedKeys, topLevelKey) {
return true, nil
}
return false, nil
}
Expand Down
14 changes: 4 additions & 10 deletions x/wasm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"encoding/json"
"slices"

"github.com/cosmos/gogoproto/jsonpb"
"github.com/pkg/errors"
Expand Down Expand Up @@ -113,10 +114,8 @@
if a == AccessTypeUnspecified {
return errorsmod.Wrap(ErrEmpty, "type")
}
for _, v := range AllAccessTypes {
if v == a {
return nil
}
if slices.Contains(AllAccessTypes, a) {
return nil
}
return errorsmod.Wrapf(ErrInvalid, "unknown type: %q", a)
}
Expand All @@ -143,12 +142,7 @@
case AccessTypeEverybody:
return true
case AccessTypeAnyOfAddresses:
for _, v := range a.Addresses {
if v == actor.String() {
return true
}
}
return false
return slices.Contains(a.Addresses, actor.String())

Check warning on line 145 in x/wasm/types/params.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/params.go#L145

Added line #L145 was not covered by tests
default:
panic("unknown type")
}
Expand Down
15 changes: 5 additions & 10 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"
"reflect"
"slices"

wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -251,11 +252,8 @@ func (a *AbsoluteTxPosition) Bytes() []byte {
// ValidateBasic syntax checks
func (c ContractCodeHistoryEntry) ValidateBasic() error {
var found bool
for _, v := range AllCodeHistoryTypes {
if c.Operation == v {
found = true
break
}
if slices.Contains(AllCodeHistoryTypes, c.Operation) {
found = true
}
if !found {
return ErrInvalid.Wrap("operation")
Expand Down Expand Up @@ -420,11 +418,8 @@ func isSubset(super, sub []string) bool {
}
var matches int
for _, o := range sub {
for _, s := range super {
if o == s {
matches++
break
}
if slices.Contains(super, o) {
matches++
}
}
return matches == len(sub)
Expand Down