proxyz

package module
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 0 Imported by: 0

README

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

Documentation

Overview

Package proxyz defines common interfaces and utility for generated code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MethodCall

type MethodCall interface {
	// Forward forwards the method call to the next interceptor, if any,
	// or forwards to the underlying object to call the real method.
	//
	// It should be manually called in the body of every intercepting
	// function, otherwise the method will NOT be actually called.
	Forward()

	// GetArg returns the argument of the method call at the given index.
	GetArg(argIndex int) (arg interface{})

	// SetArg sets the argument of the method call at the given index.
	SetArg(argIndex int, arg interface{})

	// GetResult returns the result of the method call at the given index.
	GetResult(resultIndex int) (result interface{})

	// SetResult sets the result of the method call at the given index.
	SetResult(resultIndex int, result interface{})

	// MethodName returns the name of the method called.
	MethodName() string

	// MethodIndex returns the index of the method called.
	MethodIndex() int

	// NumberOfArgs returns the number of the arguments of the method call.
	NumberOfArgs() int

	// NumberOfResults returns the number of the results of the method call.
	NumberOfResults() int
}

MethodCall represents a call to a method.

type MethodCallInterceptor

type MethodCallInterceptor func(methodCall MethodCall)

MethodCallInterceptor is the type of function intercepting calls to methods.

type Proxy

type Proxy interface {
	// XxxInterceptMethodCall adds an interceptor to intercept the calls
	// to the method at the given index.
	XxxInterceptMethodCall(methodIndex int, methodCallInterceptor MethodCallInterceptor)

	// XxxGetMethodName returns the name of the method at the given index.
	XxxGetMethodName(methodIndex int) string

	// XxxNumberOfMethods returns the number of the methods of the proxy
	// generated, excluding the methods whose names start with 'Xxx'.
	XxxNumberOfMethods() int

	// XxxUnderlyingType returns the representation of the underlying type of
	// the proxy generated.
	XxxUnderlyingType() string
}

Proxy represents a proxy generated.

type XxxProxyBase

type XxxProxyBase struct {
	// contains filtered or unexported fields
}

XxxProxyBase represents the base of proxies generated.

func (*XxxProxyBase) XxxGetMethodCallInterceptors

func (pb *XxxProxyBase) XxxGetMethodCallInterceptors(methodIndex int) []MethodCallInterceptor

XxxGetMethodCallInterceptors returns the interceptors applied to the calls to the method at the given index. It serves for generated code.

func (*XxxProxyBase) XxxInterceptMethodCall

func (pb *XxxProxyBase) XxxInterceptMethodCall(methodIndex int, methodCallInterceptor MethodCallInterceptor)

XxxInterceptMethodCall implements Proxy.XxxInterceptMethodCall.

Directories

Path Synopsis
cmd
examples
calc
Code generated by proxyz.
Code generated by proxyz.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL