interceptor

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: JSON Imports: 2 Imported by: 0

Documentation

Overview

This permit to intercept data which will be given back by gautocloud and modified it before giving back to user. Interceptor should be used in a connector, to do so, connector have to implement ConnectorIntercepter:

type ConnectorIntercepter interface {
	Intercepter() interceptor.Intercepter
}

An interceptor work like a http middleware.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterfaceAsPtr

func InterfaceAsPtr(i interface{}) interface{}

func InterfaceAsPtrCopy

func InterfaceAsPtrCopy(i interface{}) interface{}

Types

type Intercepter

type Intercepter interface {
	// Current is interface given by user when using gautocloud.Inject(interfaceFromUser{}),
	// this can be nil if user doesn't use Inject functions from gautocloud.
	//
	// Found is interface found by gautocloud
	//
	// It should return an interface which must be the same type as found.
	// Tips: current and found have always the same type, this type is the type given by connector from its function Schema()
	Intercept(current, found interface{}) (interface{}, error)
}

This is the interface to implement to create an interceptor

func NewOverwrite

func NewOverwrite() Intercepter

This interceptor function let the user pre filled values inside is config structure to be used instead of use values found by gautocloud. This is useful for schema used in connector generic Config to let user write some values from config before fill the rest by gautocloud. This can only be used when user use inject functions from gautocloud.

Example
package main

import (
	"fmt"
	"github.com/cloudfoundry-community/gautocloud"
)

type MyConfig struct {
	Foo string
	Bar string
}

func main() {
	config := MyConfig{
		Foo: "my own data",
	}
	err := gautocloud.Inject(&config)
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("%#v", config))
}
Output:

interceptor_test.MyConfig{Foo:"my own data", Bar:"<injected by gautocloud>"}

func NewSchema

func NewSchema() Intercepter

Interceptor to let user implements himself write an interception on his struct/schema. This interceptor will call function Intercept if schema/struct implements SchemaIntercepter from current. It will return an error from intercept function if there is or the current interface after modification. Tips: If current not found (user doesn't use inject functions from gautocloud) this is schema found which will be used

Example
package main

import (
	"fmt"
	"github.com/cloudfoundry-community/gautocloud"
)

type MySchema struct {
	Foo string
	Bar string
}

func (s *MySchema) Intercept(found interface{}) error {
	f := found.(MySchema)
	s.Foo = "write"
	s.Bar = f.Bar
	return nil
}

func main() {
	var mySchema MySchema
	gautocloud.Inject(&mySchema)
	fmt.Println(fmt.Sprintf("%#v", mySchema))
}
Output:

interceptor_test.MySchema{Foo:"write", Bar:"<injected by gautocloud>"}

type IntercepterFunc

type IntercepterFunc func(current, found interface{}) (interface{}, error)

The IntercepterFunc type is an adapter to allow the use of ordinary functions as Intercepter. If f is a function with the appropriate signature, IntercepterFunc(f) is a Intercepter that calls f.

func (IntercepterFunc) Intercept

func (f IntercepterFunc) Intercept(current, found interface{}) (interface{}, error)

type SchemaIntercepter

type SchemaIntercepter interface {
	// Found is the interface found by gautocloud. It has the exactly same type than the struct/schema which implements
	// this interface.
	Intercept(found interface{}) error
}

User must implement this interface on his struct/schema to be able to intercept injected value from gautocloud. User could now modified his own schema, gautocloud will use user schema as injection.

Directories

Path Synopsis
cli
arg
This is an interceptor dedicated to push flags found to the final schema given by gautocloud.
This is an interceptor dedicated to push flags found to the final schema given by gautocloud.
urfave
This is an interceptor dedicated to push flags from https://github.com/urfave/cli to the final schema given by gautocloud.
This is an interceptor dedicated to push flags from https://github.com/urfave/cli to the final schema given by gautocloud.

Jump to

Keyboard shortcuts

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