GithubHelp home page GithubHelp logo

Comments (6)

spirozh avatar spirozh commented on June 25, 2024

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.

from schema.

themihai avatar themihai commented on June 25, 2024

@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)

}

from schema.

xeoncross avatar xeoncross commented on June 25, 2024

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{}

from schema.

stale avatar stale commented on June 25, 2024

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.

from schema.

hanrok avatar hanrok commented on June 25, 2024

still happens

from schema.

xeoncross avatar xeoncross commented on June 25, 2024

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

from schema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.