go-decorator

command module
v0.0.0-...-93dc1ea Latest Latest
Warning

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

Go to latest
Published: May 17, 2016 License: MIT Imports: 9 Imported by: 0

README

Go-Decorator

go-decorator creates an implementation of an interface suitable for the decorator pattern.

Motivation

Rather than adding logging to every method of an api, write one logging method and add it to every method of an interface. This could also be used for

Using go-decorator

Generate the decorator

$ go-decorator -type MyApi myapi.go > myapi_decorator.go

Go-decorator will include the necessary imports, and place the resulting structure in the same package.

If the generated import list is missing a library, use the -import flag to add it.

$ go-decorator -type MyApi -import github.com/... myapi.go

In code

    logMethod := func(name string, call func() error) {
        err := call()
        log.Infof("Called Method %s, error? %v", name, err)
        return err
    }

    api := &MyApi{}
    api = MyApiDecorator{Inner: api, Decorator: logMethod}

    // use API as before, it implements the same interface!
    ...

Contributing

Missing a feature? Open an issue or fork and send over a pull request.

License

This work is published under the MIT license.

Please see the LICENSE file for details.

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