Giter VIP home page Giter VIP logo

dicon's Introduction

dicon

[DEPRECATED] this repository has been moved to: https://github.com/recruit-tech/dicon

DICONtainer Generator for go.

CircleCI Go Report Card

Getting Started

Prerequisites

  • Go 1.9+
  • make

Installing

$ go get -u github.com/akito0107/dicon

How to use

  1. Write container interface and comment +DICON over it.
// +DICON
type Container interface {
	UserService() (UserService, error)
	UserRepository() (UserRepository, error)
}
  1. Prepare dependencies. You must write constructor which meets below requirements:
  • method name must be New + Interface name
  • return type must be (Interface, error) tuple.
  • dependencies which use this instance must be passed via the constructor.
type User struct {
	ID   int64
	Name string
}
type UserRepository interface {
	FindByID(id int64) (*User, error)
}

type userRepository struct{}

func (*userRepository) FindByID(id int64) (*User, error) {
	// STUB
	return &User{ID: id, Name: "foo"}, nil
}

func NewUserRepository() (UserRepository, error) {
	return &userRepository{}, nil
}
type UserService interface {
	Find(id int64) (*User, error)
}

type userService struct {
	repo UserRepository
}

func (us *userService) Find(id int64) (*User, error) {
	return us.repo.FindByID(id)
}

func NewUserService(repo UserRepository) (UserService, error) {
	return &userService{
		repo: repo,
	}, nil
}
  1. generate!
$ dicon generate --pkg sample
  1. You can get the container implementation!
// Code generated by "dicon"; DO NOT EDIT.

package sample

import (
	"fmt"

	"github.com/pkg/errors"
)

type dicontainer struct {
	store map[string]interface{}
}

func NewDIContainer() Container {
	return &dicontainer{
		store: map[string]interface{}{},
	}
}

func (d *dicontainer) UserRepository() (UserRepository, error) {
	if i, ok := d.store["UserRepository"]; ok {
		instance, ok := i.(UserRepository)
		if !ok {
			return nil, fmt.Errorf("invalid instance is cached %v", instance)
		}
		return instance, nil
	}
	instance, err := NewUserRepository()
	if err != nil {
		return nil, errors.Wrap(err, "creation UserRepository failed at DICON")
	}
	d.store["UserRepository"] = instance
	return instance, nil
}
func (d *dicontainer) UserService() (UserService, error) {
	if i, ok := d.store["UserService"]; ok {
		instance, ok := i.(UserService)
		if !ok {
			return nil, fmt.Errorf("invalid instance is cached %v", instance)
		}
		return instance, nil
	}
	dep0, err := d.UserRepository()
	if err != nil {
		return nil, errors.Wrap(err, "resolve UserRepository failed at DICON")
	}
	instance, err := NewUserService(dep0)
	if err != nil {
		return nil, errors.Wrap(err, "creation UserService failed at DICON")
	}
	d.store["UserService"] = instance
	return instance, nil
}
  1. Use it!
di := NewDIContainer()
u, err := di.UserService()
....

Generate Mock

dicon's target interfaces are often mocked in unit tests. So, dicon also provides a tool for automated mock creation.

You just type

$ dicon generate-mock --pkg sample

then, you get mocks (by the default, under the mock package)

// Code generated by "dicon"; DO NOT EDIT.

package mock

type UserRepositoryMock struct {
	FindByIdMock func(a0 int64) (*entity.User, error)
}

func NewUserRepositoryMock() *UserRepositoryMock {
	return &UserRepositoryMock{}
}

func (mk *UserRepositoryMock) FindById(a0 int64) (*entity.User, error) {
	return mk.FindByIdMock(a0)
}

type UserServiceMock struct {
	FindMock   func(a1 int64) (*entity.User, error)
}

func NewUserServiceMock() *UserServiceMock {
	return &UserServiceMock{}
}

func (mk *UserServiceMock) Find(a0 int64) (*entity.User, error) {
	return mk.FindMock(a0)
}

Generated mocks have XXXMock func as a field (XXX is same as interface method name). In testing, you can freely rewrite behaviors by assigning func to this field.

