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

Hide reflect.Value/Type from interfaces #7

Open
lukescott opened this issue Dec 9, 2013 · 1 comment
Open

Hide reflect.Value/Type from interfaces #7

lukescott opened this issue Dec 9, 2013 · 1 comment

Comments

@lukescott
Copy link

When using Invoke() or Get() you have to do a lot of Interface() and type assertions. It may be useful to change some of the methods to use plain interfaces{}, similar to how encoding/json works.

I was thinking Get would look something like this:

func (i *injector) Get(dst interface{}) {
    dval := reflect.ValueOf(dst).Elem()
    val := i.values[dval.Type()]
    if val.IsValid() {
        dval.Set(val)
    } else if i.parent != nil {
        i.parent.Get(dst)
    }
}

func (i *injector) GetInterface(dst interface{}, ifacePtr interface{}) {
    val := i.values[InterfaceOf(ifacePtr)]
    if val.IsValid() {
        reflect.ValueOf(dst).Elem().Set(val)
    } else if i.parent != nil {
        i.parent.GetInterface(dst, ifacePtr)
    }
}

These functions would be used like this:

obj := Obj{}

i.Get(&obj)
// OR
i.GetInterface(&obj, (*MyInterface)(nil))

Verses this:

ov := i.Get(Obj{})
// OR
ov := i.Get(InterfaceOf((*MyInterface)(nil)))

obj = ov.Interface().(Obj)

You could probably optimize this further by doing reflect.ValueOf/TypeOf once in the public methods and pass those to private methods that understand reflect.Value/Type.

Invoke could return []interface{} instead of []reflect.Value.

@codegangsta
Copy link
Owner

Yup. I agree with this that we can optimize the API to be cleaner for Getting values out of the type map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants