discord-gophers / goapi-gen

This package contains a set of utilities for generating Go boilerplate code for services based on OpenAPI 3.0 API definitions
Apache License 2.0
137 stars 12 forks source link

Generate middlewares as fields #98

Closed Karitham closed 1 year ago

Karitham commented 2 years ago

This PR cleans up the ugly middleware map in favor of a clean, generated struct.

The goal is to profit from the excellent autocompletion provided by IDEs to work faster.

As an added bonus, if you had previously used a middleware and forgot it in the spec, the code wont compile, as opposed to silently running, ensuring not ever forgetting a middleware both in the code and in the spec (If you've never typed it, I can't do anything for you)

Karitham commented 2 years ago

Before:

    mw := map[string]func(http.Handler) http.Handler{
        "pathMiddleware": func(h http.Handler) http.Handler {
            called = true
            return h
        },
        "operationMiddleware": func(h http.Handler) http.Handler { return h },
    }

After:

    mw := Middlewares{
        Path: func(h http.Handler) http.Handler {
            called = true
            return h
        },
        Operation: func(h http.Handler) http.Handler { return h },
    }