A method dispatcher written in go powered by reflection.

go-dispatcher

A single-file dispatcher written in Golang.

Description

Inspired by the JSON-RPC module of polygon-edge and geth, this package provides a way to register methods from a struct pointer.

It can be used to bootstrap a server or create a worker pool in your project !

Installation

Run the following command :

go get github.com/PtitLuca/[email protected]

Quick Start

A very simple example
package main

import (
	"fmt"
	"github.com/PtitLuca/go-dispatcher/dispatcher"
	"log"
)

type T struct {
}

func (t *T) Example(a, b int) int {
	return a + b
}

func main() {
	d := dispatcher.New()
	err := d.Register("Test", &T{})
	if err != nil {
		log.Fatalln(err)
	}

	output, err := d.Run("Test", "Example", 1, 2)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(output[0].Int())
}

After running, you should get this in your terminal :

3
Multi-service registration
package main

import (
	"fmt"
	"github.com/PtitLuca/go-dispatcher/dispatcher"
	"log"
)

type X struct {
}

func (x *X) Example2(a, b string) string {
	return a + b
}

type T struct {
}

func (t *T) Example(a, b int) int {
	return a + b
}

func main() {
	d := dispatcher.New()
	err := d.Register("Test", &T{})
	if err != nil {
		log.Fatalln(err)
	}

	err = d.Register("TestX", &X{})
	if err != nil {
		log.Fatalln(err)
	}

	output, err := d.Run("Test", "Example", 1, 2)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(output[0].Int())

	output, err = d.Run("TestX", "Example2", "Hello", "World")
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(output[0].String())
}

After running, you should get this in your terminal :

3
HelloWorld
Variadic arguments
package main

import (
	"fmt"
	"github.com/PtitLuca/go-dispatcher/dispatcher"
	"log"
)

type T struct {
}

func (t *T) ExampleVariadic(a int, b ...string) int {
	return a + len(b)
}

func main() {
	d := dispatcher.New()
	err := d.Register("Test", &T{})
	if err != nil {
		log.Fatalln(err)
	}

	output, err := d.Run("Test", "ExampleVariadic", 1, "These", "Are", "Variadic", "Arguments")
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(output[0].Int())
}

After running, you should get this in your terminal :

5

Features

This package has support for :

  • Multi-service registration
  • Methods registration - exported only
  • Variadic arguments

Authors

