Skip to content

Commit 0dbdae1

Browse files
tchsskraphaelCopilot
authored
Apply modernize (#3750)
* go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Use copy() * Combine types of function parameters * Use http.NoBody * Avoid shadowing builtins * Fix file permissions * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Raphael Simon <simon.raphael@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 475a321 commit 0dbdae1

29 files changed

Lines changed: 136 additions & 232 deletions

codegen/file.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go/scanner"
1010
"go/token"
1111
"io"
12+
"maps"
1213
"os"
1314
"path/filepath"
1415
"strings"
@@ -123,9 +124,7 @@ func (f *File) Render(dir string) (string, error) {
123124
// Write writes the section to the given writer.
124125
func (s *SectionTemplate) Write(w io.Writer) error {
125126
funcs := TemplateFuncs()
126-
for k, v := range s.FuncMap {
127-
funcs[k] = v
128-
}
127+
maps.Copy(funcs, s.FuncMap)
129128
tmpl := template.Must(template.New(s.Name).Funcs(funcs).Parse(s.Source))
130129
return tmpl.Execute(w, s.Data)
131130
}
@@ -158,7 +157,7 @@ func finalizeGoSource(path string) error {
158157
}
159158
}
160159
ast.SortImports(fset, file)
161-
w, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
160+
w, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
162161
if err != nil {
163162
return err
164163
}

codegen/funcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var toLower = map[string]string{"OAuth": "oauth"}
9898
// If acronym is true and a part of the string is a common acronym
9999
// then it keeps the part capitalized (firstUpper = true)
100100
// (e.g. APIVersion) or lowercase (firstUpper = false) (e.g. apiVersion).
101-
func CamelCase(name string, firstUpper bool, acronym bool) string {
101+
func CamelCase(name string, firstUpper, acronym bool) string {
102102
if name == "" {
103103
return ""
104104
}
@@ -342,7 +342,7 @@ func runeSpacePosRev(r []rune) int {
342342
}
343343

344344
func runeSpacePos(r []rune) int {
345-
for i := 0; i < len(r); i++ {
345+
for i := range r {
346346
if unicode.IsSpace(r[i]) {
347347
return i
348348
}

codegen/service/interceptors.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func interceptorFile(svc *Data, server bool) *codegen.File {
105105
sections = append(sections, &codegen.SectionTemplate{
106106
Name: section,
107107
Source: readTemplate(template),
108-
Data: map[string]interface{}{
108+
Data: map[string]any{
109109
"MethodVarName": m.VarName,
110110
"Method": m.Name,
111111
"Service": svc.Name,
@@ -148,7 +148,7 @@ func wrapperFile(svc *Data) *codegen.File {
148148
sections = append(sections, &codegen.SectionTemplate{
149149
Name: "server-interceptor-stream-wrapper-types",
150150
Source: readTemplate("server_interceptor_stream_wrapper_types"),
151-
Data: map[string]interface{}{
151+
Data: map[string]any{
152152
"WrappedServerStreams": wrappedServerStreams,
153153
},
154154
})
@@ -160,7 +160,7 @@ func wrapperFile(svc *Data) *codegen.File {
160160
sections = append(sections, &codegen.SectionTemplate{
161161
Name: "client-interceptor-stream-wrapper-types",
162162
Source: readTemplate("client_interceptor_stream_wrapper_types"),
163-
Data: map[string]interface{}{
163+
Data: map[string]any{
164164
"WrappedClientStreams": wrappedClientStreams,
165165
},
166166
})
@@ -172,7 +172,7 @@ func wrapperFile(svc *Data) *codegen.File {
172172
sections = append(sections, &codegen.SectionTemplate{
173173
Name: "server-interceptor-wrappers",
174174
Source: readTemplate("server_interceptor_wrappers"),
175-
Data: map[string]interface{}{
175+
Data: map[string]any{
176176
"Service": svc.Name,
177177
"ServerInterceptors": svc.ServerInterceptors,
178178
},
@@ -182,7 +182,7 @@ func wrapperFile(svc *Data) *codegen.File {
182182
sections = append(sections, &codegen.SectionTemplate{
183183
Name: "client-interceptor-wrappers",
184184
Source: readTemplate("client_interceptor_wrappers"),
185-
Data: map[string]interface{}{
185+
Data: map[string]any{
186186
"Service": svc.Name,
187187
"ClientInterceptors": svc.ClientInterceptors,
188188
},
@@ -194,7 +194,7 @@ func wrapperFile(svc *Data) *codegen.File {
194194
sections = append(sections, &codegen.SectionTemplate{
195195
Name: "server-interceptor-stream-wrappers",
196196
Source: readTemplate("server_interceptor_stream_wrappers"),
197-
Data: map[string]interface{}{
197+
Data: map[string]any{
198198
"WrappedServerStreams": wrappedServerStreams,
199199
},
200200
})
@@ -203,7 +203,7 @@ func wrapperFile(svc *Data) *codegen.File {
203203
sections = append(sections, &codegen.SectionTemplate{
204204
Name: "client-interceptor-stream-wrappers",
205205
Source: readTemplate("client_interceptor_stream_wrappers"),
206-
Data: map[string]interface{}{
206+
Data: map[string]any{
207207
"WrappedClientStreams": wrappedClientStreams,
208208
},
209209
})

codegen/service/service_data.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"bytes"
55
"fmt"
6+
"slices"
67
"sort"
78
"strings"
89
"text/template"
@@ -754,11 +755,8 @@ func (d *ServicesData) analyze(service *expr.ServiceExpr) *Data {
754755
att := &expr.AttributeExpr{Type: t}
755756
if len(svcs) > 0 {
756757
// Force generate type only in the specified services
757-
for _, svc := range svcs {
758-
if svc == service.Name {
759-
types = append(types, collectTypes(att, scope, seen)...)
760-
break
761-
}
758+
if slices.Contains(svcs, service.Name) {
759+
types = append(types, collectTypes(att, scope, seen)...)
762760
}
763761
continue
764762
}

dsl/meta_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dsl_test
22

33
import (
4+
"slices"
45
"testing"
56

67
. "goa.design/goa/v3/dsl"
@@ -51,12 +52,7 @@ func TestMetaData(t *testing.T) {
5152
}
5253

5354
func hasValue(vals []string, val string) bool {
54-
for _, v := range vals {
55-
if v == val {
56-
return true
57-
}
58-
}
59-
return false
55+
return slices.Contains(vals, val)
6056
}
6157
func apiExprMeta(e eval.Expression) expr.MetaExpr { return e.(*expr.APIExpr).Meta }
6258
func userTypeMeta(e eval.Expression) expr.MetaExpr { return e.(*expr.UserTypeExpr).Meta }

dsl/result_type.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,7 @@ func CollectionOf(v any, adsl ...func()) *expr.ResultTypeExpr {
371371
// at all) then inherit the views from the collection
372372
// element.
373373
rt.Views = make([]*expr.ViewExpr, len(m.Views))
374-
for i, v := range m.Views {
375-
v := v
376-
rt.Views[i] = v
377-
}
374+
copy(rt.Views, m.Views)
378375
}
379376
})
380377
// do not execute the DSL right away, will be done last to make sure

expr/attribute.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package expr
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"goa.design/goa/v3/eval"
@@ -419,12 +420,7 @@ func (a *AttributeExpr) AllRequired() []string {
419420
// attribute, false otherwise. This method only applies to attributes of type
420421
// Object.
421422
func (a *AttributeExpr) IsRequired(attName string) bool {
422-
for _, name := range a.AllRequired() {
423-
if name == attName {
424-
return true
425-
}
426-
}
427-
return false
423+
return slices.Contains(a.AllRequired(), attName)
428424
}
429425

430426
// IsRequiredNoDefault returns true if the given string matches the name of a
@@ -716,11 +712,8 @@ func (a *AttributeExpr) validateEnumDefault(ctx string, parent eval.Expression)
716712
verr := new(eval.ValidationErrors)
717713
if a.DefaultValue != nil && a.Validation != nil && a.Validation.Values != nil {
718714
var found bool
719-
for _, e := range a.Validation.Values {
720-
if e == a.DefaultValue {
721-
found = true
722-
break
723-
}
715+
if slices.Contains(a.Validation.Values, a.DefaultValue) {
716+
found = true
724717
}
725718
if !found {
726719
verr.Add(
@@ -865,13 +858,7 @@ func (v *ValidationExpr) Merge(other *ValidationExpr) {
865858
// AddRequired merges the required fields into v.
866859
func (v *ValidationExpr) AddRequired(required ...string) {
867860
for _, r := range required {
868-
found := false
869-
for _, rr := range v.Required {
870-
if r == rr {
871-
found = true
872-
break
873-
}
874-
}
861+
found := slices.Contains(v.Required, r)
875862
if !found {
876863
v.Required = append(v.Required, r)
877864
}

0 commit comments

Comments
 (0)