diff --git a/route_builder.go b/route_builder.go index 591d28c4..2cf7bd5a 100644 --- a/route_builder.go +++ b/route_builder.go @@ -5,10 +5,12 @@ package restful // that can be found in the LICENSE file. import ( + "fmt" "os" "reflect" "runtime" "strings" + "sync/atomic" "github.com/emicklei/go-restful/log" ) @@ -242,7 +244,7 @@ func (b *RouteBuilder) Build() Route { ResponseErrors: b.errorMap, ReadSample: b.readSample, WriteSample: b.writeSample, - Metadata: b.metadata} + Metadata: b.metadata} route.postBuild() return route } @@ -251,6 +253,8 @@ func concatPath(path1, path2 string) string { return strings.TrimRight(path1, "/") + "/" + strings.TrimLeft(path2, "/") } +var anonymousFuncCount int32 + // nameOfFunction returns the short name of the function f for documentation. // It uses a runtime feature for debugging ; its value may change for later Go versions. func nameOfFunction(f interface{}) string { @@ -261,5 +265,10 @@ func nameOfFunction(f interface{}) string { last = strings.TrimSuffix(last, ")-fm") // Go 1.5 last = strings.TrimSuffix(last, "·fm") // < Go 1.5 last = strings.TrimSuffix(last, "-fm") // Go 1.5 + if last == "func1" { // this could mean conflicts in API docs + val := atomic.AddInt32(&anonymousFuncCount, 1) + last = "func" + fmt.Sprintf("%d", val) + atomic.StoreInt32(&anonymousFuncCount, val) + } return last } diff --git a/route_builder_test.go b/route_builder_test.go index 5da510dd..3154ac22 100644 --- a/route_builder_test.go +++ b/route_builder_test.go @@ -59,3 +59,14 @@ func TestRouteBuilder(t *testing.T) { t.Errorf("Metadata not set") } } + +func TestAnonymousFuncNaming(t *testing.T) { + f1 := func() {} + f2 := func() {} + if got, want := nameOfFunction(f1), "func1"; got != want { + t.Errorf("got %v want %v", got, want) + } + if got, want := nameOfFunction(f2), "func2"; got != want { + t.Errorf("got %v want %v", got, want) + } +}