Comments (6)
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.
@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.
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.
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.
still happens
from schema.
@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)
- [bug] Required check bypassed if there is another field whose prefix is the field HOT 3
- [question] Unable to decode slice of structs HOT 1
- [feature] New release/tag HOT 1
- [feature] default values for scheme struct fields HOT 13
- [feature] Use json struct tags if wanted HOT 2
- [question] "+" symbol HOT 6
- ⚠️ The Gorilla Toolkit is Looking for a New Maintainer HOT 10
- [bug] Unstable decoding of different keys that are case-folded to the same value HOT 1
- Not checking for nil might lead to nil pointer dereference HOT 4
- [question] Error converting into []byte or []uint8 HOT 2
- [bug] schema: converter not found for Page, when the struct name is same with field name. HOT 6
- [question] Why can't Encoder check and call fmt.Stringer.String() on element automatically?
- [BUG] v1.2.0 Cannot decode into slice of strings HOT 2
- [bug] need to skip unexported field HOT 1
- [BUG] Nested struct with required field is not failing on decode HOT 1
- [FEATURE] Documentation HOT 4
- [Question] Is ignoring mismatched slice element types intended behavior? HOT 3
- [BUG] panic in 1.3.0 HOT 6
- [Question] Form encoding/decoding ignores slice of struct HOT 3
- [BUG] Slice encoding drops empty values rather than encoding them as `nil` or default
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schema.