Comments (15)
As it is now, my only complaint is that it's not clear that theese methods are dealing with JSON. Since XML is also supported, this can cause some confusion.
Maybe we should implement some kind of explicit model-binding functionality? The API could look something like this (brainstorming):
c.Bind(&item, binders.JSON/XML/Form/HTML/Whatever);
We could be smart and try to infer which binder to use by looking at the Content-Type
header. All binders could then have it's own validation mechanism (optional to use).
One nice thing about this is that we can also have property whitelist/blacklist functionality.
NancyFX[0] is a project I think implemented[1] model binding in a sane way.
I'll see if I have some time tonight, after the world cup games, to spike this out.
[0] https://github.com/NancyFx/Nancy/
[1] https://github.com/NancyFx/Nancy/wiki/Model-binding
from gin.
Good idea!
from gin.
I will implement something this afternoon.
from gin.
Can I spike something out on this or are you working on it?
from gin.
let me push what I have! I am working in wrapping http.ResponseWriter and better logging:
https://www.dropbox.com/s/n96ivj8hmlnu3zw/logging.png
from gin.
Check out this branch: https://github.com/gin-gonic/gin/tree/bindings
from gin.
there are still missing things for example, here https://github.com/gin-gonic/gin/blob/bindings/gin.go#L394-L398
That system would not work if the "Content-Type" includes additional flags like charset
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
from gin.
+1 for explicit
Why exactly should I wonder what I'm parsing? I'm writing the application, if I expect json, then I should get json and use binders.JSON
If I'm writing a service, and expect people to use either json or xml to communicate with it, then the API should indicate if they are sending a json or xml, e.g /something/something/json
or /something/something/xml
from gin.
Current develop
branch I am testing this API:
var msg Message
// Bind() picks the binding based in the Content-Type
if c.Bind(&msg) {
// do something with msg
}
and
var msg Message
if c.BindWith(&msg, binding.JSON) {
// do something with msg
}
binding.JSON, binding.XML and binding.Form supported
type Message struct {
Topic string `form:"topic" binding:"required"`
User string `form:"user" binding:"required"`
Text string `form:"text" binding:"required"`
}
// hit :/post?topic=Golang&user=Manu&text=Gin
func postMessage(c *gin.Context) {
var msg Message
if c.Bind(&msg) {
log.Println("Topic: " + Topic + " User:" + msg.User + " Text:"+msg.Text)
}
}
func main() {
r := gin.Default()
r.GET("/post", postMessage)
r.Run(":8080")
}
from gin.
Do you have any plans to extend the current binding to support more than requires? Only reason I ask is I am currently working on implementing a slightly more robust system that offers a few more validation options. By no means meant to validate everything but am looking to hit 90-95% of peoples use cases. If so I would love to work on integrating it into gin.
from gin.
I would love to implement a more powerful validation system, are you working around the Validation() method? How about regex validation for strings and range for numbers?
from gin.
Something like this would be awesome!
type MyStruct struct {
Name string `regex:"[a-zA-Z](2, 10)"`
Age int `min:"18"`
}
from gin.
In my current lib i have here https://github.com/michaeljs1990/val I actually switched the main call to Guaranty but that is essentially the equivalent of your Bind function with a few extra checks I have added in but it could be easily moved back over to using the Bind function to kick it off again. I am going to add in regex and min to my lib tonight as they are great ideas. I will put a pull request up for you when done to discuss some of the other specifics. Yes I am working with the Validation function currently though.
from gin.
Your library is even better! very cool. I also like the "validate" tag more than "binding".
I probably switch to validate, and but the support for "binding" would still exist in a deprecated way.
from gin.
Can't wait to see your PR! closing this issue.
from gin.
Related Issues (20)
- BasicAuth is not suitable for proxy auth
- Can't get status as expected from context.Status() HOT 1
- Gin Context implementation breaks context.Context contract HOT 4
- [Suggestions] Introducing an AI-powered robot to assist with daily issue and pull request (PR) tasks.
- Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK HOT 2
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
- http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) HOT 1
- Suggestion: Move Print Debug to Run() functions? HOT 7
- AbortWithStatusJSON doesn't abort context HOT 2
- I translated `tree.go` into java
- GetUint and GetUint64 wrong return HOT 1
- Multipart form-data request with empty key for single file upload
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 gin.