Owner
President at @PoCInnovation.
null
Comments
  • Add Go releaser workflow to simplify release process

    Add Go releaser workflow to simplify release process

    Context

    Currently, release are made manually using git tag command.

    I think implementing a more elaborate way can be useful for future release.

    Proposal

    Go Release is a well known tool that work out of the box. Since it has an easy GitHub Action integration, it doesn't cost anything to add it.

    What's your opinion @PtitLuca?

  • Add tests on dispatcher.Run with array arguments

    Add tests on dispatcher.Run with array arguments

    Description

    There was no test on register and run a method that takes array as arguments. To ensure that it's correctly supported by the dispatcher, I added 2 tests.

    Signed-off-by: Vasek - Tom C [email protected]

    Changes include

    • [x] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [ ] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [ ] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [ ] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [ ] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Implement getter to allow user add logic on his side

    Implement getter to allow user add logic on his side

    Description

    Add GetService and GetMethod functions to Dispatcher

    Those methods do not require any additional test since it's already used by the dispatcher itself.

    Resolves #9

    Signed-off-by: Vasek - Tom C [email protected]

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [x] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [ ] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [ ] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [x] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Make FuncMetadata and ServiceData function fields public

    Make FuncMetadata and ServiceData function fields public

    Description

    Make fields public to allow user integrate it in his own logic

    Changes include

    • [x] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [ ] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [x] I have assigned this PR to myself
    • [x] I have added at least 1 reviewer
    • [x] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [ ] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Add go releaser configuration and workflow

    Add go releaser configuration and workflow

    Description

    • Add go releaser configuration
    • Add go releaser GitHub action

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [x] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [x] I have assigned this PR to myself
    • [x] I have added at least 1 reviewer
    • [x] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [ ] I have added sufficient documentation in code

    Testing

    • [ ] I have tested this code with the official test suite
    • [x] I have tested this code manually

    Manual tests

    Tested on https://github.com/TomChv/test-go-releaser.

  • List registered structures and methods

    List registered structures and methods

    Feature requests

    Adding a method List to the dispatcher to list every registered structure and methods in it would be really useful for debug purpose and dynamic documentation.

  • Add dispatcher tests for service fields

    Add dispatcher tests for service fields

    Signed-off-by: Luca Georges Francois [email protected]

    Description

    This PR adds tests for updating service fields.

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [
    • [X] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [X] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [X] I have added the relevant labels
    • [X] I have updated the official documentation
    • [X] I have added sufficient documentation in code

    Testing

    • [X] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Add examples

    Add examples

    Description

    This PR adds usage examples in the README.

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [X] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [X] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [X] I have added the relevant labels
    • [X] I have updated the official documentation
    • [X] I have added sufficient documentation in code
  • Update README

    Update README

    Signed-off-by: Luca Georges Francois [email protected]

    Description

    This PR updates the README.

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [X] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [X] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [X] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [X] I have added sufficient documentation in code
  • Add variadic support

    Add variadic support

    Signed-off-by: Luca Georges Francois [email protected]

    Description

    This PR adds the support for variadic method execution. You can now register variadic methods in the dispatcher.

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [X] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [X] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [X] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [X] I have added sufficient documentation in code

    Testing

    • [X] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Add tests job in CI

    Add tests job in CI

    Signed-off-by: Luca Georges Francois [email protected]

    Description

    This PR adds a job in the CI to run the tests.

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [X] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [X] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [X] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [X] I have added sufficient documentation in code
  • Add validator method

    Add validator method

    Description

    Add validate method to verify and convert arguments to expected one

    • Add methods
    • Add tests
    • Add docs in code

    Resolves #10

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [x] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [x] I have assigned this PR to myself
    • [x] I have added at least 1 reviewer
    • [x] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [x] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Refactor dispatcher package file architecture to improve maintainability

    Refactor dispatcher package file architecture to improve maintainability

    Description

    dispatcher.go started to become huge with recent improvement. To keep that project simple, this commit split codebase into multiple files following the single responsibility principle.

    Signed-off-by: Vasek - Tom C [email protected]

    Changes include

    • [x] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [ ] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [x] I have assigned this PR to myself
    • [x] I have added at least 1 reviewer
    • [x] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [ ] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
    • [ ] I have tested this code manually
  • Make dispatcher thread safe

    Make dispatcher thread safe

    Description

    Dispatcher is now I/O thread safe.

    There is a mutex in Register method to ensure that insert a new method doesn't create a race condition. Another mutex is along each stored methods, they can then be called in concurrency without problem. The code come with a test to ensure that it works

    Signed-off-by: Vasek - Tom C [email protected]

    Changes include

    • [ ] Bugfix (non-breaking change that solves an issue)
    • [ ] Hotfix (change that solves an urgent issue, and requires immediate attention)
    • [x] New feature (non-breaking change that adds functionality)
    • [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality)

    Checklist

    • [ ] I have assigned this PR to myself
    • [ ] I have added at least 1 reviewer
    • [ ] I have added the relevant labels
    • [ ] I have updated the official documentation
    • [x] I have added sufficient documentation in code

    Testing

    • [x] I have tested this code with the official test suite
An effective time-series data compression/decompression method based on Facebook's Gorilla.

Gorilla This package provides the effective time-series data compression method based on Facebook's Gorilla.. In a nutshell, it uses delta-of-delta ti

Sep 26, 2022
Go implementation Welford’s method for one-pass variance computation

Variance and standard deviation caluculation using variance's algorithm Table of Contents Introduction Installation Usage Contributing License Introdu

Jun 5, 2022
Converts NFAs (and DFAs) to a regular expressions using the state removal method

nfa2regex: convert NFAs (and DFAs) to regular expressions An implementation of the state removal technique for converting an NFA to a regular expressi

Apr 29, 2022
This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes

This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes. It is a vanity attempt to break RSA Encryption which relies on prime multiples for encryption.

Jun 3, 2022
A simple OneText Api powered by Golang

OneTextAPI-Go A simple OneText Api powered by Golang. 使用方法 在 Vercel 调用 example: package api import ( onetext "github.com/XiaoMengXinX/OneTextAPI-

May 3, 2022
Random fake data generator written in go
Random fake data generator written in go

Gofakeit Random data generator written in go Features 160+ Functions!!! Concurrent Global Rand Struct Generator Custom Functions Http Server Command L

Jan 1, 2023
Genetic Algorithm written in go

This genetic algorithm is designed to minimise the problem specific code from a genetic algorithm. The three interfaces Gene, Initialiser, an Evaluato

Feb 15, 2022
Simple and expressive toolbox written in Go

ugo Simple and expressive toolbox written with love and care in Go. Deeply inspired by underscore.js and has the same syntax and behaviour Fully cover

Sep 27, 2022
A simplistic todo list manager written in Go
A simplistic todo list manager written in Go

Tasks Tasks is a simplistic Go webapp to manage tasks, I built this tool to manage tasks which I wanted to do, there are many good kanban style boards

Dec 10, 2022
A session manager for tmux written in Go
A session manager for tmux written in Go

Smug - tmux session manager Inspired by tmuxinator and tmuxp. Smug automates your tmux workflow. You can create a single configuration file, and Smug

Jan 6, 2023
An example client implementation written in GO to access the CyberVox platform API

About This is an example client implementation written in GO to access the CyberVox platform API.

Nov 7, 2022
💥 Fusion is a tiny stream processing library written in Go.

?? Fusion Fusion is a tiny stream processing library written in Go. See reactor for a stream processing tool built using fusion. Features Simple & lig

Jun 30, 2021
Airplay 2 Receiver written in go

Go Play 2 This is a work in progress Airplay 2 Speaker implementation largely inspired by airplay2-receiver Status Can be paired manually on IOS 14.x

Dec 25, 2022
Simple 'UserKit' for Malware written in Go. Startup, Hidden Files, Critical Process and Registry Watcher

GoUserKit Simple UserKit for Malware written in Go Features Makes Process Critical (NtSetInformationProcess) Hides Files Simple Add to Startup (HKCU R

Jan 3, 2023
jacobin - A more than minimal JVM written in Go and capable of running Java 11 bytecode.

This overview gives the background on this project, including its aspirations and the features that it supports. The remaining pages discuss the basics of JVM operation and, where applicable, how Jacobin implements the various steps, noting any items that would be of particular interest to JVM cognoscenti.

Dec 29, 2022
A wrapper for the Wandbox API, written in Golang!

GoWandBox A simple wrapper for the WandBox API, written in Golang! Documentation can be found at classpythonaddike.github.io/gowandbox/ Note: This wra

Sep 19, 2021
A simple typewriter written in go for KOBO e-readers
A simple typewriter written in go for KOBO e-readers

Kobowriter This small project aims to let you use your old KOBO e-reader (mine is a GLO HD) as a simple, distraction free typewriter. For years I thou

Dec 25, 2022
fccCoin Clone written in GoLang

fccCoin Description fccCoin Clone written in GoLang Actual Code for fccCoin written in Python

Oct 2, 2021
Conventional Commits parser written in Go

Conventional Commit Parser This is a parser for Conventional Commits go get -u github.com/release-lab/conventional-commit-parser package main import

Feb 4, 2022