Skip to content

Commit

Permalink
Add Modify method for freestyle fluent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Jun 30, 2024
1 parent 15c6134 commit edaa67a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
9 changes: 9 additions & 0 deletions babyapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ func (a *API[T]) WithContext(ctx context.Context) *API[T] {
return a
}

// Modify allows inline, fluent-style modification of the API, so custom modifications can be made in the same
// fluent style as the built-in methods
func (a *API[T]) Modify(modify func(*API[T])) *API[T] {
a.panicIfReadOnly()

modify(a)
return a
}

func (a *API[T]) panicIfReadOnly() {
if !a.readOnly.TryLock() {
panic(errors.New("API cannot be modified after starting"))
Expand Down
60 changes: 29 additions & 31 deletions babyapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,39 +1004,37 @@ func TestRootAPIWithMiddlewareAndCustomHandlers(t *testing.T) {
`, err.Error())
})

api := babyapi.NewRootAPI("root", "/")

api.Get = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
}
api.Delete = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(202)
}
api.Patch = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(203)
}
api.Post = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
}
api.Put = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(205)
}

api.AddCustomRoute(http.MethodGet, "/customRoute", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(206)
}))

api.AddCustomRootRoute(http.MethodGet, "/customRootRoute", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(207)
}))

middlewareHits := 0
api.AddMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
middlewareHits++
next.ServeHTTP(w, r)
api := babyapi.NewRootAPI("root", "/").
Modify(func(a *babyapi.API[*babyapi.NilResource]) {
a.Get = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
}
a.Delete = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(202)
}
a.Patch = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(203)
}
a.Post = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
}
a.Put = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(205)
}
}).
AddCustomRoute(http.MethodGet, "/customRoute", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(206)
})).
AddCustomRootRoute(http.MethodGet, "/customRootRoute", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(207)
})).
AddMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
middlewareHits++
next.ServeHTTP(w, r)
})
})
})

tests := []struct {
method string
Expand Down

0 comments on commit edaa67a

Please sign in to comment.