Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 925
m90 Avatar asked Mar 28 '18 17:03

m90


People also ask

How do you dereference a pointer variable?

The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer. When we dereference a pointer, then the value of the variable pointed by this pointer will be returned.

When you dereference a pointer to a pointer the result is?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

What is dereference a pointer Golang?

Dereferencing a pointer gives us access to the value the pointer points to. When we write *xPtr = 0 we are saying “store the int 0 in the memory location xPtr refers to”. If we try xPtr = 0 instead we will get a compiler error because xPtr is not an int it's a *int , which can only be given another *int .


1 Answers

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

like image 98
Pavlo Strokov Avatar answered Sep 28 '22 13:09

Pavlo Strokov