whyor

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 0 Imported by: 0

README

whyor

Compile-time dependency injection for Go. Successor to Google Wire, with a generics-based API. Generates plain Go; nothing runs at runtime.

//go:build whyor

var DBSet = whyor.Set(NewConfig, NewPG, whyor.Bind[Store, *PG]())

func InitApp(dsn string) (*App, func(), error) {
	panic(whyor.Build(DBSet, NewApp))
}
go run github.com/Medzoner/whyor/cmd/whyor gen ./...    # writes whyor_gen.go
go run github.com/Medzoner/whyor/cmd/whyor check ./...  # exit 1 if stale
go run github.com/Medzoner/whyor/cmd/whyor init ./internal/app  # wire.go skeleton

Regenerate on every change (polling, no extra dependency):

go run github.com/Medzoner/whyor/cmd/whyor gen -w ./...

Or with go generate, from a file without the whyor tag:

//go:generate go run github.com/Medzoner/whyor/cmd/whyor gen .

API:

  • Build, Set: declare an injector / group providers.
  • Bind[I, T](): provide interface I with concrete T.
  • Value[T](v): provide a literal.
  • Struct[T](): provide T and *T by filling exported fields (skip with whyor:"-").
  • FieldsOf[T]("A", "B"): provide fields of a struct as dependencies.
  • Many[T](providers...): provide a []T.
  • AutoBind[T](): use T for any needed interface it implements (must be unambiguous).
  • Closer[T](): call T.Close() in the injector's cleanup.

Providers are functions returning T, (T, func()), (T, error) or (T, func(), error). See examples/basic.

Documentation

Overview

Package whyor is a compile-time dependency injection toolkit.

Injectors are declared in files guarded by the "whyor" build tag:

//go:build whyor

func InitApp(path string) (*App, func(), error) {
	panic(whyor.Build(NewConfig, NewDB, whyor.Bind[Store, *PG]()))
}

`whyor gen` then writes a whyor_gen.go file containing plain Go code. Nothing in this package does any work at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(opts ...Option) any

Build declares an injector body. It must be the argument of panic in an injector function. It is replaced by generated code.

Types

type Option

type Option any

Option is a provider, a Set, a Bind or a Value accepted by Build and Set. Plain provider functions are accepted too.

func AutoBind

func AutoBind[T any]() Option

AutoBind makes T usable wherever an interface it implements is needed, as long as exactly one registered AutoBind type implements that interface.

func Bind

func Bind[I, T any]() Option

Bind tells the generator that interface I is satisfied by concrete type T.

func Closer

func Closer[T any]() Option

Closer registers T's Close method as its cleanup: the injector must then return a func(). It applies to providers of T that have no cleanup of their own.

func FieldsOf added in v0.2.0

func FieldsOf[T any](fields ...string) Option

FieldsOf provides the named fields of T (a struct or pointer to struct) as dependencies of their own types.

func Many

func Many[T any](opts ...Option) Option

Many provides a []T made of the given providers, each returning a value assignable to T. Its elements are not provided as T on their own.

func Set

func Set(opts ...Option) Option

Set groups providers and options so they can be reused.

func Struct added in v0.2.0

func Struct[T any]() Option

Struct provides both T and *T by filling the exported fields of struct T with dependencies. Fields tagged `whyor:"-"` are left zero.

func Value

func Value[T any](v T) Option

Value provides the given expression as a dependency of type T.

Directories

Path Synopsis
cmd
whyor command
Command whyor generates dependency injection code.
Command whyor generates dependency injection code.
examples
internal
gen
Package gen implements the whyor code generator.
Package gen implements the whyor code generator.

Jump to

Keyboard shortcuts

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