depinj

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 8 Imported by: 0

README

depinj

GoDoc Build Status Coverage Status

Dependency injection for Go

Requirements

  • Go 1.13

Tutorial

  1. Import/Export by ref ID
  2. Filter
  3. Ref link
  4. Import/Export by field type
1. Import/Export by ref ID
package main

import (
        "context"
        "fmt"

        "github.com/roy2220/depinj"
)

func main() {
        var podPool depinj.PodPool
        podPool.MustAddPod(&StrangerA{})
        podPool.MustAddPod(&StrangerB{})
        podPool.MustSetUp(context.Background())
        // Output: Hi!
}

type StrangerA struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting string `export:"the_greeting"` // export by ref id `the_greeting`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerA) SetUp(context.Context) error {
        s.Greeting = "Hi!" // set the greeting
        return nil
}

type StrangerB struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting string `import:"the_greeting"` // import by ref id `the_greeting`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerB) SetUp(context.Context) error {
        // at this point, `the_greeting` has been imported
        fmt.Println(s.Greeting) // get the greeting, s.Greeting == "Hi!"
        return nil
}
2. Filter
package main

import (
        "context"
        "fmt"

        "github.com/roy2220/depinj"
)

func main() {
        var podPool depinj.PodPool
        podPool.MustAddPod(&StrangerA{})
        podPool.MustAddPod(&StrangerB{})
        podPool.MustAddPod(&Hijacker{})
        podPool.MustSetUp(context.Background())
        // Output: Hi! Jack!
}

type StrangerA struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting string `export:"the_greeting"` // export by ref id `the_greeting`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerA) SetUp(context.Context) error {
        s.Greeting = "Hi!" // set the greeting
        return nil
}

type StrangerB struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting string `import:"the_greeting"` // import by ref id `the_greeting`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerB) SetUp(context.Context) error {
        // at this point, `the_greeting` has been imported
        fmt.Println(s.Greeting) // get the greeting, s.Greeting == "Hi! Jack!"
        return nil
}

type Hijacker struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting *string `filter:"the_greeting,ModifyGreeting,0"`
        // filter by ref id `the_greeting`, method `ModifyGreeting` and priority `0`
        //
        // The filter method is called after the pod has been set up (SetUp called),
        // it's safe to access the fields of the import or export entries in the pod,
        // which have been initialized.
        //
        // The higher priority value, the earlier call to the filter method, it's
        // useful if there are multiple filter entries for one export entry.
}

// ModifyGreeting is designated by the filter tag and called along with podPool.MustSetUp
func (h *Hijacker) ModifyGreeting(context.Context) error {
        // at this point, `the_greeting` has been exported but has not yet been imported by others
        *h.Greeting += " Jack!" // modify the greeting
        return nil
}
package main

import (
        "context"
        "fmt"

        "github.com/roy2220/depinj"
)

func main() {
        var podPool depinj.PodPool
        podPool.MustAddPod(&StrangerA{})
        podPool.MustAddPod(&StrangerB{})
        podPool.MustSetUp(context.Background())
        // Output: Hello!
}

type StrangerA struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting1 string `export:"the_greeting_1"` // export by ref id `the_greeting_1`
        Greeting2 string `export:"the_greeting_2"` // export by ref id `the_greeting_2`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerA) SetUp(context.Context) error {
        s.Greeting1 = "Hi!"    // set the greeting 1
        s.Greeting2 = "Hello!" // set the greeting 2
        return nil
}

type StrangerB struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Greeting string `import:"@guess_what"` // import by ref link `@guess_what`
}

// ResolveRefLink is called along with podPool.MustSetUp
func (s *StrangerB) ResolveRefLink(refLink string) (string, bool) {
        // refLink == "@guess_what"
        return "the_greeting_2", true // resolve ref link `@guess_what` into ref id `the_greeting_2`
}

// SetUp is called along with podPool.MustSetUp
func (s *StrangerB) SetUp(context.Context) error {
        // at this point, `@guess_what` has been imported
        fmt.Println(s.Greeting) // get the greeting, s.Greeting == "Hello!"
        return nil
}
4. Import/Export by field type
package main

import (
        "context"
        "errors"
        "fmt"

        "github.com/roy2220/depinj"
)

func main() {
        var podPool depinj.PodPool
        podPool.MustAddPod(&Foo{})
        podPool.MustAddPod(&Bar{})
        podPool.MustSetUp(context.Background())
        // Output: unknown error
}

type Foo struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Err error `export:""` // ref id omitted, export by field type - error
}

var ErrUnknown = errors.New("unknown error")

// SetUp is called along with podPool.MustSetUp
func (f *Foo) SetUp(context.Context) error {
        f.Err = ErrUnknown // set the error
        return nil
}

type Bar struct {
        depinj.DummyPod // default implementation of depinj.Pod

        Err error `import:""` // ref id omitted, import by field type - error
}

// SetUp is called along with podPool.MustSetUp
func (b *Bar) SetUp(context.Context) error {
        // at this point, error has been imported
        fmt.Println(b.Err) // get the error, b.Err == ErrUnknown
        return nil
}

Documentation

Overview

Package depinj implements dependency injection.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPod            = errors.New("depinj: invalid pod")
	ErrBadImportEntry        = errors.New("depinj: bad import entry")
	ErrBadExportEntry        = errors.New("depinj: bad export entry")
	ErrBadFilterEntry        = errors.New("depinj: bad filter entry")
	ErrPodCircularDependency = errors.New("depinj: pod circular dependency")
)

Sentinel errors

Functions

This section is empty.

Types

type DummyPod

type DummyPod struct{}

DummyPod is the dummy implementation of Pod. It could be embedded as the default implementation of Pod.

func (DummyPod) ResolveRefLink(refLink string) (refID string, ok bool)

ResolveRefLink does nothing.

func (DummyPod) SetUp

func (DummyPod) SetUp(ctx context.Context) (err error)

SetUp does nothing.

func (DummyPod) TearDown

func (DummyPod) TearDown()

TearDown does nothing.

type Pod

type Pod interface {
	// ResolveRefLink resolves the given ref link into a ref id.
	// It returns false if the ref link is unresolvable. When it
	// is called, the fields of the import/export/filter entries
	// have not yet been initialized, don't access them.
	ResolveRefLink(refLink string) (refID string, ok bool)

	// SetUp is called along with the setup of PodPool. When it
	// is called, all the fields of the import entries have been
	// initialized. The fields of the export entries should be
	// initialized within this method.
	SetUp(ctx context.Context) (err error)

	// TearDown is called along with the teardown of PodPool.
	// The fields of the export entries should be finalized
	// as necessary within this method.
	TearDown()
}

Pod represents a container for dependency injection.

type PodPool

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

PodPool represents a set of pods.

func (*PodPool) AddPod

func (pp *PodPool) AddPod(rawPod Pod) error

AddPod adds the given pod to the pool.

func (*PodPool) MustAddPod

func (pp *PodPool) MustAddPod(rawPod Pod)

MustAddPod adds the given pod to the pool, it panics if any error occurs.

func (*PodPool) MustSetUp

func (pp *PodPool) MustSetUp(ctx context.Context)

MustSetUp sets up all the pods in the pool, it panics if any error occurs.

func (*PodPool) SetUp

func (pp *PodPool) SetUp(ctx context.Context) (returnedErr error)

SetUp sets up all the pods in the pool.

func (*PodPool) TearDown

func (pp *PodPool) TearDown()

TearDown tears down all the pods in the pool in a reverse order of setups.

Jump to

Keyboard shortcuts

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