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

Add an example, and name some confusing parameters #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type TypeMapper interface {
// Maps the interface{} value based on the pointer of an Interface provided.
// This is really only useful for mapping a value as an interface, as interfaces
// cannot at this time be referenced directly without a pointer.
MapTo(interface{}, interface{}) TypeMapper
MapTo(value interface{}, pointerToInterface interface{}) TypeMapper
// Provides a possibility to directly insert a mapping based on type and value.
// This makes it possible to directly map type arguments not possible to instantiate
// with reflect like unidirectional channels.
Expand Down
2 changes: 1 addition & 1 deletion inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type TypeMapper interface {
// Maps the interface{} value based on the pointer of an Interface provided.
// This is really only useful for mapping a value as an interface, as interfaces
// cannot at this time be referenced directly without a pointer.
MapTo(interface{}, interface{}) TypeMapper
MapTo(value interface{}, pointerToInterface interface{}) TypeMapper
// Provides a possibility to directly insert a mapping based on type and value.
// This makes it possible to directly map type arguments not possible to instantiate
// with reflect like unidirectional channels.
Expand Down
79 changes: 79 additions & 0 deletions inject_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package inject_test

import (
"fmt"
"reflect"

"github.com/codegangsta/inject"
)

func ExampleInjector() {
// Create a new injector
injector := inject.New()

// Instantiate some dependency
s := StructImplementingDependencyInterface{
ID: 1,
}
s2 := StructImplementingDependencyInterface{
ID: 2,
}

// Map your instantiated dependency to its interface type.
// "(*DependencyInterface)(nil)" is a trick to get a pointer to an interface.
injector.MapTo(s, (*DependencyInterface)(nil))
// If you don't use interfaces, you can use Map instead of MapTo.
injector.Map(s2)

// Instantiate a struct needing dependencies, and do the injection.
receiver := StructReceivingDependencies{}
if err := injector.Apply(&receiver); err != nil {
panic(err)
}

// As you can see in the output,
// the interface field received the struct we mapped to the interface type,
// while the struct field received the one we mapped to its actual type.
fmt.Printf("interface field: %d\n", receiver.DependencyInterfaceField.GetID())
fmt.Printf("struct field: %d\n", receiver.DependencyStructField.GetID())

// Can also be used for functions.
injector.Invoke(Print)

// And finally, you can get the dependencies by using Get.
structWeGot := injector.Get(reflect.TypeOf(StructImplementingDependencyInterface{})).Interface().(StructImplementingDependencyInterface)
interfaceWeGot := injector.Get(inject.InterfaceOf((*DependencyInterface)(nil))).Interface().(DependencyInterface)

fmt.Printf("interface we got: %d\n", interfaceWeGot.GetID())
fmt.Printf("struct we got: %d\n", structWeGot.GetID())

// output:
// interface field: 1
// struct field: 2
// interface parameter: 1
// struct parameter: 2
// interface we got: 1
// struct we got: 2
}

type DependencyInterface interface {
GetID() int
}

type StructImplementingDependencyInterface struct {
ID int
}

func (s StructImplementingDependencyInterface) GetID() int {
return s.ID
}

type StructReceivingDependencies struct {
DependencyInterfaceField DependencyInterface `inject:"-"`
DependencyStructField StructImplementingDependencyInterface `inject:"-"`
}

func Print(d1 DependencyInterface, d2 StructImplementingDependencyInterface) {
fmt.Printf("interface parameter: %d\n", d1.GetID())
fmt.Printf("struct parameter: %d\n", d2.GetID())
}
Empty file modified update_readme.sh
100644 → 100755
Empty file.