container

package
v0.0.0-...-772259c Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

Ghast's DI Container

Ghast ships with a rudimentary DI container. Future plans include expanding upon this DI container and ultimately running all of Ghast through the container. For now, you can work with your own DI container like so:

package container

import (
	"testing"
	ghastContainer "github.com/bradcypert/ghast/pkg/container"
)

type Foo interface {
	hasFoo() bool
}

type Bar struct {
	secretKey string
}

func (b Bar) hasFoo() bool {
	return false
}

func TestResponses(t *testing.T) {
	t.Run("Should bind to the container correctly", func(t *testing.T) {
		container := ghastContainer.NewContainer()

		container.Bind("SECRET_KEY", func(container Container) interface{} {
			return "ABC123"
		})
		container.Bind("Bar", func(container Container) interface{} {
			return Bar{
				container.Make("SECRET_KEY").(string),
			}
		})

		bar := container.Make("Bar").(Bar)
		if bar.secretKey != "ABC123" {
			t.Errorf("Bound bar does not have the correct secret key")
		}

		if bar.hasFoo() != false {
			t.Errorf("Bound bar does not have the correct hasFoo implementation")
		}
	})
}

This example looks like a test, and that's because it is! You'll be able to find good examples of how to implement all of ghast by looking at the test files that live alongside the source files!

You'll notice that, at least as of now, you will have to perform a type conversion, as the container currently returns an interface{}. It is suggested that the DI keys that you bind against help provide context to the underlying type, so that other developers aren't confused about what is coming out of your container when they call Make.

Documentation

Index

Constants

View Source
const (
	AppKey    = "ghast/app"
	RouterKey = "ghast/router"
	PortKey   = "@ghast.config.port"
)

These keys are defined for use by the Ghast framework. Please do not use these keys when defining your own bindings in the container. Keys that begin with an @ are generated when parsing the YAML config for a Ghast application.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container provides a DI container to help manage application dependencies

func NewContainer

func NewContainer() *Container

NewContainer creates a new DI container

func (*Container) Bind

func (c *Container) Bind(key string, fn func(c *Container) interface{})

Bind binds the result of the provided function as the value to be found when making the given key.

func (*Container) Make

func (c *Container) Make(key string) interface{}

Make creates an instance of the provided interface

Jump to

Keyboard shortcuts

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