From cc02cccfe240ab1688dabb367bce5670a622a298 Mon Sep 17 00:00:00 2001 From: dylanhitt Date: Mon, 1 Jul 2024 14:59:33 -0400 Subject: [PATCH 1/3] fix: do not include FullPath name in operationID revert back to the function name --- mux.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mux.go b/mux.go index 45ee45d7..89757560 100644 --- a/mux.go +++ b/mux.go @@ -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 = NameFromNamespace(route.FullName, 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 + ":" + NameFromNamespace(route.FullName) return route } @@ -206,9 +206,13 @@ 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]) +func NameFromNamespace(namespace string, opts ...func(string) string) string { + ss := strings.Split(namespace, ".") + name := ss[len(ss)-1] + for _, o := range opts { + name = o(name) + } + return name } // transform camelCase to human readable string From 3518f7a9e81060834f29248c7200e5cd8ab7eb86 Mon Sep 17 00:00:00 2001 From: dylanhitt Date: Mon, 1 Jul 2024 17:05:13 -0400 Subject: [PATCH 2/3] refactor: make NameFromNamespace a method on route --- mux.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mux.go b/mux.go index 89757560..0a32bc23 100644 --- a/mux.go +++ b/mux.go @@ -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, camelToHuman) + 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 + ":" + NameFromNamespace(route.FullName) + route.Operation.OperationID = route.Method + " " + s.basePath + route.Path + ":" + route.NameFromNamespace() return route } @@ -206,8 +206,14 @@ func FuncName(f interface{}) string { return strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "-fm") } -func NameFromNamespace(namespace string, opts ...func(string) string) string { - ss := strings.Split(namespace, ".") +// 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) From e8ede2ee86d83f35f1da0db54ce1768355b272d0 Mon Sep 17 00:00:00 2001 From: dylanhitt Date: Tue, 2 Jul 2024 10:57:39 -0400 Subject: [PATCH 3/3] chore: add tests from route.NameFromNamespace --- mux_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/mux_test.go b/mux_test.go index d2f94330..6881ec6f 100644 --- a/mux_test.go +++ b/mux_test.go @@ -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 {