di

package module
v0.0.0-...-8e9da10 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 9 Imported by: 0

README

di

di (dependency-injection) saves you from writing boiler-plate setup code in your main (and elsewhere) function by automating the creation of functions which build the providers you want injected.

Usage

  1. Install di
  2. Create a provider file.
  3. Add a go:generate comment to provider file like so
//go:generate di filename.go
What is a provider file?

First, what is a provider? It's just a function which returns something. That something may be required as a dependency for another provider or not.

A provider file contains all of the definitions for each provider as well as their prerequisites for being built. It's quite literally just a file which contains functions which won't be directly called by your code.

Example More detailed examples can be found within the [examples/|examples/] directory

//go:generate di example.go
package example

import "fmt"

func Path() string {
    return "localhost"
}

func Port() int {
    return 80
}

func Address(host string, port int) string {
    return fmt.Sprintf("%s:%d", host, port)
}

Using the go generate command on example.go would produce the following file

package example

var singleton *ExampleComponent

func GetExampleComponent() *ExampleComponent {
	if singleton == nil {
		singleton = &ExampleComponent{}
	}
	return singleton
}

type ExampleComponent struct {
}

func (c *ExampleComponent) Path() string {
	return Path()
}
func (c *ExampleComponent) Port() int {
	return Port()
}
func (c *ExampleComponent) Address() string {
	return Address(
		c.Path(),
		c.Port(),
	)
}

Which could then be used in your main.go to retrieve a correctly formatted address without having to write the boiler plate or determine the order of which providers to create first.

package main 

import "example"

func main() {
    component := example.GetExampleComponent()
    fmt.Printf(component.Address())
}

Troubleshooting Errors

Type Example Message Mitigation
Missing input(s) di: 2022/06/17 09:27:40 Missing input(s) need to be provided for Foo which returns Bar (type: Baz, named: Qux) Create a provider named Qux which returns a Baz type so that the Foo provider maybe used.
Circular dependency di: 2022/06/17 09:30:19 Circular dependency detected: (type: Foo, named: ExampleA) <-> (type: Bar, named: ExampleB) and (type: Bar, named: ExampleB) <-> (type: Foo, named: ExampleA) There is a circular dependency (Foo requires Bar while Bar also requires Foo) between Foo and Bar which needs to be resolved.
panic: runtime error panic: runtime error: invalid memory address or nil pointer dereference This will happen a majority of the time due to malformed module code. Fix your code. There is also possibility that you've found a bug in di. Feel free to create an issue!

Features

di checks the correctness of your provider files and alerts of the following errors

  • providers which circularly depend on each other or themselves
  • providers which have missing/undefined dependencies

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CyclicalError

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

CyclicalError is used to denote that there are cyclical dependencies within a module.

func (*CyclicalError) Error

func (c *CyclicalError) Error() string

type Generator

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

Generator is the struct used to parse and generate the code which will make up the file for which a component will be defined.

func (*Generator) Generate

func (g *Generator) Generate(files ...string) ([]byte, string)

Generate will parse and generate the code which will later be saved to a file.

func (*Generator) Printf

func (g *Generator) Printf(format string, args ...interface{})

Printf will print to the generator's buffer ensuring that a new line is appended

type MissingError

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

MissingError is used to denote that there are dependencies which were requested, but not provided within a module.

func (*MissingError) Error

func (c *MissingError) Error() string

Directories

Path Synopsis
cmd
di
complex
Code generated by 'di -o=complex_module.gen.go complex_module.go'; DO NOT EDIT.
Code generated by 'di -o=complex_module.gen.go complex_module.go'; DO NOT EDIT.
generics
Code generated by 'di -o=generics.gen.go generics.go'; DO NOT EDIT.
Code generated by 'di -o=generics.gen.go generics.go'; DO NOT EDIT.
simple
Code generated by 'di -o=simple_module.gen.go simple_module.go'; DO NOT EDIT.
Code generated by 'di -o=simple_module.gen.go simple_module.go'; DO NOT EDIT.
singletons
Code generated by 'di -o=singletons_module.gen.go singletons_module.go'; DO NOT EDIT.
Code generated by 'di -o=singletons_module.gen.go singletons_module.go'; DO NOT EDIT.

Jump to

Keyboard shortcuts

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