Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1.x #28

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="left">
English&nbsp |&nbsp <a href="README_CN.md">中文</a>
English&nbsp |&nbsp <a href="https://github.com/gone-io/gone/blob/main/README_CN.md">中文</a>
</p>

[![license](https://img.shields.io/badge/license-GPL%20V3-blue)](LICENSE)
Expand All @@ -14,6 +14,9 @@


- [Gone](#gone)
- [What Is Gone?](#what-is-gone)
- [Features](#features)
- [Dependency Injection and Startup](#dependency-injection-and-startup)
- [🌐Web Service](#web-service)
- [💡Concepts](#concepts)
- [🌰 More Examples:](#-more-examples)
Expand All @@ -22,9 +25,86 @@


# Gone
## What Is Gone?

Gone is a lightweight golang dependency injection framework; a series of Goners components are built in for rapid development of micro services.

![img.png](docs/assert/plan.png)
## Features
- Define the Goner interface to abstract dependencies.
- Dependency Injection:
- Inject Goners.
- Inject function arguments.
- Modular, detachable design.
- Startup process control.
- Testing support.
- Built-in components:
- goner/config, supports dependency injection of configuration parameters.
- goner/tracer, adds TraceId to call chains, supports link tracing.
- goner/logrus, goner/zap, supports log recording.
- goner/gin, integrates with the gin framework to provide HTTP request parameter dependency injection.
- goner/viper, used to parse various configuration files.
- ...

## Dependency Injection and Startup
Here's an example:
```go
package main

import (
"fmt"
"github.com/gone-io/gone"
)

type Worker struct {
gone.Flag // Anonymously embedding gone.Flag structure makes it a Goner, it can be injected as a dependency into other Goners or receive injections from other Goners.
Name string
}

func (w *Worker) Work() {
fmt.Printf("I am %s, and I am working\n", w.Name)
}

type Manager struct {
gone.Flag // Anonymously embedding gone.Flag structure makes it a Goner, it can be injected as a dependency into other Goners or receive injections from other Goners.
*Worker `gone:"manager-worker"` // Named injection GonerId="manager-worker" for Worker instance.
workers []*Worker `gone:"*"` // Inject all Workers into an array.
}

First and foremost, Gone is a lightweight, Golang-based dependency injection framework inspired by the Spring Framework in Java. Secondly, the Gone framework includes a series of built-in components that provide a complete set of web development solutions through these components, offering services configuration, logging, tracing, service calls, database access, message middleware, and other commonly used microservice capabilities.
func (m *Manager) Manage() {
fmt.Printf("I am %s, and I am managing\n", m.Name)
for _, worker := range m.workers {
worker.Work()
}
}

func main() {
managerRole := &Manager{}

managerWorker := &Worker{Name: "Scott"}
ordinaryWorker1 := &Worker{Name: "Alice"}
ordinaryWorker2 := &Worker{Name: "Bob"}

gone.
Prepare(func(cemetery gone.Cemetery) error {
cemetery.
Bury(managerRole).
Bury(managerWorker, gone.GonerId("manager-worker")).
Bury(ordinaryWorker1).
Bury(ordinary, ordinaryWorker2)
return nil
}).
// Run method supports dependency injection of parameters in its function.
Run(func(manager *Manager) {
manager.Manage()
})
}
```
Summary:
1. In the Gone framework, dependencies are abstracted as Goners, which can be injected into each other.
2. By anonymously embedding the gone.Flag, the structure implements the Goner interface.
3. Before starting, load all Goners into the framework using the Bury function.
4. Use the Run method to start, where the function supports dependency injection of parameters.

[Full Documentation](https://goner.fun/)

Expand Down
84 changes: 81 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,99 @@
<img src="docs/assert/logo.png" width = "150" alt="logo" align=center />

- [Gone](#gone)
- [Gone 是什么?](#gone-是什么)
- [特性](#特性)
- [依赖注入与启动](#依赖注入与启动)
- [🌐Web服务](#web服务)
- [💡概念](#概念)
- [🌰 更多例子:](#-更多例子)
- [🪜🧰🛠️ 组件库(👉🏻 更多组件正在开发中...,💪🏻 ヾ(◍°∇°◍)ノ゙,🖖🏻)](#️-组件库-更多组件正在开发中-ヾノ゙)
- [📚完整文档](#完整文档)


# Gone
## Gone 是什么?
Gone 是一个轻量级的golang依赖注入框架;内置了一系列Goners组件用于快速开发微服务。

![img.png](docs/assert/plan.png)

## 特性
- 定义Goner接口,对依赖进行抽象
- 依赖注入
- 注入Goners
- 注入函数参数
- 模块化,可拆卸设计
- 启动流程控制
- 测试支持
- 内置组件
- goner/config,支持配置参数的依赖注入
- goner/tracer,给调用链路增加TraceId,支持链路追踪
- goner/logrus、goner/zap,支持日志记录
- goner/gin,集成gin框架,提供HTTP请求参数的依赖注入
- goner/viper,用于解析多种配置文件
- ...

## 依赖注入与启动
看一个例子:
```go
package main

import (
"fmt"
"github.com/gone-io/gone"
)

type Worker struct {
gone.Flag //匿名嵌入了 gone.Flag的结构体就是一个 Goner,可以被作为依赖注入到其他Goner,或者接收其他 Goner 的注入
Name string
}

Gone首先是一个轻量的,基于Golang的,依赖注入框架,灵感来源于Java中的Spring Framework;其次,Gone框架中包含了一系列内置组件,通过这些组件提供一整套Web开发方案,提供服务配置、日志追踪、服务调用、数据库访问、消息中间件等微服务常用能力。
func (w *Worker) Work() {
fmt.Printf("I am %s, and I am working\n", w.Name)
}

type Manager struct {
gone.Flag //匿名嵌入了 gone.Flag的结构体就是一个 Goner,可以被作为依赖注入到其他Goner,或者接收其他 Goner 的注入
*Worker `gone:"manager-worker"` //具名注入 GonerId="manager-worker" 的 Worker 实例
workers []*Worker `gone:"*"` //将所有Worker注入到一个数组
}

func (m *Manager) Manage() {
fmt.Printf("I am %s, and I am managing\n", m.Name)
for _, worker := range m.workers {
worker.Work()
}
}

func main() {
managerRole := &Manager{}

managerWorker := &Worker{Name: "Scott"}
ordinaryWorker1 := &Worker{Name: "Alice"}
ordinaryWorker2 := &Worker{Name: "Bob"}

gone.
Prepare(func(cemetery gone.Cemetery) error {
cemetery.
Bury(managerRole).
Bury(managerWorker, gone.GonerId("manager-worker")).
Bury(ordinaryWorker1).
Bury(ordinaryWorker2)
return nil
}).
//Run方法中的函数支持参数的依赖注入
Run(func(manager *Manager) {
manager.Manage()
})
}
```
总结:
1. 在Gone框架中,依赖被抽象为 Goner,Goner 之间可以互相注入
2. 在结构体中匿名嵌入 gone.Flag,结构体就实现了 Goner接口
3. 在启动前,将所有 Goners 通过 Bury函数加载到框架中
4. 使用Run方法启动,Run方法中的函数支持参数的依赖注入

[完整文档](https://goner.fun/zh/)

下面使用Gone来编写一个Web服务吧!

## 🌐Web服务
```go
Expand Down
Binary file added docs/assert/plan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions example/test/goner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ func Test_Line(t *testing.T) {
return cemetery.ReplaceBury(Mock(), gone.GonerId(pointNameA))
})
})

t.Run("Prepare.Test", func(t *testing.T) {
gone.
Prepare(Priest).
Test(func(
line *Line, //注入gone框架中注册的类型

in struct { //注入匿名结构体
point *Point `gone:"example-test-point-a"`
},
) {
assert.Equal(t, line.A.Y, 200)
assert.Equal(t, in.point.Y, 200)
})
})
}
23 changes: 15 additions & 8 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,35 @@ type Tomb interface {
type Cemetery interface {
Goner

//Bury a Goner to the Cemetery
// Bury a Goner to the Cemetery
Bury(Goner, ...GonerOption) Cemetery

//BuryOnce a Goner to the Cemetery, if the Goner is already in the Cemetery, it will be ignored
// BuryOnce a Goner to the Cemetery, if the Goner is already in the Cemetery, it will be ignored
BuryOnce(goner Goner, options ...GonerOption) Cemetery

//ReplaceBury replace the Goner in the Cemetery with a new Goner
// ReplaceBury replace the Goner in the Cemetery with a new Goner
ReplaceBury(goner Goner, options ...GonerOption) error

//ReviveOne Revive a Goner from the Cemetery
// ReviveOne Revive a Goner from the Cemetery
ReviveOne(goner any) (deps []Tomb, err error)

//ReviveAllFromTombs Revive all Goner from the Cemetery
// ReviveAllFromTombs Revive all Goner from the Cemetery
ReviveAllFromTombs() error

//GetTomById return the Tomb by the GonerId
// GetTomById return the Tomb by the GonerId
GetTomById(GonerId) Tomb

//GetTomByType return the Tombs by the GonerType
// GetTomByType return the Tombs by the GonerType
GetTomByType(reflect.Type) []Tomb

InjectFuncParameters(fn any, injectBefore func(pt reflect.Type, i int) any, injectAfter func(pt reflect.Type, i int)) (args []reflect.Value, err error)
// InjectFuncParameters used for inject func parameters,which will construct parameters for a func;return constructed parameters reflect.Value Slice;
// InjectFuncParameters accept two hook functions: injectBefore and injectAfter,and hook function can be nil.
// The ith parameter will be ignored by injecting process if injectBefore(x, i) returned is not nil, and the result of injectBefore(x, i) will be added to args.
InjectFuncParameters(
fn any,
injectBefore func(pt reflect.Type, i int) any,
injectAfter func(pt reflect.Type, i int),
) (args []reflect.Value, err error)
}

// Priest A function which has A Cemetery parameter, and return an error. use for Burying Goner
Expand Down