Skip to content

Commit

Permalink
issue emicklei#304 auto name operation to avoid validator errors
Browse files Browse the repository at this point in the history
Change-Id: Ie68bbf6a23854ab970099dd4a0f3dfc996f60cd6
  • Loading branch information
emicklei committed Feb 16, 2017
1 parent e6f937b commit d9a4a4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion route_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
11 changes: 11 additions & 0 deletions route_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d9a4a4b

Please sign in to comment.