@@ -189,43 +189,41 @@ func BuildSubcommandData(svcName string, m *service.MethodData, buildFunction *B
189
189
190
190
conversion string
191
191
)
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
+ }
200
199
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 = "\n data = 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 += "\n if err != nil {\n "
205
219
if flagType (m .Payload ) == "JSON" {
206
- target = "val"
207
- convPre = fmt .Sprintf ("var val %s\n " , m .Payload )
208
- convSuff = "\n data = 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 += "\n if 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 )
228
225
}
226
+ conversion += "\n }"
229
227
}
230
228
}
231
229
sub := & SubcommandData {
@@ -353,66 +351,64 @@ func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultVa
353
351
startIf string
354
352
endIf string
355
353
)
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 = ""
360
362
}
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 += "\n if 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 += "\n if err != nil {\n "
409
370
nilVal := "nil"
410
371
if expr .IsPrimitive (payload ) {
411
372
code += fmt .Sprintf ("var zero %s\n " , payloadRef )
412
373
nilVal = "zero"
413
374
}
414
- code += fmt .Sprintf ("if err != nil {\n \t return %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"
415
410
}
411
+ code += fmt .Sprintf ("if err != nil {\n \t return %s, err\n }" , nilVal )
416
412
}
417
413
return fmt .Sprintf ("%s%s%s" , startIf , code , endIf ), declErr
418
414
}
0 commit comments