Skip to content

Commit cbca3aa

Browse files
committed
Generator: Updates for the new REST API specification schema
Related: elastic/elasticsearch#42346
1 parent 5963931 commit cbca3aa

File tree

5 files changed

+159
-78
lines changed

5 files changed

+159
-78
lines changed

internal/cmd/generate/commands/gensource/debug.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gensource
22

33
import (
44
"fmt"
5+
"math"
56
"strings"
67
"text/tabwriter"
78

@@ -16,24 +17,37 @@ func (e *Endpoint) DebugInfo() string {
1617

1718
fmt.Fprintln(&out, strings.Repeat("─", utils.TerminalWidth()))
1819
fmt.Fprintf(&out, "API: %s (%s:%s)\n", e.MethodWithNamespace(), e.Type, e.Name)
19-
// fmt.Fprintf(&out, "<%s>\n", e.Documentation)
20+
fmt.Fprintf(&out, "%s\n", e.Documentation.Description[0:int(math.Min(float64(80), float64(len(e.Documentation.Description))))])
2021
fmt.Fprintln(&out, strings.Repeat("─", utils.TerminalWidth()))
2122

22-
fmt.Fprintf(&out, "Methods:\n %s\n", e.Methods)
23-
2423
fmt.Fprintln(&out, "Paths:")
2524
for _, path := range e.URL.Paths {
26-
fmt.Fprintf(&out, " • %s\n", path)
25+
fmt.Fprintf(w, "%6s\t%s", path.Methods[0], path.Path)
26+
if path.Deprecated.Version != "" {
27+
fmt.Fprintf(w, "\t*deprecated*")
28+
}
29+
fmt.Fprintf(w, "\n")
30+
}
31+
w.Flush()
32+
33+
longestPath := e.URL.Paths[0]
34+
for _, v := range e.URL.Paths {
35+
if len(v.Path) > len(longestPath.Path) {
36+
longestPath = v
37+
}
2738
}
2839

29-
if len(e.URL.Parts) > 0 {
40+
if len(longestPath.Parts) > 0 {
3041
fmt.Fprintln(&out, "Parts:")
31-
for _, part := range e.URL.Parts {
42+
for _, part := range longestPath.Parts {
3243
fmt.Fprintf(w, " • %s\t", part.Name)
3344
fmt.Fprintf(w, " %s", part.Type)
3445
if part.Required {
3546
fmt.Fprint(w, ", required")
3647
}
48+
if part.Default != nil {
49+
fmt.Fprintf(w, ", default: %s", part.Default)
50+
}
3751
fmt.Fprint(w, "\n")
3852
}
3953
w.Flush()
@@ -49,20 +63,26 @@ func (e *Endpoint) DebugInfo() string {
4963
if param.Type == "enum" {
5064
fmt.Fprintf(w, ": %s", strings.Join(param.Options, ", "))
5165
}
66+
if param.Default != nil {
67+
fmt.Fprintf(w, ", default: %s", param.Default)
68+
}
5269
fmt.Fprint(w, "\n")
5370
}
5471
w.Flush()
5572
}
5673

5774
if e.Body != nil {
58-
fmt.Fprint(&out, "Body: ")
75+
fmt.Fprintln(&out, "Body:")
76+
if e.Body.Description != "" {
77+
fmt.Fprintf(&out, " %s.", e.Body.Description)
78+
}
5979
if e.Body.Required {
60-
fmt.Fprintf(&out, "required")
80+
fmt.Fprintf(&out, " *Required*")
6181
} else {
62-
fmt.Fprintf(&out, "optional")
82+
fmt.Fprintf(&out, " Optional")
6383
}
6484
if e.Body.ContentType != "" {
65-
fmt.Fprintf(&out, " (format: %s)", e.Body.ContentType)
85+
fmt.Fprintf(&out, ", format: %s", e.Body.ContentType)
6686
}
6787
fmt.Fprintf(&out, "\n")
6888
}

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func (g *Generator) genMethodDefinition() {
115115
g.w("\n// ----- API Definition -------------------------------------------------------\n\n")
116116

117117
if g.Endpoint.Type == "xpack" {
118-
g.w(`// ` + g.Endpoint.MethodWithNamespace() + " - " + g.Endpoint.Documentation)
118+
g.w(`// ` + g.Endpoint.MethodWithNamespace() + " - " + g.Endpoint.Documentation.Description)
119119
} else {
120-
if g.Endpoint.Description != "" {
121-
words := strings.Split(g.Endpoint.Description, " ")
120+
if g.Endpoint.Documentation.Description != "" {
121+
words := strings.Split(g.Endpoint.Documentation.Description, " ")
122122
initial := strings.ToLower(words[0:1][0])
123123
description := initial + " " + strings.Join(words[1:], " ")
124124
lines := strings.Split(description, "\n")
@@ -127,12 +127,11 @@ func (g *Generator) genMethodDefinition() {
127127
for _, line := range lines[1:] {
128128
g.w("\n// " + line)
129129
}
130-
g.w("\n")
131130
}
131+
}
132132

133-
if g.Endpoint.Documentation != "" {
134-
g.w("//\n" + `// See full documentation at ` + g.Endpoint.Documentation + ".")
135-
}
133+
if g.Endpoint.Documentation.URL != "" {
134+
g.w("\n//\n" + `// See full documentation at ` + g.Endpoint.Documentation.URL + ".")
136135
}
137136

138137
g.w(`
@@ -174,26 +173,26 @@ func (g *Generator) genRequestStruct() {
174173
type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
175174
specialFields := []string{"index", "type", "id"}
176175
for _, n := range specialFields {
177-
if param, ok := g.Endpoint.URL.Parts[n]; ok {
176+
if param, ok := g.Endpoint.URL.AllParts[n]; ok {
178177
g.w("\n\t" + param.GoName())
179178
g.w("\t" + param.GoType(true))
180179
}
181180
}
182181

183-
if len(g.Endpoint.URL.Parts) > 0 {
182+
if len(g.Endpoint.URL.AllParts) > 0 {
184183
g.w("\n")
185184
}
186185

187186
if g.Endpoint.Body != nil {
188187
g.w("\n\tBody io.Reader")
189188
}
190189

191-
if len(g.Endpoint.URL.Parts) > 0 || g.Endpoint.Body != nil {
190+
if len(g.Endpoint.URL.AllParts) > 0 || g.Endpoint.Body != nil {
192191
g.w("\n")
193192
}
194193

195194
for _, name := range g.Endpoint.URL.PartNamesSorted {
196-
p, ok := g.Endpoint.URL.Parts[name]
195+
p, ok := g.Endpoint.URL.AllParts[name]
197196
if !ok {
198197
panic(fmt.Sprintf("Part %q not found", name))
199198
}
@@ -212,7 +211,7 @@ type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
212211

213212
}
214213

215-
if len(g.Endpoint.URL.Parts) > 0 {
214+
if len(g.Endpoint.URL.AllParts) > 0 {
216215
g.w("\n")
217216
}
218217

@@ -222,7 +221,7 @@ type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
222221
panic(fmt.Sprintf("Parameter %q not found", name))
223222
}
224223

225-
if _, ok := g.Endpoint.URL.Parts[name]; ok {
224+
if _, ok := g.Endpoint.URL.AllParts[name]; ok {
226225
continue // skip params which are also parts
227226
}
228227

@@ -348,7 +347,7 @@ func (f ` + g.Endpoint.MethodWithNamespace() + `) WithBody(v io.Reader) func(*`
348347

349348
// Generate With... methods for parts
350349
for _, pName := range g.Endpoint.URL.PartNamesSorted {
351-
if p, ok := g.Endpoint.URL.Parts[pName]; ok {
350+
if p, ok := g.Endpoint.URL.AllParts[pName]; ok {
352351
if skipRequiredArgs[p.Name] && p.Name != "type" {
353352
continue
354353
}
@@ -361,7 +360,7 @@ func (f ` + g.Endpoint.MethodWithNamespace() + `) WithBody(v io.Reader) func(*`
361360

362361
// Generate With... methods for params
363362
for _, pName := range g.Endpoint.URL.ParamNamesSorted {
364-
if _, ok := g.Endpoint.URL.Parts[pName]; ok {
363+
if _, ok := g.Endpoint.URL.AllParts[pName]; ok {
365364
continue // skip params which are also parts
366365
}
367366
if p, ok := g.Endpoint.URL.Params[pName]; ok {
@@ -452,15 +451,15 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
452451
}`)
453452
g.w("\n\n")
454453
default:
455-
g.w("\t" + `method = "` + g.Endpoint.Methods[0] + `"` + "\n\n")
454+
g.w("\t" + `method = "` + g.Endpoint.URL.Paths[0].Methods[0] + `"` + "\n\n")
456455
}
457456

458457
// Get default part values for specific APIs
459458
// TODO: Move to overrides
460459
var defparts bool
461460
switch g.Endpoint.Name {
462461
case "index", "create", "delete", "explain", "exists", "get", "get_source", "update", "termvectors":
463-
for _, p := range g.Endpoint.URL.Parts {
462+
for _, p := range g.Endpoint.URL.AllParts {
464463
if p.Default != nil {
465464
var fieldName string
466465
var fieldValue string
@@ -504,25 +503,25 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
504503

505504
pathGrow.WriteString(` path.Grow(`)
506505

507-
if len(g.Endpoint.URL.Parts) < 1 {
508-
if g.Endpoint.URL.Path == "" {
509-
panic(fmt.Sprintf("FAIL: %q: empty endpoint\n", g.Endpoint.Name))
506+
// FIXME: Select longest path based on number of template entries, not string length
507+
longestPath := g.Endpoint.URL.Paths[0]
508+
for _, v := range g.Endpoint.URL.Paths {
509+
if len(v.Path) > len(longestPath.Path) {
510+
longestPath = v
510511
}
511-
pathGrow.WriteString(`len("` + g.Endpoint.URL.Path + `")`)
512-
pathContent.WriteString(` path.WriteString("` + g.Endpoint.URL.Path + `")` + "\n")
512+
}
513513

514-
} else {
515-
// FIXME: Select longest path based on number of template entries, not string length
516-
longestPath := g.Endpoint.URL.Paths[0]
517-
for _, v := range g.Endpoint.URL.Paths {
518-
if len(v) > len(longestPath) {
519-
longestPath = v
520-
}
514+
if len(longestPath.Parts) < 1 {
515+
if len(g.Endpoint.URL.Paths) < 1 {
516+
panic(fmt.Sprintf("FAIL: %q: empty endpoint\n", g.Endpoint.Name))
521517
}
518+
pathGrow.WriteString(`len("` + longestPath.Path + `")`)
519+
pathContent.WriteString(` path.WriteString("` + longestPath.Path + `")` + "\n")
522520

521+
} else {
523522
pathParts := make([]string, 0)
524523
apiArgs := g.Endpoint.RequiredArguments()
525-
for _, v := range strings.Split(longestPath, "/") {
524+
for _, v := range strings.Split(longestPath.Path, "/") {
526525
if v != "" {
527526
pathParts = append(pathParts, v)
528527
}
@@ -555,7 +554,8 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
555554

556555
// Optional arguments
557556
if p == "" {
558-
for _, a := range g.Endpoint.URL.Parts {
557+
for _, a := range longestPath.Parts {
558+
// fmt.Printf("a: %+v\n", a)
559559
if strings.HasPrefix(v, "{") && a.Name == r.Replace(v) {
560560
p = a.GoName()
561561

0 commit comments

Comments
 (0)