Link

Advanced usage

Beyond the code … your genius!!


Skip Pointcut execution

Let’s break the flow!

Prerequisites

Let’s check that our environment is ready to follow the tutorial!

  • Install beyond tool & clone the beyond-examples repository
    >> go get github.com/wesovilabs/beyond
    >> git clone https://github.com/wesovilabs/beyond-examples.git
    >> cd before
    

Let’s do it!

> Define the advice

Open file advice/breaking.go and have a look at type BreakingAdvice.

type BreakingAdvice struct {
  prefix string
}

func (a *BreakingAdvice) Before(ctx *context.BeyondContext) {
  params := make([]string, ctx.Params().Count())
  ctx.Params().ForEach(func(index int, arg *context.Arg) {
    params[index] = fmt.Sprintf("%s:%v", arg.Name(), arg.Value())
  })
}
func (a *BreakingAdvice) Returning(ctx *context.BeyondContext) {
  fmt.Println("Returning")
}

func NewBreakingAdvice() api.Around {
  return &BreakingAdvice{}
}
  • Register the above functions

Open file cmd/main.go and have a look at function Beyond().

func Beyond() *api.Beyond {
  return api.New().
    WithAround(advice.NewBreakingAdvice, "greeting.*(...)...")
}

func main() {
  if err:=greeting.Hello("John");err!=nil{
    fmt.Println(err.Error())
  }
  if err:=greeting.Bye("John");err!=nil{
    fmt.Println(err.Error())
  }
}

> Beyond in action

>> beyond run cmd/main.go
Hey John
Returning
Bye John
Returning

That’s a normal behavior… but if we modify function Before in file advice/breaking.go

func (a *BreakingAdvice) Before(ctx *context.BeyondContext) {
  params := make([]string, ctx.Params().Count())
  ctx.Params().ForEach(func(index int, arg *context.Arg) {
    params[index] = fmt.Sprintf("%s:%v", arg.Name(), arg.Value())
  })
  if ctx.Function()=="Hello"{
    fmt.Println("Leaving the flow")
    ctx.Results().SetAt(0,errors.New("this is awesome!"))
    ctx.Exit()
  }
}
>> beyond run cmd/main.go
Leaving the flow
this is awesome!
Bye John
Returning

Wowwww… invocation to greeting.Hello was skipped!!

Challenge

By making use of the above example, could you implement an advice that skip joincut invocations when the given param in function greeting.Hello has already been passed….?

If you found any problem to resolve this challenge, don’t hesitate to drop me an email at ivan.corrales.solera@gmail.com and I will be happy to give you some help.


If you enjoyed this article, I would really appreciate if you shared it with your networks