mux

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package mux provides a multiplexer (or router) that allows multiple github.com/anoideaopen/foundation/core/routing.Router instances to be used together. This is useful when different parts of a smart contract require distinct routing logic or when multiple routing strategies need to be combined. The `mux.Router` acts as a central hub that delegates method calls to the appropriate router based on the method name.

When initializing a chaincode, if multiple routers are provided using github.com/anoideaopen/foundation/core.WithRouters, this multiplexer will be automatically used to manage them. It ensures that each method is routed to the correct handler according to the method-to-function mappings defined by the underlying routers.

Example usage:

// Initialize individual routers
reflectRouter := reflect.NewRouter(myContract)
grpcRouter := grpc.NewRouter()

// Create a multiplexer router
muxRouter, err := mux.NewRouter(reflectRouter, grpcRouter)
if err != nil {
    log.Fatal(err)
}

// Use the multiplexer router in your chaincode initialization
cc, err := core.NewCC(myContract, core.WithRouter(muxRouter))
if err != nil {
    log.Fatal(err)
}

// Start the chaincode
if err = cc.Start(); err != nil {
    log.Fatal(err)
}

Error Handling

If a method or function is defined more than once across the provided routers, the `NewRouter` function will return an `ErrChaincodeFunction` error to avoid ambiguity in routing.

Additionally, if a method is not supported by any of the provided routers, the multiplexer will return an `ErrUnsupportedMethod` error.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChaincodeFunction is returned when a chaincode function has already been defined in the router.
	ErrChaincodeFunction = errors.New("chaincode function already defined")

	// ErrUnsupportedMethod is returned when a method is not supported by the router.
	ErrUnsupportedMethod = errors.New("unsupported method")
)

Functions

This section is empty.

Types

type Router

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

Router is a multiplexer that routes methods to the appropriate handler.

The Router struct combines multiple github.com/anoideaopen/foundation/core/routing.Router instances, allowing them to be used together. Each method call is delegated to the appropriate router based on a method-to-function mapping. This enables complex routing logic where different types of method calls are handled by different routers.

The multiplexer ensures that each method is routed to the correct handler according to the method-to-function mappings defined by the underlying routers.

func NewRouter

func NewRouter(router ...routing.Router) (*Router, error)

NewRouter creates a new Router with the provided github.com/anoideaopen/foundation/core/routing.Router instances.

It iterates over the provided routers and builds internal mappings between methods and functions. If a function is defined in more than one router, an error is returned to avoid conflicts. This function is used to initialize the multiplexer that manages multiple routers.

If multiple routers are provided during the initialization of a chaincode using github.com/anoideaopen/foundation/core.WithRouters, this function is automatically invoked to create a multiplexer.

Returns an error if any chaincode function is defined more than once.

func (*Router) ArgCount

func (r *Router) ArgCount(method string) int

ArgCount returns the number of arguments the method takes (excluding the receiver).

This method delegates the check to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name.

func (*Router) AuthRequired

func (r *Router) AuthRequired(method string) bool

AuthRequired indicates if the method requires authentication.

This method delegates the check to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name.

func (*Router) Check

func (r *Router) Check(stub shim.ChaincodeStubInterface, method string, args ...string) error

Check validates the provided arguments for the specified method.

The Check method delegates the validation of arguments to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name. If the method is not supported by any of the provided routers, it returns ErrUnsupportedMethod.

func (*Router) Function

func (r *Router) Function(method string) string

Function returns the name of the chaincode function by the specified method.

This method returns the contract function name associated with the specified method, based on the internal mappings of the multiplexer.

func (*Router) Handlers

func (r *Router) Handlers() map[string]string

Handlers returns a map of method names to chaincode functions.

This map is built based on the underlying routers and their mappings. It provides a comprehensive view of which methods are handled by which functions across all combined github.com/anoideaopen/foundation/core/routing.Router instances.

func (*Router) Invoke

func (r *Router) Invoke(stub shim.ChaincodeStubInterface, method string, args ...string) ([]byte, error)

Invoke calls the specified method with the provided arguments.

The Invoke method routes the call to the correct github.com/anoideaopen/foundation/core/routing.Router based on the method name. If the method is not supported by any of the provided routers, it returns ErrUnsupportedMethod.

func (*Router) IsInvoke

func (r *Router) IsInvoke(method string) bool

IsInvoke checks if the method is an invoke type.

This method delegates the check to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name.

func (*Router) IsQuery

func (r *Router) IsQuery(method string) bool

IsQuery checks if the method is a query type.

This method delegates the check to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name.

func (*Router) IsTransaction

func (r *Router) IsTransaction(method string) bool

IsTransaction checks if the method is a transaction type.

This method delegates the check to the appropriate github.com/anoideaopen/foundation/core/routing.Router based on the method name.

func (*Router) Method

func (r *Router) Method(function string) string

Method retrieves the method associated with the specified chaincode function.

This method returns the method name linked to the specified contract function, based on the internal mappings of the multiplexer.

Jump to

Keyboard shortcuts

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