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

Error is returned when passing a pointer to an interface variable to decode #32

Closed
oveddan opened this issue Mar 23, 2015 · 6 comments
Closed
Labels

Comments

@oveddan
Copy link

oveddan commented Mar 23, 2015

When calling decode, if you pass a pointer to an interface variable, you get an error:

schema: interface must be a pointer to struct

Here is code to reproduce:

import (
    "testing"
    "github.com/gorilla/schema"
)

type Cat struct {
    name string
}

type SomethingGeneric struct {
    getSomething func() interface{}
}

func getSomeCat() interface{} {
    return Cat{}
}

var somethingForCats = SomethingGeneric{getSomething: getSomeCat}

var decoder = schema.NewDecoder()

func TestCanDecodeInterfacePointer(t *testing.T) {
    dst := somethingForCats.getSomething()

    values := map[string][]string{
        "name": {"Tommy"},
    }

    err := decoder.Decode(&dst, values)

    if err != nil {
        t.Errorf("Could not decode.  Got error %v", err.Error())
    }

}

To avoid some boilerplate code, I need to be able to have some reusable code that can pass an interface variable pointer to decode.

@oveddan oveddan changed the title Get error when passing a pointer to an interface to decode Error is returned when passing a pointer to an interface variable to decode Mar 23, 2015
@spirozh
Copy link
Contributor

spirozh commented Apr 7, 2015

The test checks for err set to nil, but there is no test for correct decoding, so I was able to quickly pass this test and the rest of the existing test suite while still not populating the Cat object. It would be very helpful to have a complete test for the functionality that you desire (need).

When you use schema, you should be aware that returning Cat{} from getSomeCat will ensure the immutability of all the fields. For it to be mutable, it must return &Cat{}. Also, lowercase named fields, such as name, are considered immutable by reflect. See http://blog.golang.org/laws-of-reflection for more details.

http://play.golang.org/p/vxXM9c2m68 is a simple demonstration of mutability and immutability in structs passed by interface and accessed through reflection.

@themihai
Copy link

@spirozh I think you are a bit off the track (unless I'm wrong). The issue is that Schema doesn't decode into &interface{} . Reflect can see through interface{} and also set it if it's a pointer there. See below an example with both json which works and schema which fails with the error mentioned by OP.
Btw I think the label should be changed to Bug so that it can be tracked accordingly.

package main

import "encoding/json"
import "fmt"
import "strings"
import "github.com/gorilla/schema"
func main() {
        dst := struct{ Name string }{}
        dsti := interface{}(dst)

        values := `{"name": "Tommy"}`

        err := json.NewDecoder(strings.NewReader(values)).Decode(&dsti)

        if err != nil {
                panic(err)
        }
        fmt.Printf("decoded %v", dsti)
        valuesM := map[string][]string{
        "name": {"Tommy"},
        }
        if err :=  schema.NewDecoder().Decode(&dsti,valuesM); err !=nil{
                panic(err)
        }
        fmt.Printf("decoded with schmea %v", dsti)

}

@elithrar elithrar removed the question label Mar 20, 2017
@xeoncross
Copy link

xeoncross commented Jan 18, 2018

Here is a simpler version of the problem:

package main

import (
	"fmt"
	"log"

	"github.com/gorilla/schema"
)

type post struct {
	Title string
}

func main() {
	validateStruct(new(post))
}

func validateStruct(s interface{}) {
	decoder := schema.NewDecoder()
	err := decoder.Decode(&s, map[string][]string{"Title": {"One"}})
	if err != nil {
		log.Fatal(err) // schema: interface must be a pointer to struct
	}
}

Update

My mistake. I just read the source and realized that my problem was that I should be passing the value, not the pointer:

err := decoder.Decode(s, map[string][]string{"Title": {"One"}})

Note for search engine users: The original error I was having personally, was do to how I was passing the struct: new(post) vs post{}

@stale
Copy link

stale bot commented Dec 9, 2018

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label Dec 9, 2018
@stale stale bot closed this as completed Dec 17, 2018
@hanrok
Copy link

hanrok commented Sep 9, 2019

still happens

@xeoncross
Copy link

@hanrok please provide more information if the existing answers do not solve your problem. Please provide code showing the issue.

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

No branches or pull requests

6 participants