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

fix: revert back to the simple funcName style operationID #135

Merged
merged 3 commits into from
Jul 4, 2024
Merged
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
20 changes: 15 additions & 5 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func Register[T, B any](s *Server, route Route[T, B], controller http.Handler, m
route.FullName = route.Path
}

route.Operation.Summary = NameFromNamespace(route.FullName)
route.Operation.Summary = route.NameFromNamespace(camelToHuman)
route.Operation.Description = "controller: `" + route.FullName + "`\n\n---\n\n"
route.Operation.OperationID = route.Method + " " + s.basePath + route.Path + ":" + route.FullName
route.Operation.OperationID = route.Method + " " + s.basePath + route.Path + ":" + route.NameFromNamespace()

return route
}
Expand Down Expand Up @@ -206,9 +206,19 @@ func FuncName(f interface{}) string {
return strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "-fm")
}

func NameFromNamespace(namespace string) string {
fullName := strings.Split(namespace, ".")
return camelToHuman(fullName[len(fullName)-1])
// NameFromNamespace returns the Route's FullName final string
// delimited by `.`. Essentially getting the name of the function
// and leaving the package path
//
// The output can be further modified with a list of optional
// string manipulation funcs (i.e func(string) string)
func (r Route[T, B]) NameFromNamespace(opts ...func(string) string) string {
ss := strings.Split(r.FullName, ".")
name := ss[len(ss)-1]
for _, o := range opts {
name = o(name)
}
return name
}

// transform camelCase to human readable string
Expand Down
69 changes: 69 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,75 @@ func ExampleContextNoBody_SetHeader() {
// test
}

func wrappedFunc(custom string) func(string) string {
return func(s string) string {
return s + custom
}
}

func TestNameFromNamespace(t *testing.T) {
testCases := []struct {
name string

opts []func(string) string
route Route[any, any]
expectedOutput string
}{
{
name: "base",

route: Route[any, any]{
FullName: "pkg.test.MyFunc1",
},
expectedOutput: "MyFunc1",
},
{
name: "with camelToHuman",

route: Route[any, any]{
FullName: "pkg.test.MyFunc1",
},
opts: []func(string) string{
camelToHuman,
},
expectedOutput: "my func1",
},
{
name: "with inline opt",

route: Route[any, any]{
FullName: "pkg.test.MyFunc1",
},
opts: []func(string) string{
camelToHuman,
func(s string) string {
return s + " foo"
},
},
expectedOutput: "my func1 foo",
},
{
name: "with wrapped func",

route: Route[any, any]{
FullName: "pkg.test.MyFunc1",
},
opts: []func(string) string{
wrappedFunc("Foo"),
camelToHuman,
},
expectedOutput: "my func1 foo",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := tc.route.NameFromNamespace(tc.opts...)
require.Equal(t, tc.expectedOutput, actual)
})
}
}

func BenchmarkCamelToHuman(b *testing.B) {
b.Run("camelToHuman", func(b *testing.B) {
for range b.N {
Expand Down