func TestUserService_Find(t *testing.T) {
	m := mock.NewUserRepositoryMock()
	m.FindByIdMock = func(id int64) (*entity.User, error) {
		
		// mocking logic....
		
		return user, nil
	}
	
	service := NewUserService(m) // passing the mock
	
	if _, err := service.Find(id); err != nil {
		t.Error(err)
	}
}

Options

  • generate
$ dicon generate -h
NAME:
   dicon generate - generate dicon_gen file

USAGE:
   dicon generate [command options] [arguments...]

OPTIONS:
   --pkg value, -p value  target package(s).
   --out value, -o value  output file name (default: "dicon_gen")
   --dry-run
  • generate mock
$ dicon generate-mock -h
NAME:
   dicon generate-mock - generate dicon_mock file

USAGE:
   dicon generate-mock [command options] [arguments...]

OPTIONS:
   --pkg value, -p value   target package(s).
   --out value, -o value   output file name (default: "dicon_mock")
   --dist value, -d value  output package name (default: "mock")
   --dry-run

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

dicon's People

Contributors

akito0107 avatar gmidorii avatar orisano 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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dicon's Issues

add error to constructor return values

add error to constructor return values.

now:

func NewDependency() Dependency {
    return &dependency{}
}

expected:

func NewDependency() (Dependency, error) {
    return &dependency{}, nil
}

API Document

There are no api documentation...
Needs api documents.

generating mock method arguments is incorrect

generating mock method arguments is incorrect.

environment

go version go1.9.2 darwin/amd64
dicon revision: 81c5355

steps to reproduce

  1. setup
go get -u github.com/akito0107/dicon
cd $GOPATH/src/github.com/akito0107/dicon
git checkout 81c5355d6f1591b8cc4ed81deba7a58e4a2b4051
go build
  1. create files

test/container.go

package test

// +DICON
type Container interface {
	A() (A, error)
}

test/a.go

package test

type A interface {
	F(b, c int) int
}

type a struct{}

func (a *a) F(b, c int) int {
	return 0
}

func NewA() (A, error) {
	return &a{}, nil
}
  1. generate mock
./dicon generate-mock --pkg test

output

expected

mock/dicon_mock.go

// Code generated by "dicon"; DO NOT EDIT.

package mock

type AMock struct {
	FMock func(a0, a1 int) int
}

func NewAMock() *AMock {
	return &AMock{}
}

func (mk *AMock) F(a0, a1 int) int {
	return mk.FMock(a0, a1)
}

actual

mock/dicon_mock.go

// Code generated by "dicon"; DO NOT EDIT.

package mock

type AMock struct {
	FMock func(a0 int) int
}

func NewAMock() *AMock {
	return &AMock{}
}

func (mk *AMock) F(a0 int) int {
	return mk.FMock(a0)
}

mock can't compile when mock method return values is empty

generating mock code can't compile when mock method return values is empty.

environment

go version go1.9.2 darwin/amd64
dicon revision: 81c5355

steps to reproduce

  1. setup
go get -u github.com/akito0107/dicon
cd $GOPATH/src/github.com/akito0107/dicon
git checkout 81c5355d6f1591b8cc4ed81deba7a58e4a2b4051
go build
  1. create files

test/container.go

package test

// +DICON
type Container interface {
	A() (A, error)
}

test/a.go

package test

type A interface {
	F()
}

type a struct{}

func (a *a) F() {}

func NewA() (A, error) {
	return &a{}, nil
}
  1. generate mock
./dicon generate-mock --pkg test

output

expected

mock/dicon_mock.go

// Code generated by "dicon"; DO NOT EDIT.

package mock

type AMock struct {
	FMock func()
}

func NewAMock() *AMock {
	return &AMock{}
}

func (mk *AMock) F() {
	mk.FMock()
}

actual

mock/dicon_mock.go

// Code generated by "dicon"; DO NOT EDIT.

package mock

type AMock struct {
	FMock func()
}

func NewAMock() *AMock {
	return &AMock{}
}

func (mk *AMock) F() {
	return mk.FMock()
}

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.