GithubHelp home page GithubHelp logo

degorator's Introduction

Decorator in Golang

Build Status codecov Go Report Card GoDoc License

Degorator implements the decorator pattern in Golang. This can be used to add behavior, such as logs or metrics, into a function without affecting the original behavior at runtime.

##Requirements

go get github.com/starwander/degorator

Usage

  • Decorate: injects two functions(injectedBefore & injectedAfter) into the target function.
  • MakeDecorator: generate a decorator to a certain function type which can be used later.
Original Decorated
func Myfunc(in)out{ func MyfuncDecorated(in)out{
...... injectedBefore(in)
do someting out = MyFunc(in)
...... injectedAfter(out,in)
} }

Example

package main

import (
	"fmt"
	"github.com/starwander/degorator"
)

type Counter struct {
	number int
	error  int
}

func (m *Counter) add(s string) {
	if s == "nothing" {
		return
	}
	m.number++
}

func (m *Counter) addErr(err error) {
	if err != nil {
		m.error++
	}
}

func Log(s string) {
	fmt.Println("input:", s)
}

type MyFunc func(s string) error

var myFunc MyFunc = func(s string) error {
	if s == "error" {
		return fmt.Errorf("error")
	}
	return nil
}

func main() {
	counter := new(Counter)

	var CounterDecorator func(MyFunc) MyFunc
	err := degorator.MakeDecorator(&CounterDecorator, counter.add, counter.addErr)
	if err != nil {
		panic(err)
	}
	myFunc = CounterDecorator(myFunc)

	myFunc("something")
	//1 0
	fmt.Println(counter.number, counter.error)

	myFunc("error")
	//2 1
	fmt.Println(counter.number, counter.error)

	myFunc("nothing")
	//2 1
	fmt.Println(counter.number, counter.error)

	var myFuncDecorated MyFunc
	err = degorator.Decorate(&myFuncDecorated, myFunc, Log, nil)
	if err != nil {
		panic(err)
	}

	//input: another
	myFuncDecorated("another")
	//3 1
	fmt.Println(counter.number, counter.error)
}

Reference

GoDoc

LICENSE

Degorator source code is licensed under the Apache Licence, Version 2.0.

degorator's People

Contributors

starwander avatar

Stargazers

Lyubomir Raykov avatar  avatar Ali Halabyah avatar Taylor Hurt avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.