Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Middleware function to be less brittle #142

Open
renaz6 opened this issue Jun 13, 2023 · 0 comments
Open

Update Middleware function to be less brittle #142

renaz6 opened this issue Jun 13, 2023 · 0 comments
Assignees

Comments

@renaz6
Copy link
Member

renaz6 commented Jun 13, 2023

Update Middleware function to be less brittle, using the builder or functional options pattern, as described by John in #141:

This is the kind of function that will probably grow over time, with new parameters. This makes the API brittle, since you wind up breaking existing clients when you change the function's signature.

If we add more parameters here, it would probably be better to move to either the builder pattern or the functional options pattern described by https://golang.cafe/blog/golang-functional-options-pattern.html.

An really trivial example:

type TraceOption func(*traceHandlerBuilder)

type traceHandlerBuilder {
    // whatever fields are necessary to build the handler
}

func (builder traceHandlerBuilder) build() func(http.Handler) http.Handler { ... }

func EchoFirstTraceNodeInfo(opts ...TraceOption) func(http.Handler) http.Handler {
   var builder traceHandlerBuilder
   for _, o := range opts {
        o(&builder)
   }

   return o.build()
}

Alternatively, you can expose the builder as part of your API. gorilla/mux takes this approach. The choice of which is really more about what will be more stable for a client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant