autowire

command module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 0 Imported by: 0

README

Autowire

Autowire is a code generation tool for easy use of google wire

Wire is a code generation tool that automates connecting components using dependency injection. wire is useful but sometimes you still need to do some handwriting. this tool aims to simplify your work by automatically complete your source code

Installing

Install autowire by running:

go install github.com/hauntedness/autowire/cmd/autowire@latest

Usage

Move to target package

cd ~/projects/autowire/example/inj

Make sure proper build tag and the entry provider exist

//go:build wireinject

package inj

import (
	"github.com/google/wire"
	"github.com/hauntedness/autowire/example/inj/liu"
	"github.com/hauntedness/autowire/example/inj/zhao"
)

type Shu struct{}

func NewShu(liu *liu.Liu, zhao *zhao.Zhao) *Shu {
	return &Shu{}
}

func InitShu() *Shu {
	wire.Build(NewShu)
	return nil
}

Run autowire

autowire 

Or run go tool

go run github.com/hauntedness/autowire/cmd/autowire@latest

Now you should see the code is refactored.

//go:build wireinject

package inj

import (
	"github.com/google/wire"
	"github.com/hauntedness/autowire/example/inj/guan"
	"github.com/hauntedness/autowire/example/inj/liu"
	"github.com/hauntedness/autowire/example/inj/zhang"
	"github.com/hauntedness/autowire/example/inj/zhang/yanyan"
	"github.com/hauntedness/autowire/example/inj/zhao"
)

type Shu struct{}

func NewShu(liu *liu.Liu, zhao *zhao.Zhao) *Shu {
	return &Shu{}
}

func InitShu() *Shu {
	wire.Build(NewShu, yanyan.NewYanYan, liu.NewLiu, zhao.NewZhao, guan.NewGuan, zhang.NewZhang)
	return nil
}

Note

Current limitation

  • The code completion only works for the function provider, A workaround is manually create a function
  • By default, autowire only treat functions like NewXXX() bean as a valid provider
  • Autowire also have a default algorithm to pick provider from multiple matches.
  • If the default behavior is not what you need, you can replace it with your own implementation. see github.com/hauntedness/autowire/pkg.ProcessConfigurer.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
msg
pkg
copied from github.com/google/wire/internal/wire
copied from github.com/google/wire/internal/wire

Jump to

Keyboard shortcuts

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