Skip to content

Commit

Permalink
Rename type Intercepter to RouteIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
gregoryv committed Mar 23, 2024
1 parent de60e08 commit 5873b3e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions apidoc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/gregoryv/web/apidoc"
)

func ExampleNewIntercepter() {
func ExampleDoc_Document() {
mux := http.NewServeMux()
x := apidoc.NewDoc(mux)

// use the intercepter when defining routes
// use the RouteIndex when defining routes
mux.Handle("/", someHandler)
x.Document("/")

Expand All @@ -26,7 +26,7 @@ func ExampleNewIntercepter() {

func ExampleIntercepter_Defines() {
mux := http.NewServeMux()
x := apidoc.NewIntercepter()
x := apidoc.NewRouteIndex()

mux.Handle("/", someHandler)
x.Document("/")
Expand Down
7 changes: 4 additions & 3 deletions apidoc/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// NewDoc returns a documentation generator for the given router
func NewDoc(router http.Handler) *Doc {
return &Doc{
router: router,
Intercepter: NewIntercepter(),
router: router,
RouteIndex: NewRouteIndex(),
}
}

Expand All @@ -30,7 +30,8 @@ type Doc struct {

router http.Handler

*Intercepter
// RouteIndex is used to index any routes you wish to document.
*RouteIndex
}

// NewRequest returns a <pre> element of a request based on the
Expand Down
14 changes: 7 additions & 7 deletions apidoc/intercepter.go → apidoc/routeindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ import (
// NewIntercepter returns a wrapper for the underlying handler
// registering all routes being defined. It can then be used to add
// more documentation to each route.
func NewIntercepter() *Intercepter {
return &Intercepter{
func NewRouteIndex() *RouteIndex {
return &RouteIndex{
index: make(map[string]struct{}),
ErrHandler: &logIt{},
}
}

type Intercepter struct {
type RouteIndex struct {
routes []string
index map[string]struct{}

// handles errors when calling method Defines
ErrHandler
}

func (d *Intercepter) Document(pattern string) {
func (d *RouteIndex) Document(pattern string) {
key := WithMethod(pattern)
d.routes = append(d.routes, key)
d.index[key] = struct{}{}
}

// Routes returns a list of defined routes as "METHOD PATTERN"
func (d *Intercepter) Routes() []string {
func (d *RouteIndex) Routes() []string {
return d.routes
}

// Defines checks if the given pattern, [METHOD ]PATTERN, has not been
// defined. Use it when documenting your routes. The given error
// handler is used to signal error, eg. using testing.T in a test.
func (d *Intercepter) Defines(pattern string) {
func (d *RouteIndex) Defines(pattern string) {
key := WithMethod(pattern)
// if the ErrHandler is e.g. testing.T
if eh, ok := d.ErrHandler.(interface{ Helper() }); ok {
Expand All @@ -53,7 +53,7 @@ func (d *Intercepter) Defines(pattern string) {
}

// Undocumented returns empty string if all routes are documented.
func (d *Intercepter) Undocumented() string {
func (d *RouteIndex) Undocumented() string {
if len(d.index) == 0 {
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html

## [unreleased]

- Add type apidoc.Intercepter
- Use type apidoc.Doc to index routes for additional documentation

## [0.26.1] 2024-03-22

Expand Down

0 comments on commit 5873b3e

Please sign in to comment.