gaffa

package module
v0.0.0-...-bb15537 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Gaffa

Gaffa (German for Duct Tape) is a dependency injection framework for Go. So far it is just a stripped down version of Service Weaver's dependency injection.

Installation

go install git.deathsgun.xyz/lbuening/gaffa/cmd/gaffa@latest

go install installs the gaffa command to $GOBIN, which defaults to $HOME/go/bin. Make sure this directory is included in your PATH. You can accomplish this, for example, by adding the following to your .bashrc and running source ~/.bashrc:

export PATH=$PATH:$(go env GOPATH)/bin

Usage

Create a new project
mkdir myproject
cd myproject
go mod init myproject
go get git.deathsgun.xyz/lbuening/gaffa
Create main.go
package main

import (
	"context"
	"git.deathsgun.xyz/lbuening/gaffa"
)

type app struct {
	gaffa.Implements[gaffa.Main]
	exampleService gaffa.Ref[IExampleService]
}

func run(ctx context.Context, a *app) error {
	return a.exampleService.DoSomething(ctx)
}

func main() {
	err := gaffa.Run[app, *app](context.Background(), run)
	if err != nil {
		panic(err)
	}
}
Create example_service.go
package main

import (
    "context"
    "fmt"
    "git.deathsgun.xyz/lbuening/gaffa"
)

type IExampleService interface {
    DoSomething(ctx context.Context) error
}

type exampleService struct {
    gaffa.Implements[IExampleService]
}

func (s *exampleService) DoSomething(_ context.Context) error {
    fmt.Println("Hello World!")
    return nil
}
Generate code
gaffa generate ./...
Run
go run .

Output:

Hello World!

Credits

This project is completely based on Service Weaver. The only difference is that I have just stripped the part out for the dependency injection.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run[T any, _ PointerToMain[T]](ctx context.Context, app func(context.Context, *T) error) error

func Type

func Type[T any]() reflect.Type

Types

type Implements

type Implements[T any] struct {
	// contains filtered or unexported fields
}

Implements is a type that is being embedded inside a component implementation struct to indicate that the struct implements a component of type T. For example, consider a Cache component.

type Cache interface {
    Get(ctx context.Context, key string) (string, error)
    Put(ctx context.Context, key, value string) error
}

A concrete type that implements the Cache component is written as follows:

type lruCache struct {
    weaver.Implements[Cache]
    ...
}

Because Implements is embedded inside the component implementation, methods of Implements are available as methods of the component implementation type and can be invoked directly. For example, given an instance c of type lruCache, we can call c.Logger().

func (*Implements[T]) Logger

func (i *Implements[T]) Logger(_ context.Context) *slog.Logger

Logger returns a logger that associates its log entries with this component.

type InstanceOf

type InstanceOf[T any] interface {
	// contains filtered or unexported methods
}

InstanceOf is the interface implemented by a struct that embeds gaffa.Implements[T].

type Main

type Main interface{}

Main is the interface implemented by an application's main component.

type PointerToMain

type PointerToMain[T any] interface {
	*T
	InstanceOf[Main]
}

PointerToMain is a type constraint that asserts *T is an instance of Main (i.e. T is a struct that embeds weaver.Implements[weaver.Main]).

type Ref

type Ref[T any] struct {
	// contains filtered or unexported fields
}

Ref is a field that can be placed inside a component implementation struct. T must be a component type. Service Weaver will automatically fill such a field with a handle to the corresponding component.

func (Ref[T]) Get

func (r Ref[T]) Get() T

Get returns a handle to the component of type T.

Directories

Path Synopsis
cmd
internal
files
Package files contains file-related utilities.
Package files contains file-related utilities.
generate
Package generate contains code which is almost completely stolen from Service Weaver.
Package generate contains code which is almost completely stolen from Service Weaver.

Jump to

Keyboard shortcuts

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