From 4717c24bec9e733b5c1df3479e6c602d51b24f6b Mon Sep 17 00:00:00 2001 From: Hector Jusforgues Date: Sun, 10 Apr 2016 21:45:44 +0700 Subject: [PATCH] Add an example, and name some confusing parameters --- README.md | 2 +- inject.go | 2 +- inject_example_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++ update_readme.sh | 0 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 inject_example_test.go mode change 100644 => 100755 update_readme.sh diff --git a/README.md b/README.md index 679abe0..99a3542 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/inject.go b/inject.go index 3ff713c..c37e065 100644 --- a/inject.go +++ b/inject.go @@ -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. diff --git a/inject_example_test.go b/inject_example_test.go new file mode 100644 index 0000000..159c6ce --- /dev/null +++ b/inject_example_test.go @@ -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()) +} diff --git a/update_readme.sh b/update_readme.sh old mode 100644 new mode 100755