m90
m90

Reputation: 11822

How do i dereference a pointer value passed as the empty interface?

I've got a method taking a target interface{} on a type that I use for database access like:

func (c *client) Query(query someType, target interface{}) error {
    return c.db.Query(query).Decode(target)
}

This is then called like

result := resultType{}
if err := c.Query(myQuery, &result); err == nil {
    // do sth with result
}

Which does what I want it do as I am passing the pointer address of result

The trouble I am now running into is that I do not know how I can mock this kind of behavior (mutating the passed reference) in a test.

In case I wouldn't need to pass interface{} I could imagine it being done like this:

type mockClient struct {
    targetValue resultType
}

func (m *mockClient) Query(query someType, target *resultType) error {
    *target = m.targetValue
    return nil
}

If I try to do the same using my actual signature, I am not able to dereference the value contained in target like this:

type mockClient struct {
    targetValue interface{}
}

func (m *mockClient) Query(query someType, target interface{}) error {
    target = m.targetValue // this does not mutate the passed target
    return nil
} 

Can I dereference a pointer value when it is passed in as the empty interface? In case it is not possible, what would be another approach of testing the side effects my method has without having to resort to concrete types as arguments?

Upvotes: 9

Views: 7006

Answers (1)

Pavlo Strokov
Pavlo Strokov

Reputation: 2087

You can use 'reflect' package to do it.

package main

import (
    "fmt"
    "reflect"
)

type mockClient struct {}

func (m *mockClient) Query(query string, target interface{}) error {
    a := "changed"
    va := reflect.ValueOf(a)
    reflect.ValueOf(target).Elem().Set(va)
    return nil
}

func main() {
    var mc mockClient
    target := "initial"
    mc.Query("qwe", &target)
    fmt.Println(target)
}

The simple example to reference you can find here

Upvotes: 16

Related Questions