Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
molon committed Jun 21, 2024
0 parents commit b7debd8
Show file tree
Hide file tree
Showing 7 changed files with 901 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024-present The Plant

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
167 changes: 167 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# inject

The `inject` package is inspired by [codecangsta/inject](https://github.com/codegangsta/inject) and provides simplified public methods for dependency injection in Go.

## Features

- `Provide`: Used to provide dependencies through a function.
- `Invoke`: Used to resolve dependencies and invoke a function with them.
- `Resolve`: Used to resolve a dependency by its type.
- `Apply`: Used to apply dependencies to a struct.
- `SetParent`: Used to set the parent injector.
- Thread safety ensured
- Supports injection through the `inject` tag of struct fields

## Usage

Here's an example of how to use the `inject` package:

```go
package inject_test

import (
"fmt"

"github.com/theplant/inject"
)

// Define interfaces and implementations
type Printer interface {
Print() string
}

type SimplePrinter struct{}

func (p *SimplePrinter) Print() string {
return "Printing document"
}

// New type definition
type DocumentDescription string

type Document struct {
Injector *inject.Injector `inject:""` // Injector will be provided by default, so you can also get it if needed

ID string // Not injected
Description DocumentDescription `inject:""` // Exported non-optional field
Printer Printer `inject:""` // Exported non-optional field
Size int64 `inject:"optional"` // Exported optional field
page int `inject:""` // Unexported non-optional field
name string `inject:"optional"` // Unexported optional field
}

func ExampleInjector() {
inj := inject.New()

// Provide dependencies
if err := inj.Provide(
func() Printer {
return &SimplePrinter{}
},
func() string {
return "A simple string"
},
func() DocumentDescription {
return "A document description"
},
func() int {
return 42
},
); err != nil {
panic(err)
}

{
// Resolve dependencies
var printer Printer
if err := inj.Resolve(&printer); err != nil {
panic(err)
}
fmt.Println("Resolved printer:", printer.Print())
}

printDoc := func(doc *Document) {
fmt.Printf("Document id: %q\n", doc.ID)
fmt.Printf("Document description: %q\n", doc.Description)
fmt.Printf("Document printer: %q\n", doc.Printer.Print())
fmt.Printf("Document size: %d\n", doc.Size)
fmt.Printf("Document page: %d\n", doc.page)
fmt.Printf("Document name: %q\n", doc.name)
}

fmt.Println("-------")

{
// Invoke a function
results, err := inj.Invoke(func(printer Printer) *Document {
return &Document{
// This value will be retained as it is not tagged with `inject`, despite string being provided
ID: "idInvoked",
// This value will be overridden since it is tagged with `inject` and DocumentDescription is provided
Description: "DescriptionInvoked",
// This value will be overridden with the same value since it is tagged with `inject` and Printer is provided
Printer: printer,
// This value will be retained since it is tagged with `inject:"optional"` and int64 is not provided
Size: 100,
}
})
if err != nil {
panic(err)
}

printDoc(results[0].(*Document))
}

fmt.Println("-------")

{
// Apply dependencies to a struct instance
doc := &Document{}
if err := inj.Apply(doc); err != nil {
panic(err)
}

printDoc(doc)
}

fmt.Println("-------")

{
// Create a child injector and then apply dependencies to a struct instance
child := inject.New()
child.SetParent(inj)

doc := &Document{}
if err := child.Apply(doc); err != nil {
panic(err)
}

printDoc(doc)
}

// Output:
// Resolved printer: Printing document
// -------
// Document id: "idInvoked"
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 100
// Document page: 42
// Document name: "A simple string"
// -------
// Document id: ""
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 0
// Document page: 42
// Document name: "A simple string"
// -------
// Document id: ""
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 0
// Document page: 42
// Document name: "A simple string"
}

```
146 changes: 146 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package inject_test

import (
"fmt"

"github.com/theplant/inject"
)

// Define interfaces and implementations
type Printer interface {
Print() string
}

type SimplePrinter struct{}

func (p *SimplePrinter) Print() string {
return "Printing document"
}

// New type definition
type DocumentDescription string

type Document struct {
Injector *inject.Injector `inject:""` // Injector will be provided by default, so you can also get it if needed

ID string // Not injected
Description DocumentDescription `inject:""` // Exported non-optional field
Printer Printer `inject:""` // Exported non-optional field
Size int64 `inject:"optional"` // Exported optional field
page int `inject:""` // Unexported non-optional field
name string `inject:"optional"` // Unexported optional field
}

func ExampleInjector() {
inj := inject.New()

// Provide dependencies
if err := inj.Provide(
func() Printer {
return &SimplePrinter{}
},
func() string {
return "A simple string"
},
func() DocumentDescription {
return "A document description"
},
func() int {
return 42
},
); err != nil {
panic(err)
}

{
// Resolve dependencies
var printer Printer
if err := inj.Resolve(&printer); err != nil {
panic(err)
}
fmt.Println("Resolved printer:", printer.Print())
}

printDoc := func(doc *Document) {
fmt.Printf("Document id: %q\n", doc.ID)
fmt.Printf("Document description: %q\n", doc.Description)
fmt.Printf("Document printer: %q\n", doc.Printer.Print())
fmt.Printf("Document size: %d\n", doc.Size)
fmt.Printf("Document page: %d\n", doc.page)
fmt.Printf("Document name: %q\n", doc.name)
}

fmt.Println("-------")

{
// Invoke a function
results, err := inj.Invoke(func(printer Printer) *Document {
return &Document{
// This value will be retained as it is not tagged with `inject`, despite string being provided
ID: "idInvoked",
// This value will be overridden since it is tagged with `inject` and DocumentDescription is provided
Description: "DescriptionInvoked",
// This value will be overridden with the same value since it is tagged with `inject` and Printer is provided
Printer: printer,
// This value will be retained since it is tagged with `inject:"optional"` and int64 is not provided
Size: 100,
}
})
if err != nil {
panic(err)
}

printDoc(results[0].(*Document))
}

fmt.Println("-------")

{
// Apply dependencies to a struct instance
doc := &Document{}
if err := inj.Apply(doc); err != nil {
panic(err)
}

printDoc(doc)
}

fmt.Println("-------")

{
// Create a child injector and then apply dependencies to a struct instance
child := inject.New()
child.SetParent(inj)

doc := &Document{}
if err := child.Apply(doc); err != nil {
panic(err)
}

printDoc(doc)
}

// Output:
// Resolved printer: Printing document
// -------
// Document id: "idInvoked"
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 100
// Document page: 42
// Document name: "A simple string"
// -------
// Document id: ""
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 0
// Document page: 42
// Document name: "A simple string"
// -------
// Document id: ""
// Document description: "A document description"
// Document printer: "Printing document"
// Document size: 0
// Document page: 42
// Document name: "A simple string"
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/theplant/inject

go 1.22.3

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit b7debd8

Please sign in to comment.