Skip to content

Commit

Permalink
Removes deprecated linter (exportloopref) and replaces with copyloopvar
Browse files Browse the repository at this point in the history
Fixes up unnecessary loop var copies across multiple files
  • Loading branch information
rspurgeon committed Oct 3, 2024
1 parent 25aeab0 commit 3f8581f
Show file tree
Hide file tree
Showing 14 changed files with 2 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
- dogsled
- durationcheck
- exhaustive
- exportloopref
- gci
- goconst
- gofmt
Expand All @@ -29,6 +28,7 @@ linters:
- unparam
- wastedassign
- errorlint
- copyloopvar
issues:
max-same-issues: 0
exclude-rules:
Expand Down
26 changes: 0 additions & 26 deletions pkg/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func (b *stateBuilder) consumerGroups() {
}

for _, cg := range b.targetContent.ConsumerGroups {
cg := cg
current, err := b.currentState.ConsumerGroups.Get(*cg.Name)
if utils.Empty(cg.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -290,7 +289,6 @@ func (b *stateBuilder) certificates() {

func (b *stateBuilder) ingestSNIs(snis []kong.SNI) error {
for _, sni := range snis {
sni := sni
currentSNI, err := b.currentState.SNIs.Get(*sni.Name)
if utils.Empty(sni.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -316,7 +314,6 @@ func (b *stateBuilder) caCertificates() {
}

for _, c := range b.targetContent.CACertificates {
c := c
cert, err := b.currentState.CACertificates.Get(*c.Cert)
if utils.Empty(c.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -412,8 +409,6 @@ func (b *stateBuilder) consumers() {
}

for _, c := range b.targetContent.Consumers {
c := c

var (
consumer *state.Consumer
err error
Expand Down Expand Up @@ -602,7 +597,6 @@ func (b *stateBuilder) ingestIntoConsumerGroup(consumer FConsumer) error {

func (b *stateBuilder) ingestKeyAuths(creds []kong.KeyAuth) error {
for _, cred := range creds {
cred := cred
existingCred, err := b.currentState.KeyAuths.Get(*cred.Key)
if utils.Empty(cred.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -626,7 +620,6 @@ func (b *stateBuilder) ingestKeyAuths(creds []kong.KeyAuth) error {

func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {
for _, cred := range creds {
cred := cred
existingCred, err := b.currentState.BasicAuths.Get(*cred.Username)
if utils.Empty(cred.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -650,7 +643,6 @@ func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {

func (b *stateBuilder) ingestHMACAuths(creds []kong.HMACAuth) error {
for _, cred := range creds {
cred := cred
existingCred, err := b.currentState.HMACAuths.Get(*cred.Username)
if utils.Empty(cred.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -674,7 +666,6 @@ func (b *stateBuilder) ingestHMACAuths(creds []kong.HMACAuth) error {

func (b *stateBuilder) ingestJWTAuths(creds []kong.JWTAuth) error {
for _, cred := range creds {
cred := cred
existingCred, err := b.currentState.JWTAuths.Get(*cred.Key)
if utils.Empty(cred.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -698,7 +689,6 @@ func (b *stateBuilder) ingestJWTAuths(creds []kong.JWTAuth) error {

func (b *stateBuilder) ingestOauth2Creds(creds []kong.Oauth2Credential) error {
for _, cred := range creds {
cred := cred
existingCred, err := b.currentState.Oauth2Creds.Get(*cred.ClientID)
if utils.Empty(cred.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -722,7 +712,6 @@ func (b *stateBuilder) ingestOauth2Creds(creds []kong.Oauth2Credential) error {

func (b *stateBuilder) ingestACLGroups(creds []kong.ACLGroup) error {
for _, cred := range creds {
cred := cred
if utils.Empty(cred.ID) {
existingCred, err := b.currentState.ACLGroups.Get(
*cred.Consumer.ID,
Expand All @@ -746,7 +735,6 @@ func (b *stateBuilder) ingestACLGroups(creds []kong.ACLGroup) error {
func (b *stateBuilder) ingestMTLSAuths(creds []kong.MTLSAuth) {
kong230Version := semver.MustParse("2.3.0")
for _, cred := range creds {
cred := cred
// normally, we'd want to look up existing resources in this case
// however, this is impossible here: mtls-auth simply has no unique fields other than ID,
// so we don't--schema validation requires the ID
Expand Down Expand Up @@ -876,7 +864,6 @@ func (b *stateBuilder) services() {
}

for _, s := range b.targetContent.Services {
s := s
err := b.ingestService(&s)
if err != nil {
b.err = err
Expand Down Expand Up @@ -950,7 +937,6 @@ func (b *stateBuilder) ingestService(s *FService) error {

// routes for the service
for _, r := range s.Routes {
r := r
r.Service = utils.GetServiceReference(s.Service)
if err := b.ingestRoute(*r); err != nil {
return err
Expand All @@ -965,7 +951,6 @@ func (b *stateBuilder) routes() {
}

for _, r := range b.targetContent.Routes {
r := r
if err := b.ingestRoute(r); err != nil {
b.err = err
return
Expand Down Expand Up @@ -1007,7 +992,6 @@ func (b *stateBuilder) vaults() {
}

for _, v := range b.targetContent.Vaults {
v := v
vault, err := b.currentState.Vaults.Get(*v.Prefix)
if utils.Empty(v.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -1034,7 +1018,6 @@ func (b *stateBuilder) licenses() {
}

for _, l := range b.targetContent.Licenses {
l := l
// Fill with a random ID if the ID is not given.
// If ID is not given in the file to sync from, a NEW license will be created.
if utils.Empty(l.ID) {
Expand All @@ -1051,7 +1034,6 @@ func (b *stateBuilder) rbacRoles() {
}

for _, r := range b.targetContent.RBACRoles {
r := r
role, err := b.currentState.RBACRoles.Get(*r.Name)
if utils.Empty(r.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -1069,7 +1051,6 @@ func (b *stateBuilder) rbacRoles() {
b.rawState.RBACRoles = append(b.rawState.RBACRoles, &r.RBACRole)
// rbac endpoint permissions for the role
for _, ep := range r.EndpointPermissions {
ep := ep
ep.Role = &kong.RBACRole{ID: kong.String(*r.ID)}
b.rawState.RBACEndpointPermissions = append(b.rawState.RBACEndpointPermissions, &ep.RBACEndpointPermission)
}
Expand Down Expand Up @@ -1146,7 +1127,6 @@ func (b *stateBuilder) upstreams() {
}

for _, u := range b.targetContent.Upstreams {
u := u
ups, err := b.currentState.Upstreams.Get(*u.Name)
if utils.Empty(u.ID) {
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -1181,8 +1161,6 @@ func (b *stateBuilder) upstreams() {

func (b *stateBuilder) ingestTargets(targets []kong.Target) error {
for _, t := range targets {
t := t

if t.Target != nil && hasIPv6Format(*t.Target) {
normalizedTarget, err := normalizeIPv6(*t.Target)
if err != nil {
Expand Down Expand Up @@ -1215,7 +1193,6 @@ func (b *stateBuilder) plugins() {

var plugins []FPlugin
for _, p := range b.targetContent.Plugins {
p := p
if p.Consumer != nil && !utils.Empty(p.Consumer.ID) {
c, err := b.intermediate.Consumers.GetByIDOrUsername(*p.Consumer.ID)
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -1287,7 +1264,6 @@ func (b *stateBuilder) filterChains() {

var filterChains []FFilterChain
for _, f := range b.targetContent.FilterChains {
f := f
if f.Service != nil && !utils.Empty(f.Service.ID) {
s, err := b.intermediate.Services.Get(*f.Service.ID)
if errors.Is(err, state.ErrNotFound) {
Expand Down Expand Up @@ -1476,7 +1452,6 @@ func (b *stateBuilder) ingestRoute(r FRoute) error {

func (b *stateBuilder) ingestPlugins(plugins []FPlugin) error {
for _, p := range plugins {
p := p
cID, rID, sID, cgID := pluginRelations(&p.Plugin)
plugin, err := b.currentState.Plugins.GetByProp(*p.Name,
sID, rID, cID, cgID)
Expand Down Expand Up @@ -1543,7 +1518,6 @@ func pluginRelations(plugin *kong.Plugin) (cID, rID, sID, cgID string) {

func (b *stateBuilder) ingestFilterChains(filterChains []FFilterChain) error {
for _, f := range filterChains {
f := f
rID, sID := filterChainRelations(&f.FilterChain)
filterChain, err := b.currentState.FilterChains.GetByProp(sID, rID)
if utils.Empty(f.ID) {
Expand Down
17 changes: 1 addition & 16 deletions pkg/file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -356,7 +355,6 @@ func fetchService(id string, kongState *state.KongState, config WriteConfig) (*F
return nil, err
}
for _, p := range plugins {
p := p
if p.Route != nil || p.Consumer != nil || p.ConsumerGroup != nil {
continue
}
Expand All @@ -370,7 +368,6 @@ func fetchService(id string, kongState *state.KongState, config WriteConfig) (*F
return compareOrder(s.Plugins[i], s.Plugins[j])
})
for _, r := range routes {
r := r
r.Service = nil
route, err := getFRouteFromRoute(r, kongState, config)
if err != nil {
Expand Down Expand Up @@ -405,7 +402,6 @@ func populateServicelessRoutes(kongState *state.KongState, file *Content,
return err
}
for _, r := range routes {
r := r
if r.Service != nil {
continue
}
Expand All @@ -429,7 +425,6 @@ func populatePlugins(kongState *state.KongState, file *Content,
return err
}
for _, p := range plugins {
p := p
associations := 0
if p.Consumer != nil {
associations++
Expand Down Expand Up @@ -551,7 +546,6 @@ func populateUpstreams(kongState *state.KongState, file *Content,
return err
}
for _, t := range targets {
t := t
t.Upstream = nil
utils.ZeroOutID(t, t.Target.Target, config.WithID)
utils.ZeroOutTimestamps(t)
Expand Down Expand Up @@ -611,7 +605,6 @@ func populateCertificates(kongState *state.KongState, file *Content,
return err
}
for _, s := range snis {
s := s
s.Certificate = nil
utils.ZeroOutID(s, s.Name, config.WithID)
utils.ZeroOutTimestamps(s)
Expand Down Expand Up @@ -668,7 +661,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, p := range plugins {
p := p
if p.Service != nil || p.Route != nil || p.ConsumerGroup != nil {
continue
}
Expand All @@ -687,7 +679,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range keyAuths {
k := k
utils.ZeroOutID(k, k.Key, config.WithID)
utils.ZeroOutTimestamps(k)
utils.MustRemoveTags(k, config.SelectTags)
Expand All @@ -699,7 +690,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range hmacAuth {
k := k
k.Consumer = nil
utils.ZeroOutID(k, k.Username, config.WithID)
utils.ZeroOutTimestamps(k)
Expand All @@ -711,7 +701,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range jwtSecrets {
k := k
k.Consumer = nil
utils.ZeroOutID(k, k.Key, config.WithID)
utils.ZeroOutTimestamps(k)
Expand All @@ -723,7 +712,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range basicAuths {
k := k
k.Consumer = nil
utils.ZeroOutID(k, k.Username, config.WithID)
utils.ZeroOutTimestamps(k)
Expand All @@ -735,7 +723,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range oauth2Creds {
k := k
k.Consumer = nil
utils.ZeroOutID(k, k.ClientID, config.WithID)
utils.ZeroOutTimestamps(k)
Expand All @@ -747,7 +734,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range aclGroups {
k := k
k.Consumer = nil
utils.ZeroOutID(k, k.Group, config.WithID)
utils.ZeroOutTimestamps(k)
Expand All @@ -759,7 +745,6 @@ func populateConsumers(kongState *state.KongState, file *Content,
return err
}
for _, k := range mtlsAuths {
k := k
utils.ZeroOutTimestamps(k)
utils.MustRemoveTags(k, config.SelectTags)
k.Consumer = nil
Expand Down Expand Up @@ -916,7 +901,7 @@ func WriteContentToFile(content *Content, filename string, format Format) error
} else {
filename = utils.AddExtToFilename(filename, strings.ToLower(string(format)))
prefix, _ := filepath.Split(filename)
if err := ioutil.WriteFile(filename, c, 0o600); err != nil {
if err := os.WriteFile(filename, c, 0o600); err != nil {
return fmt.Errorf("writing file: %w", err)
}
for _, sp := range content.ServicePackages {
Expand Down
1 change: 0 additions & 1 deletion pkg/state/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func TestDocumentCollection_Add(t *testing.T) {
}
k.Add(d1)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := k.Add(tt.args.document); (err != nil) != tt.wantErr {
Expand Down
4 changes: 0 additions & 4 deletions pkg/state/filter_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ func TestFilterChainsCollection_Add(t *testing.T) {
}

for _, tt := range commonCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand All @@ -259,7 +258,6 @@ func TestFilterChainsCollection_Add(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -371,7 +369,6 @@ func TestFilterChainsCollection_Update(t *testing.T) {
})

for _, tt := range commonCases {
tt := tt
if utils.Empty(tt.filterChain.ID) {
continue
}
Expand Down Expand Up @@ -406,7 +403,6 @@ func TestFilterChainsCollection_Update(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand Down
Loading

0 comments on commit 3f8581f

Please sign in to comment.