MCPcopy Create free account
hub / github.com/goadesign/goa / FieldLoadCode

Function FieldLoadCode

codegen/cli/cli.go:352–419  ·  view source on GitHub ↗

FieldLoadCode returns the code used in the build payload function that initializes one of the payload object fields. It returns the initialization code and a boolean indicating whether the code requires an "err" variable.

(f *FlagData, argName, argTypeName, validate string, defaultValue any, payload expr.DataType, payloadRef string)

Source from the content-addressed store, hash-verified

350// initializes one of the payload object fields. It returns the initialization
351// code and a boolean indicating whether the code requires an "err" variable.
352func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultValue any, payload expr.DataType, payloadRef string) (string, bool) {
353 var (
354 code string
355 declErr bool
356 startIf string
357 endIf string
358 )
359 if !f.Required {
360 startIf = fmt.Sprintf("if %s != \"\" {\n", f.FullName)
361 endIf = "\n}"
362 }
363 if argTypeName == codegen.GoNativeTypeName(expr.String) {
364 ref := "&"
365 if f.Required || defaultValue != nil {
366 ref = ""
367 }
368 code = argName + " = " + ref + f.FullName
369 declErr = validate != ""
370 } else {
371 var checkErr bool
372 code, declErr, checkErr = conversionCode(f.FullName, argName, argTypeName, !f.Required && defaultValue == nil)
373 if checkErr {
374 code += "\nif err != nil {\n"
375 nilVal := "nil"
376 if expr.IsPrimitive(payload) {
377 code += fmt.Sprintf("var zero %s\n", payloadRef)
378 nilVal = "zero"
379 }
380 if flagType(argTypeName) == "JSON" {
381 code += fmt.Sprintf(`return %s, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
382 nilVal, argName, f.Example)
383 } else {
384 code += fmt.Sprintf(`return %s, fmt.Errorf("invalid value for %s, must be %s")`,
385 nilVal, argName, f.Type)
386 }
387 code += "\n}"
388 }
389 }
390 if validate != "" {
391 nilCheck := "if " + argName + " != nil {"
392 if strings.HasPrefix(validate, nilCheck) {
393 // hackety hack... the validation code is generated for the client and needs to
394 // account for the fact that the field could be nil in this case. We are reusing
395 // that code to validate a CLI flag which can never be nil. Lint tools complain
396 // about that so remove the if statements. Ideally we'd have a better way to do
397 // this but that requires a lot of changes and the added complexity might not be
398 // worth it.
399 var lines []string
400 ls := strings.Split(validate, "\n")
401 for i := 1; i < len(ls)-1; i++ {
402 if ls[i+1] == nilCheck {
403 i++ // skip both closing brace on previous line and check
404 continue
405 }
406 lines = append(lines, ls[i])
407 }
408 validate = strings.Join(lines, "\n")
409 }

Callers 2

makeFlagsFunction · 0.92
makeFlagsFunction · 0.92

Calls 4

GoNativeTypeNameFunction · 0.92
IsPrimitiveFunction · 0.92
conversionCodeFunction · 0.85
flagTypeFunction · 0.85

Tested by

no test coverage detected