Skip to content

Commit f23a373

Browse files
authored
[Generator] Required arguments pointers shouldn't be nil (#401) (#404)
* Generator: add not nil validation for required arguments * Skip: add empty required argument tests to skip list
1 parent 27bd1a0 commit f23a373

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/build/cmd/generate/commands/gensource/generator.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,9 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
569569
g.w(f(g.Endpoint))
570570
} else {
571571
var (
572-
pathGrow strings.Builder
573-
pathContent strings.Builder
572+
requiredArgsValidation strings.Builder
573+
pathGrow strings.Builder
574+
pathContent strings.Builder
574575
)
575576

576577
pathGrow.WriteString(` path.Grow(`)
@@ -623,15 +624,18 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
623624
pathContent.WriteString(` path.WriteString("/")` + "\n")
624625
switch a.Type {
625626
case "int":
627+
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
626628
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
627629
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
628630
case "string":
629631
pathGrow.WriteString(`len(r.` + p + `) + `)
630632
pathContent.WriteString(` path.WriteString(r.` + p + `)` + "\n")
631633
case "list":
634+
requiredArgsValidation.WriteString(`if len(r.` + p + `) == 0 { return nil, errors.New("` + a.Name + ` is required and cannot be nil or empty") }` + "\n")
632635
pathGrow.WriteString(`len(strings.Join(r.` + p + `, ",")) + `)
633636
pathContent.WriteString(` path.WriteString(strings.Join(r.` + p + `, ","))` + "\n")
634637
case "long":
638+
requiredArgsValidation.WriteString(`if r.` + p + ` == nil { return nil, errors.New("` + a.Name + ` is required and cannot be nil") }` + "\n")
635639
pathGrow.WriteString(`len(strconv.Itoa(*r.` + p + `)) + `)
636640
pathContent.WriteString(` path.WriteString(strconv.Itoa(*r.` + p + `))` + "\n")
637641
default:
@@ -710,6 +714,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
710714

711715
// Write out the content
712716
pathGrow.WriteString(`)`)
717+
g.w(requiredArgsValidation.String() + "\n")
713718
g.w(strings.Replace(pathGrow.String(), " + )", ")", 1) + "\n")
714719
g.w(pathContent.String() + "\n")
715720
}

internal/build/cmd/generate/commands/gentests/skips.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ cat.aliases/10_basic.yml:
136136
- "Column headers (pre 7.4.0)"
137137
- "Alias against closed index (pre 7.4.0)"
138138
139+
# Checks for nil required arguments makes this test incompatible with the integration tests
140+
indices.delete_alias/all_path_options.yml:
141+
- check delete with blank index and blank alias
142+
indices.put_alias/all_path_options.yml:
143+
- put alias with blank index
144+
- put alias with missing name
145+
139146
indices.put_mapping/10_basic.yml:
140147
- "Put mappings with explicit _doc type bwc"
141148

0 commit comments

Comments
 (0)