Code Monkey home page Code Monkey logo

go-autowire's Introduction

Go-AutoWire

helps you to generate wire files with easy annotate

中文文档

this project is base on wire

but it did simplify the wire usage and make wire much more stronger

Installation

Install Wire by running:

go get github.com/google/wire/cmd/wire

then

Install Gutowire by running:

go get github.com/Just-maple/go-autowire/cmd/gutowire

and ensuring that $GOPATH/bin is added to your $PATH.

Usage example

If you want to build a zoo,you may need some dependencies like animals

package example

type Zoo struct{ 
    Cat         Cat
    Dog         Dog
    FlyAnimal FlyAnimal
}

type Cat struct{
}

type FlyAnimal interface{
    Fly()
}

type Bird struct{
}

func (b Bird)Fly(){
}

type Dog struct{
}

in traditional wire,you need to write some files to explain the wire relation to google/wire

package example_zoo

import (
	"github.com/google/wire"
)

var zooSet = wire.NewSet(
	wire.Struct(new(Zoo), "*"),
)

var animalsSet = wire.NewSet(
	wire.Struct(new(Cat), "*"),
	wire.Struct(new(Dog), "*"),

	wire.Struct(new(Bird), "*"),
	wire.Bind(new(FlyAnimal), new(Bird)),
)

var sets = wire.NewSet(zooSet, animalsSet)

func InitZoo() Zoo {
	panic(wire.Build(sets))
}

you need to rewrite your wire.go and comes much more harder to manager all the dependencies

as your zoo goes bigger and bigger

but now

you can use gutowire

write annotate as below

package example

// @autowire.init(set=zoo)
// it will be collect into zooSet (this comment is not necessary)
type Zoo struct{ 
    Cat         Cat
    Dog         Dog
    FlyAnimal FlyAnimal
}

// it will be collect into animalsSet (this comment is not necessary)
// @autowire(set=animals)
type Cat struct{
}


type FlyAnimal interface{
    Fly()
}

// it will be collect into animalsSet and wire as interface FlyAnimal (this comment is not necessary)
// @autowire(set=animals,FlyAnimal)
type Bird struct{
}

func (b Bird)Fly(){
}

// it will be collect into animalsSet (this comment is not necessary)
// @autowire(set=animals)
type Dog struct{
}

.init in @autowire.init(set=zoo) will auto write InitializeZoo func in wire.gen.go like below:

// Code generated by go-autowire. DO NOT EDIT.

// +build wireinject
//
package example_zoo

import "github.com/google/wire"

func InitializeZoo() (*Zoo, func(), error) {
	panic(wire.Build(Sets))
}

and run

gutowire -s ./example_zoo ./example_zoo

-s means scope to look up build dependencies

all the wire files you need will generate

look at file generated in example_zoo

go-autowire's People

Contributors

just-maple avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-autowire's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.