middlewarer

command module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 10 Imported by: 0

README

middlewarer

Generate a middleware framework for any Go interface

Installation

Requirements:

  • Go 1.20 or newer
go install github.com/DominicWuest/middlewarer@latest

Example

example.go:

//go:generate middlewarer -type=Foo
type Foo interface {
    Bar(Baz) Quz
}

foo_middleware.go

// WrapFoo wraps the methods of the provided interface
// in the provided middleware
func WrapFoo(in Foo, m FooMiddleware) Foo {
    m.wrapped = in
    return &m
}

// FooMiddleware implements Foo
type FooMiddleware struct {
    wrapped Foo

    BarMiddleware func(BarHandler) BarHandler
}

type BarHandler func(Baz) Quz

func (m *FooMiddleware) Bar(a0 Baz) Quz {
    fun := m.wrapped.Bar
    if m.BarMiddleware != nil {
        fun = m.BarMiddleware(fun)
    }
    return fun(a0)
}

Adding Middleware

This example shows how to add a logger to Foo, declared in the example section.

logger.go

func AddLoggerToFoo(f Foo) Foo {
    return WrapFoo(f, FooMiddleware{
        BarMiddleware: barLogger
    })
}

func barLogger(next BarHandler) BarHandler {
    return func(b Baz) Quz {
        log.Println("Calling Bar!")
        res := next()
        log.Println("Bar returned: ", res)
        return res
    }
}

Optional Implementation

When wrapping an instance of an interface <I> by calling Wrap<I>, the provided struct <I>Middleware is allowed to have fields evaluating to nil.

This allows adding middleware to select methods.

Take for example

//go:generate middlewarer -type=Server
type Server interface {
    Init()
    Request() Response
}

If you now want to wrap the Request method with middleware, but not the Init method, you can simply do:

// Get some server instance you want to wrap
s := getServer()

// Overwrite s with the wrapped instance
s = WrapServer(s, ServerMiddleware{
    // InitMiddleware: nil
    RequestMiddleware: someMiddlewareFunc
})

When calling Init on s, no middleware gets invoked. However, when calling Request, the passed middleware function gets called.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL