Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 21 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -281,17 +282,17 @@ func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {
}

o := &Operation{
HandlerName: slug(name),
GoName: toGoName(name, true),
Use: use,
Aliases: aliases,
Short: short,
Long: escapeString(description),
Method: method,
CanHaveBody: method == "Post" || method == "Put" || method == "Patch",
ReturnType: returnType,
Path: path,
AllParams: params,
HandlerName: Slug(name),
GoName: toGoName(name, true),
Use: use,
Aliases: aliases,
Short: short,
Long: escapeString(description),
Method: method,
CanHaveBody: method == "Post" || method == "Put" || method == "Patch",
ReturnType: returnType,
Path: path,
AllParams: params,
RequiredParams: requiredParams,
OptionalParams: optionalParams,
MediaType: reqMt,
Expand Down Expand Up @@ -325,7 +326,7 @@ func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {
}

for name, waiter := range waiters {
waiter.CLIName = slug(name)
waiter.CLIName = Slug(name)
waiter.GoName = toGoName(name+"-waiter", true)
waiter.Operation = operationMap[waiter.OperationID]
waiter.Use = usage(name, waiter.Operation.RequiredParams)
Expand Down Expand Up @@ -419,18 +420,20 @@ func escapeString(value string) string {
return transformed
}

func slug(operationID string) string {
transformed := strings.ToLower(operationID)
func Slug(operationID string) string {
re, _ := regexp.Compile("([a-z])([A-Z])")
transformed := re.ReplaceAllString(operationID, "$1-$2")
transformed = strings.ToLower(transformed)
transformed = strings.Replace(transformed, "_", "-", -1)
transformed = strings.Replace(transformed, " ", "-", -1)
return transformed
}

func usage(name string, requiredParams []*Param) string {
usage := slug(name)
usage := Slug(name)

for _, p := range requiredParams {
usage += " " + slug(p.Name)
usage += " " + Slug(p.Name)
}

return usage
Expand Down Expand Up @@ -462,7 +465,7 @@ func getParams(path *openapi3.PathItem, httpMethod string) []*Param {
}
}

cliName := slug(p.Value.Name)
cliName := Slug(p.Value.Name)
if p.Value.Extensions[ExtName] != nil {
cliName = extStr(p.Value.Extensions[ExtName])
}
Expand Down Expand Up @@ -631,7 +634,7 @@ func generate(cmd *cobra.Command, args []string) {

funcs := template.FuncMap{
"escapeStr": escapeString,
"slug": slug,
"Slug": Slug,
"title": strings.Title,
}

Expand Down
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
main "github.com/danielgtaylor/openapi-cli-generator"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -71,3 +72,27 @@ func TestEchoSuccess(t *testing.T) {

assert.JSONEq(t, "{\"hello\": \"world\", \"q\": \"foo\", \"request-id\": \"bar\"}", string(out))
}

func Test_slug(t *testing.T) {
type args struct {
operationID string
}
tests := []struct {
name string
args args
want string
}{
{name: "lowercase with spaces", args: args{operationID: "get all articles"}, want: "get-all-articles"},
{name: "Mixed sentence case", args: args{operationID: "Get all articles"}, want: "get-all-articles"},
{name: "lower snake case", args: args{operationID: "get_all_articles"}, want: "get-all-articles"},
{name: "upper snake case", args: args{operationID: "GET_ALL_ARTICLES"}, want: "get-all-articles"},
{name: "kebab case", args: args{operationID: "get-all-articles"}, want: "get-all-articles"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := main.Slug(tt.args.operationID); got != tt.want {
t.Errorf("Slug() = %v, want %v", got, tt.want)
}
})
}
}