Permalink
Browse files

cleanup

  • Loading branch information...
peter-edge committed Oct 31, 2015
1 parent c02a1c0 commit bdaa71bbb29b8978a1b7c70df659cabee07f4f13
Showing with 82 additions and 143 deletions.
  1. +1 −3 binding.go
  2. +4 −13 builder.go
  3. +7 −21 common.go
  4. +0 −4 doc.go
  5. +0 −1 example/README.md
  6. +2 −2 example/api/api.go
  7. +1 −1 example/cloud/cloud.go
  8. 0 example/{main → cmd/inject-example}/main.go
  9. +3 −0 inject.go
  10. +52 −44 inject_error.go
  11. +0 −30 inject_test.go
  12. +7 −14 injector.go
  13. +5 −10 module.go
View
@@ -34,9 +34,7 @@ func (i *intermediateBinding) String() string {
func (i *intermediateBinding) resolvedBinding(module *module, injector *injector) (resolvedBinding, error) {
binding, ok := module.binding(i.bindingKey)
if !ok {
eb := newErrorBuilder(injectErrorTypeNoFinalBinding)
eb.addTag("bindingKey", i.bindingKey)
return nil, eb.build()
return nil, errNoFinalBinding.withTag("bindingKey", i.bindingKey)
}
return binding.resolvedBinding(module, injector)
}
View
@@ -80,18 +80,13 @@ func (b *baseBuilder) setBinding(bindingKey bindingKey, binding binding) {
func verifyBindingReflectType(bindingKeyReflectType reflect.Type, bindingReflectType reflect.Type) error {
if !isSupportedBindingKeyReflectType(bindingKeyReflectType) {
eb := newErrorBuilder(injectErrorTypeNotSupportedYet)
eb = eb.addTag("bindingKeyReflectType", bindingKeyReflectType)
return eb.build()
return errNotSupportedYet.withTag("bindingKeyReflectType", bindingReflectType)
}
if isInterfacePtr(bindingKeyReflectType) {
bindingKeyReflectType = bindingKeyReflectType.Elem()
}
if !bindingReflectType.AssignableTo(bindingKeyReflectType) {
eb := newErrorBuilder(injectErrorTypeNotAssignable)
eb = eb.addTag("bindingKeyReflectType", bindingKeyReflectType)
eb = eb.addTag("bindingReflectType", bindingReflectType)
return eb.build()
return errNotAssignable.withTag("bindingKeyReflectType", bindingKeyReflectType).withTag("bindingReflectType", bindingReflectType)
}
return nil
}
@@ -122,18 +117,14 @@ func verifyTaggedConstructorReflectType(bindingKeyReflectType reflect.Type, cons
func verifyConstructorReturnValues(bindingKeyReflectType reflect.Type, constructorReflectType reflect.Type) error {
if constructorReflectType.NumOut() != 2 {
eb := newErrorBuilder(injectErrorTypeConstructorReturnValuesInvalid)
eb = eb.addTag("constructorReflectType", constructorReflectType)
return eb.build()
return errConstructorReturnValuesInvalid.withTag("constructorReflectType", constructorReflectType)
}
err := verifyBindingReflectType(bindingKeyReflectType, constructorReflectType.Out(0))
if err != nil {
return err
}
if !constructorReflectType.Out(1).AssignableTo(errorReflectType) {
eb := newErrorBuilder(injectErrorTypeConstructorReturnValuesInvalid)
eb = eb.addTag("constructorReflectType", constructorReflectType)
return eb.build()
return errConstructorReturnValuesInvalid.withTag("constructorReflectType", constructorReflectType)
}
return nil
}
View
@@ -56,9 +56,7 @@ func isSupportedBindConstantReflectType(reflectType reflect.Type) bool {
func verifyIsFunc(funcReflectType reflect.Type) error {
if !isFunc(funcReflectType) {
eb := newErrorBuilder(injectErrorTypeNotFunction)
eb = eb.addTag("funcReflectType", funcReflectType)
return eb.build()
return errNotFunction.withTag("funcReflectType", funcReflectType)
}
numIn := funcReflectType.NumIn()
for i := 0; i < numIn; i++ {
@@ -76,25 +74,17 @@ func verifyIsFunc(funcReflectType reflect.Type) error {
func verifyIsTaggedFunc(funcReflectType reflect.Type) error {
if !isFunc(funcReflectType) {
eb := newErrorBuilder(injectErrorTypeNotFunction)
eb = eb.addTag("funcReflectType", funcReflectType)
return eb.build()
return errNotFunction.withTag("funcReflectType", funcReflectType)
}
if funcReflectType.NumIn() != 1 {
eb := newErrorBuilder(injectErrorTypeTaggedParametersInvalid)
eb = eb.addTag("funcReflectType", funcReflectType)
return eb.build()
return errTaggedParametersInvalid.withTag("funcReflectType", funcReflectType)
}
inReflectType := funcReflectType.In(0)
if !isStruct(inReflectType) {
eb := newErrorBuilder(injectErrorTypeTaggedParametersInvalid)
eb = eb.addTag("funcReflectType", funcReflectType)
return eb.build()
return errTaggedParametersInvalid.withTag("funcReflectType", funcReflectType)
}
if inReflectType.Name() != "" {
eb := newErrorBuilder(injectErrorTypeTaggedParametersInvalid)
eb = eb.addTag("funcReflectType", funcReflectType)
return eb.build()
return errTaggedParametersInvalid.withTag("funcReflectType", funcReflectType)
}
err := verifyStructCanBePopulated(inReflectType)
if err != nil {
@@ -117,14 +107,10 @@ func verifyStructCanBePopulated(structReflectType reflect.Type) error {
func verifyParameterCanBeInjected(parameterReflectType reflect.Type, tag string) error {
if tag == "" && !isSupportedNoTagParameterReflectType(parameterReflectType) {
eb := newErrorBuilder(injectErrorTypeNotSupportedYet)
eb.addTag("parameterReflectType", parameterReflectType)
return eb.build()
return errNotSupportedYet.withTag("parameterReflectType", parameterReflectType)
}
if tag != "" && !isSupportedBindingKeyReflectType(parameterReflectType) {
eb := newErrorBuilder(injectErrorTypeNotSupportedYet)
eb.addTag("parameterReflectType", parameterReflectType)
return eb.build()
return errNotSupportedYet.withTag("parameterReflectType", parameterReflectType)
}
return nil
}
View
4 doc.go

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.
View
@@ -36,7 +36,7 @@ type api struct {
func createApi(s struct {
AwsProvider cloud.Provider `inject:"aws"`
DigitalOceanProvider cloud.Provider `inject:"digitalOcean"`
DigitalOceanProvider cloud.Provider `inject:"digital_ocean"`
MoreThings more.MoreThings
}) (Api, error) {
return &api{s.AwsProvider, s.DigitalOceanProvider, s.MoreThings}, nil
@@ -66,7 +66,7 @@ func (a *api) getProvider(provider string) (cloud.Provider, error) {
switch provider {
case "aws":
return a.awsProvider, nil
case "digitalOcean":
case "digital_ocean":
return a.digitalOceanProvider, nil
default:
return nil, fmt.Errorf("api: Unknown provider %v", provider)
View
@@ -8,7 +8,7 @@ import (
func CreateModule() inject.Module {
module := inject.CreateModule()
module.BindTagged("aws", (*Provider)(nil)).ToSingletonConstructor(createAwsProvider)
module.BindTagged("digitalOcean", (*Provider)(nil)).ToSingletonConstructor(createDigitalOceanProvider)
module.BindTagged("digital_ocean", (*Provider)(nil)).ToSingletonConstructor(createDigitalOceanProvider)
return module
}
File renamed without changes.
View
@@ -1,3 +1,6 @@
/*
Package inject is guice-inspired dependency injection for Go.
*/
package inject // import "go.pedge.io/inject"
import (
View
@@ -6,7 +6,6 @@ import (
)
const (
injectErrorPrefix = "inject: "
injectErrorTypeNil = "Parameter is nil"
injectErrorTypeReflectTypeNil = "reflect.TypeOf() returns nil"
injectErrorTypeNotSupportedYet = "Binding type not supported yet, feel free to help!"
@@ -27,64 +26,73 @@ const (
injectErrorTypeBindingErrors = "Errors with bindings"
)
var (
errNil = newInjectError(injectErrorTypeNil)
errReflectTypeNil = newInjectError(injectErrorTypeReflectTypeNil)
errNotSupportedYet = newInjectError(injectErrorTypeNotSupportedYet)
errNotAssignable = newInjectError(injectErrorTypeNotAssignable)
errConstructorReturnValuesInvalid = newInjectError(injectErrorTypeConstructorReturnValuesInvalid)
errIntermediateBinding = newInjectError(injectErrorTypeIntermediateBinding)
errFinalBinding = newInjectError(injectErrorTypeFinalBinding)
errCannotCastModule = newInjectError(injectErrorTypeCannotCastModule)
errNoBinding = newInjectError(injectErrorTypeNoBinding)
errNoFinalBinding = newInjectError(injectErrorTypeNoFinalBinding)
errAlreadyBound = newInjectError(injectErrorTypeAlreadyBound)
errTagEmpty = newInjectError(injectErrorTypeTagEmpty)
errTaggedParametersInvalid = newInjectError(injectErrorTypeTaggedParametersInvalid)
errNotFunction = newInjectError(injectErrorTypeNotFunction)
errNotInterfacePtr = newInjectError(injectErrorTypeNotInterfacePtr)
errNotStructPtr = newInjectError(injectErrorTypeNotStructPtr)
errNotSupportedBindType = newInjectError(injectErrorTypeNotSupportedBindType)
errBindingErrors = newInjectError(injectErrorTypeBindingErrors)
)
type injectError struct {
errorType string
tags map[string]interface{}
// TODO(pedge): there has to be a better way to do this
tagOrder []string
tags injectErrorTags
}
func (i *injectError) Error() string {
value := fmt.Sprintf("%s%s", injectErrorPrefix, i.errorType)
tagStrings := i.tagStrings()
if len(tagStrings) > 0 {
value = fmt.Sprintf("%s tags{%s}", value, strings.Join(tagStrings, " "))
}
return value
func newInjectError(errorType string) *injectError {
return &injectError{errorType, make([]*injectErrorTag, 0)}
}
func (i *injectError) Type() string {
return i.errorType
func (i *injectError) Error() string {
value := fmt.Sprintf("inject: %s", i.errorType)
if len(i.tags) == 0 {
return value
}
return fmt.Sprintf("%s %s", value, i.tags.String())
}
func (i *injectError) GetTag(key string) (interface{}, bool) {
value, ok := i.tags[key]
return value, ok
func (i *injectError) withTag(key string, value interface{}) *injectError {
return &injectError{i.errorType, append(i.tags, newInjectErrorTag(key, value))}
}
func (i *injectError) tagStrings() []string {
strings := make([]string, len(i.tags))
ii := 0
for _, key := range i.tagOrder {
value := i.tags[key]
var valueString string
if stringer, ok := value.(fmt.Stringer); ok {
valueString = fmt.Sprintf("%v", stringer.String())
} else {
valueString = fmt.Sprintf("%v", value)
}
strings[ii] = fmt.Sprintf("%s:%s", key, valueString)
ii++
}
return strings
type injectErrorTag struct {
key string
value interface{}
}
type injectErrorBuilder struct {
errorType string
tags map[string]interface{}
tagOrder []string
func newInjectErrorTag(key string, value interface{}) *injectErrorTag {
return &injectErrorTag{key, value}
}
func newErrorBuilder(errorType string) *injectErrorBuilder {
return &injectErrorBuilder{errorType, make(map[string]interface{}), make([]string, 0)}
func (t *injectErrorTag) String() string {
if stringer, ok := t.value.(fmt.Stringer); ok {
return fmt.Sprintf("%s:%s", t.key, stringer.String())
}
return fmt.Sprintf("%s:%s", t.key, t.value)
}
func (i *injectErrorBuilder) addTag(key string, value interface{}) *injectErrorBuilder {
i.tags[key] = value
i.tagOrder = append(i.tagOrder, key)
return i
}
type injectErrorTags []*injectErrorTag
func (i *injectErrorBuilder) build() *injectError {
return &injectError{i.errorType, i.tags, i.tagOrder}
func (ts injectErrorTags) String() string {
if len(ts) == 0 {
return ""
}
s := make([]string, len(ts))
for i, tag := range ts {
s[i] = tag.String()
}
return fmt.Sprintf("tags{%s}", strings.Join(s, " "))
}
View
@@ -13,36 +13,6 @@ const (
goRoutineIterations = 100
)
// ***** error tests *****
func TestInjectErrorWithoutTags(t *testing.T) {
errorBuilder := newErrorBuilder("foo")
injectError := errorBuilder.build()
require.Equal(t, "inject: foo", injectError.Error())
require.Equal(t, "foo", injectError.Type())
_, ok := injectError.GetTag("tagOne")
require.False(t, ok)
}
func TestInjectErrorWithTags(t *testing.T) {
errorBuilder := newErrorBuilder("foo")
errorBuilder = errorBuilder.addTag("tagOne", 1)
errorBuilder = errorBuilder.addTag("tagTwo", "two")
injectError := errorBuilder.build()
// making sure that the order of the tags is the same and that this does not rely
// only on a map - this is a bad and non-deterministic way to do this but fix later
for i := 0; i < 100; i++ {
require.Equal(t, "inject: foo tags{tagOne:1 tagTwo:two}", injectError.Error())
}
require.Equal(t, "foo", injectError.Type())
tagOne, ok := injectError.GetTag("tagOne")
require.True(t, ok)
require.Equal(t, 1, tagOne)
tagTwo, ok := injectError.GetTag("tagTwo")
require.True(t, ok)
require.Equal(t, "two", tagTwo)
}
// ***** simple bind tests *****
type SimpleInterface interface {
View
@@ -16,7 +16,7 @@ func createInjector(modules []Module) (Injector, error) {
for _, m := range modules {
castModule, ok := m.(*module)
if !ok {
return nil, newErrorBuilder(injectErrorTypeCannotCastModule).build()
return nil, errCannotCastModule
}
err := installModuleToInjector(&injector, castModule)
if err != nil {
@@ -33,18 +33,15 @@ func createInjector(modules []Module) (Injector, error) {
func installModuleToInjector(injector *injector, module *module) error {
numBindingErrors := len(module.bindingErrors)
if numBindingErrors > 0 {
eb := newErrorBuilder(injectErrorTypeBindingErrors)
err := errBindingErrors
for i := 0; i < numBindingErrors; i++ {
eb.addTag(strconv.Itoa(i+1), module.bindingErrors[i].Error())
err = err.withTag(strconv.Itoa(i+1), module.bindingErrors[i].Error())
}
return eb.build()
return err
}
for bindingKey, binding := range module.bindings {
if foundBinding, ok := injector.bindings[bindingKey]; ok {
eb := newErrorBuilder(injectErrorTypeAlreadyBound)
eb.addTag("bindingKey", bindingKey)
eb.addTag("foundBinding", foundBinding)
return eb.build()
return errAlreadyBound.withTag("bindingKey", bindingKey).withTag("foundBinding", foundBinding)
}
resolvedBinding, err := binding.resolvedBinding(module, injector)
if err != nil {
@@ -294,9 +291,7 @@ func (i *injector) get(bindingKey bindingKey) (interface{}, error) {
func (i *injector) getBinding(bindingKey bindingKey) (resolvedBinding, error) {
binding, ok := i.bindings[bindingKey]
if !ok {
eb := newErrorBuilder(injectErrorTypeNoBinding)
eb.addTag("bindingKey", bindingKey)
return nil, eb.build()
return nil, errNoBinding.withTag("bindingKey", bindingKey)
}
return binding, nil
}
@@ -326,9 +321,7 @@ func (i *injector) validateBindingKeys(bindingKeys []bindingKey) error {
func verifyIsStructPtr(reflectType reflect.Type) error {
if !isStructPtr(reflectType) {
eb := newErrorBuilder(injectErrorTypeNotStructPtr)
eb.addTag("reflectType", reflectType)
return eb.build()
return errNotStructPtr.withTag("reflectType", reflectType)
}
return nil
}
Oops, something went wrong.

0 comments on commit bdaa71b

Please sign in to comment.