Calling functions by name and getting outputs by using reflect package.

Invoker

godoc license

A library to call (invoke) functions by taking names and sample inputs of those functions as parameters. And returns the types and values of output.

Purpose

Testing the result of multiple functions and business logic.

Usage

go get github.com/odese/invoker

Call Example for Single Function:

package main

import (
	"fmt"

	"github.com/odese/invoker"
)

func main() {
	outputTypes, outputValues, err := invoker.Invoke(Sum, []interface{}{1, 2})
	fmt.Println("outputTypes: ", outputTypes)
	fmt.Println("outputValues: ", outputValues)
	fmt.Println("err: ", err)
}


func Sum(a, b int) (r Response, sum int, err error) {
	r.Field1 = a + b
	r.Field2 = "Sum2 Response"
	sum = a + b
	return r, sum, err
}

type Response struct {
	Field1 int
	Field2 string
}

// Output:
// outputTypes:  [<main.Response Value> <int Value> <error Value>]
// outputValues:  [{3 Sum2 Response} 3 <nil>]
// err:  <nil>

Call Example for Consecutive Functions:

package main

import (
	"fmt"

	"github.com/odese/invoker"
)

func main() {
	args := make([]invoker.FuncArguments, 0)
	var arg invoker.FuncArguments
	arg.Function = Sum
	arg.Inputs = []interface{}{1, 2}
	args = append(args, arg)

	arg.Function = Sum
	arg.Inputs = []interface{}{2, 3}
	args = append(args, arg)

	for i := 0; i < len(args); i++ {
		outputTypes, outputValues, err := invoker.Invoke(args[i].Function, args[i].Inputs)
		fmt.Println("outputTypes: ", outputTypes)
		fmt.Println("outputValues: ", outputValues)
		fmt.Println("err: ", err)
	}
}

func Sum(a, b int) (r Response, sum int, err error) {
	r.Field1 = a + b
	r.Field2 = "Sum2 Response"
	sum = a + b
	return r, sum, err
}

type Response struct {
	Field1 int
	Field2 string
}

// Output:
// outputTypes:  [<main.Response Value> <int Value> <error Value>]
// outputValues:  [{3 Sum2 Response} 3 <nil>]
// err:  <nil>
// 
// outputTypes:  [<main.Response Value> <int Value> <error Value>]
// outputValues:  [{5 Sum2 Response} 5 <nil>]
// err:  <nil>

Call Example for Arbitrary Ordered Functions:

package main

import (
	"fmt"

	"github.com/odese/invoker"
)

func main() {
	var arg1 invoker.FuncArguments
	arg1.Function = Sum
	arg1.Inputs = []interface{}{1, 2}
	
	var arg2 invoker.FuncArguments
	arg2.Function = Sum
	arg2.Inputs = []interface{}{2, 3}
	
	funcMap := map[string]invoker.FuncArguments{
		"b" : arg2,
		"a" : arg1,
	}

		for k, v := range funcMap {
		fmt.Println(k)
		outputTypes, outputValues, err := invoker.Invoke(funcMap[k].Function, v.Inputs)
		
		fmt.Println("outputTypes: ", outputTypes)
		fmt.Println("outputValues: ", outputValues)
		fmt.Println("err: ", err)
	}
}

func Sum(a, b int) (r Response, sum int, err error) {
	r.Field1 = a + b
	r.Field2 = "Sum2 Response"
	sum = a + b
	return r, sum, err
}

type Response struct {
	Field1 int
	Field2 string
}

// Output:
// b
// outputTypes:  [<main.Response Value> <int Value> <error Value>]
// outputValues:  [{5 Sum2 Response} 5 <nil>]
// err:  <nil>
// a
// outputTypes:  [<main.Response Value> <int Value> <error Value>]
// outputValues:  [{3 Sum2 Response} 3 <nil>]
// err:  <nil>
Similar Resources

Extended library functions using generics in Go.

Just few extended standard library functions for Golang using generics.

Dec 16, 2021

Helper functions for common scenarios, using Go generics.

zeroflucs generics When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain

Feb 18, 2022

Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Jan 4, 2023

Simple library to handle ANSI functions and parsing of color formatting strings

Emerald A basic color library for use in my Go projects, built on top of mgutz/ansi. Package ansi is a small, fast library to create ANSI colored stri

Oct 28, 2022

Tiny Go tool for running multiple functions concurrently and collecting their results into an error slice.

Overview Short for "ConCurrent". Tiny Go tool for running multiple functions concurrently and collecting their results into an error slice. Dependency

Nov 22, 2021

Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023

A super simple Lodash like utility library with essential functions that empowers the development in Go

A super simple Lodash like utility library with essential functions that empowers the development in Go

A simple Utility library for Go Go does not provide many essential built in functions when it comes to the data structure such as slice and map. This

Jan 4, 2023

This is an open source project for commonly used functions for the Go programming language.

Common Functions This is an open source project for commonly used functions for the Go programming language. This package need = go 1.3 Code Conventi

Jan 8, 2023

Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022
Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English 简体中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

Dec 9, 2022
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Feb 5, 2022
A hands-on approach for getting started with Go generics.

Go Generics the Hard Way This repository is a hands-on approach for getting started with Go generics: Prerequisites: how to install the prerequisites

Dec 27, 2022
Getting into Golang 1.18

Go 1.18 New features of Golang 1.18 with tutorial and examples. In this repository I introduce new features of Golang version 1.18 with their examples

Aug 28, 2022
MCsniperGO, a fast, efficient, and feature-packed minecraft name sniper.

MCsniperGO This project was made possible by my donators Usage This sniper is in it's beta stage, meaning bugs should be expected. Easy installation d

Dec 31, 2022
generate random data like name, email, uuid, address, images and etc.

gg-rand generate random data like name, email, uuid, address, images and etc. build and install: make run: gg-rand $ gg-rand SillyName : Knavesa

Nov 16, 2022
A Golang tool to whitelist ASN's based on organization name

A Golang tool to whitelist ASN's based on organization name. This works by providing a list of ASN org names. This tool uses goPacket to monitor incoming traffic, capturing the IP's and checking the IP to see if it is a part of a whitelisted ASN. If it is not, it blocks that connection and future connections using iptables.

Jul 23, 2022
Go linter to check the struct literal to use field name

Structfield Find struct literals using non-labeled fields. The structfield analysis reports the usage of struct literal using non-labeled fields more

Aug 23, 2021
A parser for Ontario's baby name data
A parser for Ontario's baby name data

obnp What? A parser for Ontario's baby name data Why? I wanted to see if a specific name existed in both the male and female datasets. This tool is mo

Mar 15, 2022
This Go package allows you to set handler functions that run when named events occur

This Go package allows you to set handler functions that run when named events occur

Feb 10, 2022