di

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

// struct with named and unnamed dependencies.
type root struct {
	Nested *nested `di:""`
	Named  *named  `di:"name=someName"`
}

// struct with interface dependency.
type nested struct {
	W worker `di:""`
}

type named struct {
	name string
}

type worker interface {
	Work() string
}

type builder struct {
	work string
}

func (b *builder) Work() string {
	return b.work
}

func main() {
	// create the dependencies.
	r := &root{}
	nam := &named{name: "Pesho"}
	nes := &nested{}
	b := &builder{work: "quick start"}

	// create the di container and add all dependencies to it.
	c := di.NewContainer()
	err := c.Register(
		&di.Dependency{Value: r},
		&di.Dependency{Value: nam, Name: "someName"}, // register the named dependency with the same name as in the struct definition.
		&di.Dependency{Value: nes},
		&di.Dependency{Value: b},
	)
	if err != nil {
		panic(err)
	}

	// resolve all registered dependencies
	err = c.ResolveAll()
	if err != nil {
		panic(err)
	}

	// use the resolved dependencies
	fmt.Println("Worker:", r.Nested.W.Work())
	fmt.Println("Name:", r.Named.name)
}
Output:

Worker: quick start
Name: Pesho

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container is the di container.

func NewContainer

func NewContainer() *Container

NewContainer creates new di container.

Example
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	container := di.NewContainer()
	fmt.Println(container)
}
Output:

func (*Container) Register

func (c *Container) Register(deps ...*Dependency) error

Register adds the provided dependencies to the container.

Example
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	type p struct {
		prop string
	}
	type v struct {
		value int
		Prop  *p `di:""`
	}
	type d1 struct {
		Dep  *v `di:""`
		Prop *p `di:"name=resolveMe"`
	}

	c := di.NewContainer()
	err := c.Register(
		&di.Dependency{Value: new(d1)},
		&di.Dependency{Value: &v{value: 5}},
		&di.Dependency{Value: &p{prop: "named"}, Name: "resolveMe"}, // Register named dependency.
		&di.Dependency{Value: &p{prop: "unnamed"}},                  // Register unnamed dependency.
	)
	if err != nil {
		panic(err)
	}

	res := new(d1)
	c.Resolve(res)

	fmt.Println("Dep:", res.Dep.value)
	fmt.Println("Prop named:", res.Prop.prop)
	fmt.Println("Prop unnamed:", res.Dep.Prop.prop)
}
Output:

Dep: 5
Prop named: named
Prop unnamed: unnamed

func (*Container) Resolve

func (c *Container) Resolve(out interface{}) error

Resolve sets the out parameter to the resolved dependency value.

Example (Interface)
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

type breaker interface {
	Break() string
}

type programmer struct {
	Item *breakable `di:""`
}

func (p *programmer) Break() string {
	return p.Item.name
}

type breakable struct {
	name string
}

func main() {
	// Create the di container and add all dependencies to it.
	c := di.NewContainer()
	c.Register(
		&di.Dependency{Value: new(programmer)},          // Register the interface implementation.
		&di.Dependency{Value: &breakable{name: "code"}}, // Register other dependencies.
	)

	// Resolve the interface dependency.
	// Due to a limitation in the current go version (go1.9.4), if the out
	// parameter is interface, it should be pointer to interface.
	res := new(breaker)
	err := c.Resolve(res)
	if err != nil {
		panic(err)
	}

	fmt.Println("Breaking:", (*res).Break()) // Deref the interface and use its value.
}
Output:

Breaking: code
Example (Struct)
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

type magic interface {
	Magic() string
}

type magician struct {
	Spell *spell `di:""`
}

func (m *magician) Magic() string {
	return m.Spell.name
}

type spell struct {
	name string
}

func main() {
	// create the di container and add all dependencies to it.
	c := di.NewContainer()
	c.Register(
		&di.Dependency{Value: new(magician)},
		&di.Dependency{Value: &spell{name: "fireblast"}},
	)

	// resolve the struct dependency.
	res := new(magician)
	err := c.Resolve(res)
	if err != nil {
		panic(err)
	}

	fmt.Println("Magic:", res.Magic())
}
Output:

Magic: fireblast

func (*Container) ResolveAll

func (c *Container) ResolveAll() error

ResolveAll populates the marked dependencies with the registered dependencies.

Example
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	type d1 struct {
		v string
	}
	// First hierarchy.
	type h1 struct {
		Dep *d1 `di:""`
	}

	type d2 struct {
		v string
	}
	// Second hierarchy.
	type h2 struct {
		Dep *d2 `di:""`
	}

	c := di.NewContainer()
	first := new(h1)
	second := new(h2)
	c.Register(
		&di.Dependency{Value: &d2{v: "d2"}},
		&di.Dependency{Value: &d1{v: "d1"}},
		&di.Dependency{Value: first},
		&di.Dependency{Value: second},
	)

	err := c.ResolveAll()
	if err != nil {
		panic(err)
	}

	fmt.Println("First:", first.Dep.v)
	fmt.Println("Second:", second.Dep.v)
}
Output:

First: d1
Second: d2

func (*Container) ResolveByName

func (c *Container) ResolveByName(name string, out interface{}) error

ResolveByName sets the out parameter to the resolved by name dependency value.

Example
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	type dep struct {
		value string
	}

	c := di.NewContainer()
	c.Register(
		&di.Dependency{Value: &dep{value: "unnamed"}},
		&di.Dependency{Value: &dep{value: "named"}, Name: "pickMe"},
	)

	named := new(dep)
	unnamed := new(dep)
	err := c.ResolveByName("pickMe", named)
	if err != nil {
		panic(err)
	}

	fmt.Println("Named:", named.value)

	c.Resolve(unnamed)
	fmt.Println("Unnamed:", unnamed.value)
}
Output:

Named: named
Unnamed: unnamed

func (*Container) ResolveNew

func (c *Container) ResolveNew(out interface{}) error

ResolveNew returns new instance of the provided type. The dependencies of the instance marked for resolving will not be new.

Example (NotRegistered)
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	type dep struct {
		value int
	}

	c := di.NewContainer()
	res := new(dep)
	err := c.ResolveNew(res)
	if err != nil {
		panic(err)
	}

	fmt.Println("Result:", res.value)
}
Output:

Result: 0
Example (Registered)
package main

import (
	"fmt"

	"github.com/TsvetanMilanov/go-simple-di/di"
)

func main() {
	type dep struct {
		value int
	}

	c := di.NewContainer()
	c.Register(
		&di.Dependency{Value: &dep{value: 400}},
	)

	res := new(dep)
	err := c.ResolveNew(res)
	if err != nil {
		panic(err)
	}

	fmt.Println("Result:", res.value) // The result won't be 400.
}
Output:

Result: 0

type Dependency

type Dependency struct {
	Name  string
	Value interface{}
}

Dependency is di dependency.

Jump to

Keyboard shortcuts

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