Code Monkey home page Code Monkey logo

aspectgo's Introduction

AspectGo: AOP Framework for Go

Join the chat at https://gitter.im/AkihiroSuda/aspectgo Build Status Go Report Card

AspectGo is an AOP framework for Go. You can write an aspect in a simple Go-compatible grammar.

I'm hoping to propose merging AspectGo to the upstream of golang.org/x/exp if possible, but no concret plan yet.

Recipe:

  • Logging
  • Assertion
  • Fault injection
  • Mocking
  • Coverage-guided genetic fuzzing (as in AFL)
  • Fuzzed(randomized) scheduling

The interface is not fixed yet. Your suggestion and PR are welcome.

Install

$ go install github.com/AkihiroSuda/aspectgo/cmd/aspectgo

Example

$ go build github.com/AkihiroSuda/aspectgo/example/hello && ./hello
hello
$ aspectgo \
  -w /tmp/wovengopath \                         # output gopath
  -t github.com/AkihiroSuda/aspectgo/example/hello \  # target package
  example/hello/main_aspect.go                  # aspect file
$ GOPATH=/tmp/wovengopath go build github.com/AkihiroSuda/aspectgo/example/hello && ./hello
BEFORE hello
hello
AFTER hello

The aspect is located on example/hello/main_aspect.go:

package main

import (
	"fmt"
	"regexp"

	asp "github.com/AkihiroSuda/aspectgo/aspect"
)

// ExampleAspect implements interface asp.Aspect
type ExampleAspect struct {
}

// Executed on compilation-time
func (a *ExampleAspect) Pointcut() asp.Pointcut {
	pkg := regexp.QuoteMeta("github.com/AkihiroSuda/aspectgo/example/hello")
	s := pkg + ".*"
	return asp.NewCallPointcutFromRegexp(s)
}

// Executed ONLY on runtime
func (a *ExampleAspect) Advice(ctx asp.Context) []interface{} {
	args := ctx.Args()
	fmt.Println("BEFORE hello")
	res := ctx.Call(args)
	fmt.Println("AFTER hello")
	return res
}

The target is example/hello/main.go:

package main

import (
	"fmt"
)

func sayHello(s string) {
	fmt.Println("hello " + s)
}

func main() {
	sayHello("world")
}

You can also execute other examples as follows:

$ go test -v github.com/AkihiroSuda/aspectgo/example

If the output is hard to read, please add the -parallel 1 flag to go test.

Hint

  • Clean /tmp/wovengopath before running aspectgo every time.
  • Clean GOPATH before running aspectgo for faster compilation.

Current Limitation

  • Only single aspect file is supported (But you can define multiple aspects in a single file)
  • Only regexp for function name (excluding main and init) and method name can be a pointcut
  • Only "call" pointcut is supported. No support for "execution" pointcut yet:
    • Suppose that *S, *T implements I, and there is a call to I.Foo() in the target package. You can make a pointcut for I.Foo(), but you can't make a pointcut for *S nor *T.
    • Aspect cannot be woven to Go-builtin packages. i.e., You can't hook a call from a Go-builtin pacakge. (But you can hook a call to a Go-builtin package by just making a "call" pointcut for it)
  • Only "around" advice is supported. No support for "before" and "after" pointcut.
  • If an object hits multiple pointcuts, only the last one is effective.

Related Work

aspectgo's People

Contributors

akihirosuda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.