caller

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package caller provides sets of functions that allow calling other functions with unknown sets of arguments.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(fn interface{}, params ...interface{}) ([]interface{}, error)

Call calls the given function with the given arguments. It returns values returned by the function in a slice.

Example (Error)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

func main() {
	fn := func(a int, b int) int {
		return a * b
	}
	_, err := caller.Call(fn, "2", "2")
	fmt.Println(err)
}
Output:
arg0: cannot cast `string` to `int`
arg1: cannot cast `string` to `int`
Example (Ok)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

type Person struct {
	name string
}

func (p *Person) SetName(n string) {
	p.name = n
}

func (p Person) WithName(n string) Person {
	p.name = n
	return p
}

func main() {
	// type Person struct {
	//     name string
	// }
	//
	// func (p *Person) SetName(n string) {
	//     p.name = n
	// }

	p := &Person{}
	_, _ = caller.Call(p.SetName, "Mary")
	fmt.Println(p.name)
}
Output:
Mary
Example (ReturnValue)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

func main() {
	fn := func(a int, b int) int {
		return a * b
	}
	r, _ := caller.Call(fn, 2, 2)
	fmt.Println(r[0])
}
Output:
4

func CallByName

func CallByName(object interface{}, method string, params ...interface{}) ([]interface{}, error)

CallByName works similar to Call with the difference it calls the method by the name over the given receiver.

Example
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

type Person struct {
	name string
}

func (p *Person) SetName(n string) {
	p.name = n
}

func (p Person) WithName(n string) Person {
	p.name = n
	return p
}

func main() {
	// type Person struct {
	//     name string
	// }
	//
	// func (p *Person) SetName(n string) {
	//     p.name = n
	// }

	p := &Person{}
	_, _ = caller.CallByName(p, "SetName", "Mary")
	fmt.Println(p.name)
}
Output:
Mary

func CallProvider

func CallProvider(provider interface{}, params ...interface{}) (interface{}, error)

CallProvider works similar to Call with the difference it requires a provider as the first argument. Provider is a function which returns 1 or 2 values. The second return value which is optional must be a type of error. TODO see the docs for template.FuncMap

p := func() (interface{}, error) {
    db, err := sql.Open("mysql", "user:password@/dbname")
    if err != nil {
         return nil, err
    }

    db.SetConnMaxLifetime(time.Minute * 3)
    db.SetMaxOpenConns(10)
    db.SetMaxIdleConns(10)

    return db, nil
}

mysql, err := CallProvider(p)
Example
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

type Person struct {
	name string
}

func NewPerson(name string) *Person {
	return &Person{name: name}
}

func (p *Person) SetName(n string) {
	p.name = n
}

func (p Person) WithName(n string) Person {
	p.name = n
	return p
}

func main() {
	// type Person struct {
	//     Name string
	// }
	//
	// func NewPerson(name string) *Person {
	//     return &Person{Name: name}
	// }

	p, _ := caller.CallProvider(NewPerson, "Mary")
	fmt.Printf("%+v", p)
}
Output:
&{name:Mary}

func CallWitherByName

func CallWitherByName(object interface{}, wither string, params ...interface{}) (interface{}, error)

CallWitherByName works similar to CallByName with the difference the method must be a wither.

type Person struct {
    name string
}

func (p Person) WithName(n string) Person {
    p.Name = n
    return p
}

func main() {
    p := Person{}
    p2, _ := caller.CallWitherByName(p, "WithName", "Mary")
    fmt.Printf("%+v", p2) // {name:Mary}
}
Example
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/caller"
)

type Person struct {
	name string
}

func (p *Person) SetName(n string) {
	p.name = n
}

func (p Person) WithName(n string) Person {
	p.name = n
	return p
}

func main() {
	// type Person struct {
	//     name string
	// }
	//
	// func (p Person) WithName(n string) Person {
	//     p.name = n
	//     return p
	// }

	p := Person{}
	p2, _ := caller.CallWitherByName(p, "WithName", "Mary")
	fmt.Printf("%+v", p2)
}
Output:
{name:Mary}

Types

This section is empty.

Jump to

Keyboard shortcuts

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