Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Generator] Required arguments pointers shouldn't be nil #401

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Generator: add not nil validation for required arguments
  • Loading branch information
Anaethelion committed Jan 19, 2022
commit 47179c8dddf133403f64daeacb11eac4f8b0d26c
9 changes: 7 additions & 2 deletions internal/build/cmd/generate/commands/gensource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,9 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
g.w(f(g.Endpoint))
} else {
var (
pathGrow strings.Builder
pathContent strings.Builder
requiredArgsValidation strings.Builder
pathGrow strings.Builder
pathContent strings.Builder
)

pathGrow.WriteString(` path.Grow(`)
Expand Down Expand Up @@ -623,15 +624,18 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
pathContent.WriteString(` path.WriteString("/")` + "\n")
switch a.Type {
case "int":
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
case "string":
pathGrow.WriteString(`len(r.` + p + `) + `)
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
case "list":
requiredArgsValidation.WriteString(`if len(r.` + p + `) == 0 { return nil, errors.New("` + a.Name + ` is required and cannot be nil or empty") }` + "\n")
pathGrow.WriteString(`len(strings.Join(r.` + p + `, ",")) + `)
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
case "long":
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
default:
Expand Down Expand Up @@ -710,6 +714,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,

// Write out the content
pathGrow.WriteString(`)`)
g.w(requiredArgsValidation.String() + "\n")
g.w(strings.Replace(pathGrow.String(), " + )", ")", 1) + "\n")
g.w(pathContent.String() + "\n")
}
Expand Down