Skip to content

Commit 2cd5aaa

Browse files
authored
Address lint issues reported by DeepSource (#3647)
* Fix issues reported by DeepSource * Address lint issues reported by DeepSource * Fix additional lint issues
1 parent f7f766d commit 2cd5aaa

File tree

9 files changed

+791
-939
lines changed

9 files changed

+791
-939
lines changed

codegen/cli/cli.go

+83-87
Original file line numberDiff line numberDiff line change
@@ -189,43 +189,41 @@ func BuildSubcommandData(svcName string, m *service.MethodData, buildFunction *B
189189

190190
conversion string
191191
)
192-
{
193-
en := m.Name
194-
name = codegen.KebabCase(en)
195-
fullName = goifyTerms(svcName, en)
196-
description = m.Description
197-
if description == "" {
198-
description = fmt.Sprintf("Make request to the %q endpoint", m.Name)
199-
}
192+
en := m.Name
193+
name = codegen.KebabCase(en)
194+
fullName = goifyTerms(svcName, en)
195+
description = m.Description
196+
if description == "" {
197+
description = fmt.Sprintf("Make request to the %q endpoint", m.Name)
198+
}
200199

201-
if m.Payload != "" && buildFunction == nil && len(flags) > 0 {
202-
// No build function, just convert the arg to the body type
203-
var convPre, convSuff string
204-
target := "data"
200+
if m.Payload != "" && buildFunction == nil && len(flags) > 0 {
201+
// No build function, just convert the arg to the body type
202+
var convPre, convSuff string
203+
target := "data"
204+
if flagType(m.Payload) == "JSON" {
205+
target = "val"
206+
convPre = fmt.Sprintf("var val %s\n", m.Payload)
207+
convSuff = "\ndata = val"
208+
}
209+
conv, _, check := conversionCode(
210+
"*"+flags[0].FullName+"Flag",
211+
target,
212+
m.Payload,
213+
false,
214+
)
215+
conversion = convPre + conv + convSuff
216+
if check {
217+
conversion = "var err error\n" + conversion
218+
conversion += "\nif err != nil {\n"
205219
if flagType(m.Payload) == "JSON" {
206-
target = "val"
207-
convPre = fmt.Sprintf("var val %s\n", m.Payload)
208-
convSuff = "\ndata = val"
209-
}
210-
conv, _, check := conversionCode(
211-
"*"+flags[0].FullName+"Flag",
212-
target,
213-
m.Payload,
214-
false,
215-
)
216-
conversion = convPre + conv + convSuff
217-
if check {
218-
conversion = "var err error\n" + conversion
219-
conversion += "\nif err != nil {\n"
220-
if flagType(m.Payload) == "JSON" {
221-
conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
222-
flags[0].FullName+"Flag", flags[0].Example)
223-
} else {
224-
conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid value for %s, must be %s")`,
225-
flags[0].FullName+"Flag", flags[0].Type)
226-
}
227-
conversion += "\n}"
220+
conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
221+
flags[0].FullName+"Flag", flags[0].Example)
222+
} else {
223+
conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid value for %s, must be %s")`,
224+
flags[0].FullName+"Flag", flags[0].Type)
228225
}
226+
conversion += "\n}"
229227
}
230228
}
231229
sub := &SubcommandData{
@@ -353,66 +351,64 @@ func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultVa
353351
startIf string
354352
endIf string
355353
)
356-
{
357-
if !f.Required {
358-
startIf = fmt.Sprintf("if %s != \"\" {\n", f.FullName)
359-
endIf = "\n}"
354+
if !f.Required {
355+
startIf = fmt.Sprintf("if %s != \"\" {\n", f.FullName)
356+
endIf = "\n}"
357+
}
358+
if argTypeName == codegen.GoNativeTypeName(expr.String) {
359+
ref := "&"
360+
if f.Required || defaultValue != nil {
361+
ref = ""
360362
}
361-
if argTypeName == codegen.GoNativeTypeName(expr.String) {
362-
ref := "&"
363-
if f.Required || defaultValue != nil {
364-
ref = ""
365-
}
366-
code = argName + " = " + ref + f.FullName
367-
declErr = validate != ""
368-
} else {
369-
var checkErr bool
370-
code, declErr, checkErr = conversionCode(f.FullName, argName, argTypeName, !f.Required && defaultValue == nil)
371-
if checkErr {
372-
code += "\nif err != nil {\n"
373-
nilVal := "nil"
374-
if expr.IsPrimitive(payload) {
375-
code += fmt.Sprintf("var zero %s\n", payloadRef)
376-
nilVal = "zero"
377-
}
378-
if flagType(argTypeName) == "JSON" {
379-
code += fmt.Sprintf(`return %s, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
380-
nilVal, argName, f.Example)
381-
} else {
382-
code += fmt.Sprintf(`return %s, fmt.Errorf("invalid value for %s, must be %s")`,
383-
nilVal, argName, f.Type)
384-
}
385-
code += "\n}"
386-
}
387-
}
388-
if validate != "" {
389-
nilCheck := "if " + argName + " != nil {"
390-
if strings.HasPrefix(validate, nilCheck) {
391-
// hackety hack... the validation code is generated for the client and needs to
392-
// account for the fact that the field could be nil in this case. We are reusing
393-
// that code to validate a CLI flag which can never be nil. Lint tools complain
394-
// about that so remove the if statements. Ideally we'd have a better way to do
395-
// this but that requires a lot of changes and the added complexity might not be
396-
// worth it.
397-
var lines []string
398-
ls := strings.Split(validate, "\n")
399-
for i := 1; i < len(ls)-1; i++ {
400-
if ls[i+1] == nilCheck {
401-
i++ // skip both closing brace on previous line and check
402-
continue
403-
}
404-
lines = append(lines, ls[i])
405-
}
406-
validate = strings.Join(lines, "\n")
407-
}
408-
code += "\n" + validate + "\n"
363+
code = argName + " = " + ref + f.FullName
364+
declErr = validate != ""
365+
} else {
366+
var checkErr bool
367+
code, declErr, checkErr = conversionCode(f.FullName, argName, argTypeName, !f.Required && defaultValue == nil)
368+
if checkErr {
369+
code += "\nif err != nil {\n"
409370
nilVal := "nil"
410371
if expr.IsPrimitive(payload) {
411372
code += fmt.Sprintf("var zero %s\n", payloadRef)
412373
nilVal = "zero"
413374
}
414-
code += fmt.Sprintf("if err != nil {\n\treturn %s, err\n}", nilVal)
375+
if flagType(argTypeName) == "JSON" {
376+
code += fmt.Sprintf(`return %s, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
377+
nilVal, argName, f.Example)
378+
} else {
379+
code += fmt.Sprintf(`return %s, fmt.Errorf("invalid value for %s, must be %s")`,
380+
nilVal, argName, f.Type)
381+
}
382+
code += "\n}"
383+
}
384+
}
385+
if validate != "" {
386+
nilCheck := "if " + argName + " != nil {"
387+
if strings.HasPrefix(validate, nilCheck) {
388+
// hackety hack... the validation code is generated for the client and needs to
389+
// account for the fact that the field could be nil in this case. We are reusing
390+
// that code to validate a CLI flag which can never be nil. Lint tools complain
391+
// about that so remove the if statements. Ideally we'd have a better way to do
392+
// this but that requires a lot of changes and the added complexity might not be
393+
// worth it.
394+
var lines []string
395+
ls := strings.Split(validate, "\n")
396+
for i := 1; i < len(ls)-1; i++ {
397+
if ls[i+1] == nilCheck {
398+
i++ // skip both closing brace on previous line and check
399+
continue
400+
}
401+
lines = append(lines, ls[i])
402+
}
403+
validate = strings.Join(lines, "\n")
404+
}
405+
code += "\n" + validate + "\n"
406+
nilVal := "nil"
407+
if expr.IsPrimitive(payload) {
408+
code += fmt.Sprintf("var zero %s\n", payloadRef)
409+
nilVal = "zero"
415410
}
411+
code += fmt.Sprintf("if err != nil {\n\treturn %s, err\n}", nilVal)
416412
}
417413
return fmt.Sprintf("%s%s%s", startIf, code, endIf), declErr
418414
}

codegen/service/interceptors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// InterceptorsFiles returns the interceptors files for the given service.
11-
func InterceptorsFiles(genpkg string, service *expr.ServiceExpr) []*codegen.File {
11+
func InterceptorsFiles(_ string, service *expr.ServiceExpr) []*codegen.File {
1212
var files []*codegen.File
1313
svc := Services.Get(service.Name)
1414

codegen/service/service.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,9 @@ func Files(genpkg string, service *expr.ServiceExpr, userTypePkgs map[string][]s
122122

123123
// transform result type functions
124124
for _, t := range svc.viewedResultTypes {
125-
svcSections = append(svcSections, &codegen.SectionTemplate{
126-
Name: "viewed-result-type-to-service-result-type",
127-
Source: readTemplate("type_init"),
128-
Data: t.ResultInit,
129-
})
130-
svcSections = append(svcSections, &codegen.SectionTemplate{
131-
Name: "service-result-type-to-viewed-result-type",
132-
Source: readTemplate("type_init"),
133-
Data: t.Init,
134-
})
125+
svcSections = append(svcSections,
126+
&codegen.SectionTemplate{Name: "viewed-result-type-to-service-result-type", Source: readTemplate("type_init"), Data: t.ResultInit},
127+
&codegen.SectionTemplate{Name: "service-result-type-to-viewed-result-type", Source: readTemplate("type_init"), Data: t.Init})
135128
}
136129
var projh []*codegen.TransformFunctionData
137130
for _, t := range svc.projectedTypes {

0 commit comments

Comments
 (0)