GithubHelp home page GithubHelp logo

proxyz's Introduction

proxyz

GoDoc Build Status Coverage Status

ProxyZ is a proxy generator for Go.

It analyzes the method sets of Go types from the sources and then generates code of wrapper structures for the types, which work as proxies. You might intercept method calls via the the proxies in order to check/modify the arguments and results of the method calls, even bypass the method calls.

Features

  • Supports method call interception
  • Supports embedded structures/interfaces analysis

Installation

go get github.com/roy2220/proxyz/...
go install github.com/roy2220/proxyz/cmd/proxyz

Usage

proxyz [--format] [--write FILE] IPKG ITYPE OPKG OTYPE

Positional arguments:
  IPKG                   input package
  ITYPE                  input type
  OPKG                   output package
  OTYPE                  output type

Options:
  --format, -f           format output [default: true]
  --write FILE, -w FILE
                         write output to file inside output package directory
  --help, -h             display this help and exit

Quick Start

1. Create new file calc.go

package main

import "fmt"

type calc struct {
}

func (calc) Sum(x, y int) string {
        s := fmt.Sprintf("%d + %d = %d", x, y, x+y)
        return s
}

func main() {
        var c calc
        s := c.Sum(1, 2)
        fmt.Println(s)
        // Output: 1 + 2 = 3
}

2. Generate proxy

"$(go env GOPATH)/bin/proxyz" . calc . calcProxy -w calcproxy.go
# output written to file "calcproxy.go"

See Usage for explanation

3. Update file calc.go

package main

import (
        "fmt"
        "strings"

        "github.com/roy2220/proxyz"
)

type calc struct {
}

func (calc) Sum(x, y int) string {
        s := fmt.Sprintf("%d + %d = %d", x, y, x+y)
        return s
}

func main() {
        var c calc
        // new a proxy.
        cp := newCalcProxy(&c) // function `newCalcProxy` is generated by proxyz.

        // add a method call interceptor.
        cp.XxxInterceptMethodCall(
                // method index, indicating the method `Sum` here.
                calcProxySum, // constant `calcProxySum` is generated by proxyz.

                // method call intercepting function.
                func(mc proxyz.MethodCall) {
                        // modify the second argument `y`, whose index is 1.
                        y := mc.GetArg(1).(int)
                        y += 100
                        mc.SetArg(1, y)

                        // forward the method call to the next interceptor, if any,
                        // or forward to the underlying object `calc`.
                        // NOTE: without this call, the method `Sum` will NOT be
                        //       actually called, and no result provided.
                        mc.Forward()

                        // modify the first (only) result, whose index is 0.
                        s := mc.GetResult(0).(string)
                        s = strings.ReplaceAll(s, "=", "!=")
                        mc.SetResult(0, s)
                },
        )

        // call Sum() from the proxy.
        s := cp.Sum(1, 2)

        fmt.Println(s)
        // Output: 1 + 102 != 103
}

4. Run the code

go run calc.go calcproxy.go
# 1 + 102 != 103

See examples/calc for the complete code

proxyz's People

Contributors

roy2220 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

proxyz's Issues

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.