Skip to content

Commit

Permalink
Generator: Updates for the new REST API specification schema
Browse files Browse the repository at this point in the history
Related: elastic/elasticsearch#42346
(cherry picked from commit cbca3aa)
  • Loading branch information
karmi committed Oct 2, 2019
1 parent da9d675 commit 78e269c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 78 deletions.
40 changes: 30 additions & 10 deletions internal/cmd/generate/commands/gensource/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gensource

import (
"fmt"
"math"
"strings"
"text/tabwriter"

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

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

fmt.Fprintf(&out, "Methods:\n %s\n", e.Methods)

fmt.Fprintln(&out, "Paths:")
for _, path := range e.URL.Paths {
fmt.Fprintf(&out, " • %s\n", path)
fmt.Fprintf(w, "%6s\t%s", path.Methods[0], path.Path)
if path.Deprecated.Version != "" {
fmt.Fprintf(w, "\t*deprecated*")
}
fmt.Fprintf(w, "\n")
}
w.Flush()

longestPath := e.URL.Paths[0]
for _, v := range e.URL.Paths {
if len(v.Path) > len(longestPath.Path) {
longestPath = v
}
}

if len(e.URL.Parts) > 0 {
if len(longestPath.Parts) > 0 {
fmt.Fprintln(&out, "Parts:")
for _, part := range e.URL.Parts {
for _, part := range longestPath.Parts {
fmt.Fprintf(w, " • %s\t", part.Name)
fmt.Fprintf(w, " %s", part.Type)
if part.Required {
fmt.Fprint(w, ", required")
}
if part.Default != nil {
fmt.Fprintf(w, ", default: %s", part.Default)
}
fmt.Fprint(w, "\n")
}
w.Flush()
Expand All @@ -49,20 +63,26 @@ func (e *Endpoint) DebugInfo() string {
if param.Type == "enum" {
fmt.Fprintf(w, ": %s", strings.Join(param.Options, ", "))
}
if param.Default != nil {
fmt.Fprintf(w, ", default: %s", param.Default)
}
fmt.Fprint(w, "\n")
}
w.Flush()
}

if e.Body != nil {
fmt.Fprint(&out, "Body: ")
fmt.Fprintln(&out, "Body:")
if e.Body.Description != "" {
fmt.Fprintf(&out, " %s.", e.Body.Description)
}
if e.Body.Required {
fmt.Fprintf(&out, "required")
fmt.Fprintf(&out, " *Required*")
} else {
fmt.Fprintf(&out, "optional")
fmt.Fprintf(&out, " Optional")
}
if e.Body.ContentType != "" {
fmt.Fprintf(&out, " (format: %s)", e.Body.ContentType)
fmt.Fprintf(&out, ", format: %s", e.Body.ContentType)
}
fmt.Fprintf(&out, "\n")
}
Expand Down
62 changes: 31 additions & 31 deletions internal/cmd/generate/commands/gensource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func (g *Generator) genMethodDefinition() {
g.w("\n// ----- API Definition -------------------------------------------------------\n\n")

if g.Endpoint.Type == "xpack" {
g.w(`// ` + g.Endpoint.MethodWithNamespace() + " - " + g.Endpoint.Documentation)
g.w(`// ` + g.Endpoint.MethodWithNamespace() + " - " + g.Endpoint.Documentation.Description)
} else {
if g.Endpoint.Description != "" {
words := strings.Split(g.Endpoint.Description, " ")
if g.Endpoint.Documentation.Description != "" {
words := strings.Split(g.Endpoint.Documentation.Description, " ")
initial := strings.ToLower(words[0:1][0])
description := initial + " " + strings.Join(words[1:], " ")
lines := strings.Split(description, "\n")
Expand All @@ -127,12 +127,11 @@ func (g *Generator) genMethodDefinition() {
for _, line := range lines[1:] {
g.w("\n// " + line)
}
g.w("\n")
}
}

if g.Endpoint.Documentation != "" {
g.w("//\n" + `// See full documentation at ` + g.Endpoint.Documentation + ".")
}
if g.Endpoint.Documentation.URL != "" {
g.w("\n//\n" + `// See full documentation at ` + g.Endpoint.Documentation.URL + ".")
}

g.w(`
Expand Down Expand Up @@ -174,26 +173,26 @@ func (g *Generator) genRequestStruct() {
type ` + g.Endpoint.MethodWithNamespace() + `Request struct {`)
specialFields := []string{"index", "type", "id"}
for _, n := range specialFields {
if param, ok := g.Endpoint.URL.Parts[n]; ok {
if param, ok := g.Endpoint.URL.AllParts[n]; ok {
g.w("\n\t" + param.GoName())
g.w("\t" + param.GoType(true))
}
}

if len(g.Endpoint.URL.Parts) > 0 {
if len(g.Endpoint.URL.AllParts) > 0 {
g.w("\n")
}

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

if len(g.Endpoint.URL.Parts) > 0 || g.Endpoint.Body != nil {
if len(g.Endpoint.URL.AllParts) > 0 || g.Endpoint.Body != nil {
g.w("\n")
}

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

}

if len(g.Endpoint.URL.Parts) > 0 {
if len(g.Endpoint.URL.AllParts) > 0 {
g.w("\n")
}

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

if _, ok := g.Endpoint.URL.Parts[name]; ok {
if _, ok := g.Endpoint.URL.AllParts[name]; ok {
continue // skip params which are also parts
}

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

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

// Generate With... methods for params
for _, pName := range g.Endpoint.URL.ParamNamesSorted {
if _, ok := g.Endpoint.URL.Parts[pName]; ok {
if _, ok := g.Endpoint.URL.AllParts[pName]; ok {
continue // skip params which are also parts
}
if p, ok := g.Endpoint.URL.Params[pName]; ok {
Expand Down Expand Up @@ -452,15 +451,15 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
}`)
g.w("\n\n")
default:
g.w("\t" + `method = "` + g.Endpoint.Methods[0] + `"` + "\n\n")
g.w("\t" + `method = "` + g.Endpoint.URL.Paths[0].Methods[0] + `"` + "\n\n")
}

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

pathGrow.WriteString(` path.Grow(`)

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

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

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

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

Expand Down
Loading

0 comments on commit 78e269c

Please sign in to comment.