gontainer-helpers

module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT

README

Go Reference Tests Coverage Status Go Report Card Quality Gate Status

Gontainer-helpers

A set of helpers for Gontainer.

Caller

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

More examples

Container

Provides a concurrent-safe DI container.

package main

import (
	"fmt"

	"github.com/gontainer/gontainer-helpers/container"
)

type Person struct {
	Name string
}

type People struct {
    People []Person
}

func main()  {
	// create Mary Jane
	mary := container.NewService()
	mary.SetConstructor(func() Person {
		return Person{}
	})
	mary.SetField("Name", container.NewDependencyValue("Mary Jane"))
	mary.Tag("person", 1) // priority = 1, ladies first :)

	// create Peter Parker
	peter := container.NewService()
	peter.SetConstructor(func() Person {
		return Person{}
	})
	peter.SetField("Name", container.NewDependencyProvider(func() string {
		return "Peter Parker"
	}))
	peter.Tag("person", 0)

	// create "people"
	people := container.NewService()
	people.SetValue(People{})                                       // instead of providing a constructor, we can provide a value directly
	people.SetField("People", container.NewDependencyTag("person")) // fetch all objects tagged as "person", and assign them to the field "people"

	// create a container, and append all services there
	c := container.NewContainer()
	c.OverrideService("mary", mary)
	c.OverrideService("peter", peter)
	c.OverrideService("people", people)

	// instead of these 2 following lines,
	// you can write:
	//
	// peopleObject, _ := c.Get("people")
	var peopleObject People
	_ = c.CopyServiceTo("people", &peopleObject)

	fmt.Printf("%+v\n", peopleObject)

	// Output: {People:[{Name:Mary Jane} {Name:Peter Parker}]}
}

More examples

Copier

var (
    from = 5         // the type of the variable `to` can be different from the type of the variable `from`
    to   interface{} // as long as the value of `from` is assignable to the `to`
)
_ = copier.Copy(from, &to)
fmt.Println(to)
// Output:
// 5

More examples

Exporter

Export the given variable to a GO code.

var s string
s, _ = exporter.Export([]uint{1, 2, 3})
fmt.Println(s)
// Output: []uint{uint(1), uint(2), uint(3)}

More examples

Graph

g := graph.New()
g.AddDep("company", "tech-team")
g.AddDep("tech-team", "cto")
g.AddDep("cto", "company")
g.AddDep("cto", "ceo")
g.AddDep("ceo", "company")

fmt.Println(g.CircularDeps())

// Output:
// [[company tech-team cto company] [company tech-team cto ceo company]]

More examples

Grouperror

Native approach

When errors are being joined using the standard library, the output may be unreadable:

package main

import (
	"errors"
	"fmt"
)

func main() {
	err := errors.Join(
		errors.New("invalid name"),
		nil, // nil-errors are being ignored
		nil,
		errors.New("invalid age"),
	)

	err = fmt.Errorf("validation: %w", err)

	err = errors.Join(
		errors.New("unexpected error"),
		err,
	)

	err = fmt.Errorf("could not create new user: %w", err)

	err = fmt.Errorf("operation failed: %w", err)

	fmt.Println(err.Error())

	// Output:
	// operation failed: could not create new user: unexpected error
	// validation: invalid name
	// invalid age
}

Prefix

Use Prefix to solve that:

package main

import (
	"fmt"
	
	"github.com/gontainer/gontainer-helpers/grouperror"
)

func main()  {
	err := grouperror.Prefix(
		"validation: ",
		errors.New("invalid name"),
		nil, // nil-errors are being ignored
		nil,
		errors.New("invalid age"),
	)

	err = grouperror.Prefix(
		"could not create new user: ",
		errors.New("unexpected error"),
		err,
	)

	err = grouperror.Prefix("operation failed: ", err)

	fmt.Println(err.Error())
	
	// Output:
	// operation failed: could not create new user: unexpected error
	// operation failed: could not create new user: validation: invalid name
	// operation failed: could not create new user: validation: invalid age
	
	// use grouperror.Collection(err) to get a slice with all errors
}

More examples

Setter

person := struct {
    name string
}{}
_ = setter.Set(&person, "name", "Mary")
fmt.Println(person.name)
// Output:
// Mary

More examples

Directories

Path Synopsis
Package caller provides sets of functions that allow calling other functions with unknown sets of arguments.
Package caller provides sets of functions that allow calling other functions with unknown sets of arguments.
Package container provides a set of tools to create a DI container.
Package container provides a set of tools to create a DI container.
graph
Package graph provides tool to detect circular dependencies and find all dependant nodes in a DI container build by `container` package.
Package graph provides tool to detect circular dependencies and find all dependant nodes in a DI container build by `container` package.
Package copier allows for copying a value to a variable with an unknown type.
Package copier allows for copying a value to a variable with an unknown type.
Package errors is deprecated.
Package errors is deprecated.
assert
Package assert is deprecated.
Package assert is deprecated.
Package exporter provides sets of function to export variables to a GO code.
Package exporter provides sets of function to export variables to a GO code.
Package graph provides tool to detect circular dependencies and find all dependant nodes in graphs.
Package graph provides tool to detect circular dependencies and find all dependant nodes in graphs.
Package grouperror provides a toolset to join and split errors.
Package grouperror provides a toolset to join and split errors.
assert
Package assert provides tool to test error groups:
Package assert provides tool to test error groups:
internal
Package setter allows for manipulation of a value of an exported field of any struct.
Package setter allows for manipulation of a value of an exported field of any struct.

Jump to

Keyboard shortcuts

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