Link

Beyond

A Go library that will drive you to the AOP paradigm.

View it on GitHub


Getting started

Installation

Install beyond

go get -u github.com/wesovilabs/beyond

Add beyond to your project

Add beyond to go.mod.

go.mod

module github.com/wesovilabs/beyond-examples/greetings
...
require github.com/wesovilabs/beyond v1.0.0
...

Available Beyond releases can be found here

Usage

Advices registration

Let’s write a function to register the advices.

package main

func Beyond()*api.Beyond{
  ...
}
  • The function must be declared in main package.
  • The function must be named Beyond.
  • It must not receive any argument.
  • It must return a pointer of type Beyond (*github.com/wesovilabs/beyond/api.Beyond).

Have a look at the below example:

package main

import (
   "github.com/wesovilabs/beyond/api"
   "github.com/wesovilabs/beyond/api/advice"
)

func Beyond()*api.Beyond{
   return api.New().
      WithBefore(advice.NewTracingAdvice,"*.*(...)...")      		
}

There’re three types of supported advices:

  • Before: Advice that is executed before a function/method invocation.
  • Returning: Advice that is executed after a function/method invocation.
  • Around: Advice that surrounds a function/method invocation.

We can register as many advices as we need by making use of these methods:

  • withBefore:
    func (g *Beyond) WithBefore(func() Before, string) *Beyond{
    ...
    }
    

    You can learn more about writing Before advices here

  • withReturning:
    func (g *Beyond) WithReturning(func() Returning, string) *Beyond {
    ...
    }
    

    You can learn more about writing Returning advices here

  • withAround:
    func (g *Beyond) WithAround(func() Around, string) *Beyond {
    ....	
    }
    

    You can learn more about writing Around advices here

As you could realize, the above methods retrieve two params:

  • The first param must be a function that returns an object of type Before, Returning or Advice.
  • The second argument must be an expression that will be used to find the joinpoints. In other words, the expressions will be used to define which functions or methods must be intercepted by the advices.

Ignore

Alternatively, we can use method Ignore to omit some functions to be intercepted by advices.

 package main
 
 import (
    "github.com/wesovilabs/beyond/api"
    "github.com/wesovilabs/beyond/api/advice"
 )
 
 func Beyond()*api.Beyond{
    return api.New().
       WithBefore(advice.NewTracingAdvice,"*.*(...)...").
       Ignore("pkg.*(...)...")
 }

Write your own advices is very straightforward!