Strongly typed language
Data binding
In strongly typed languages, binding parameters are divided into several types. Introduction to the most widely used Gin web framework
1. Bind and ShouldBind
Gin's Context provides two major methods for requesting data binding: prefixed by Bind and distinction with ShouldBind in naming. These two major categories of methods have some differences in behavior.
type | describe |
---|---|
must | Conditions must be met |
should | Conditions should be met |
Bind
The method will automaticallyhttp status
Set to 400. And no more information will be returned. Due to the existence, error handling is complex and has many restrictions (only common types are supported), the verification rules are simple, and there may be security risks. In daily development, it is almost not recommended to use bind to bind data.
The difference between these methods shouldBind and ShouldBindJSON is that the former will automatically determine which binder to be used based on the header header. If the requested Content-Type is agreed in the development specification within the team, it is more reasonable to directly choose the latter.
In actual development, ShouldBind automatically determines which binder is used most based on the header header. Because if the team's agreement is not seen by the new team, the code incompatibility is prone to occur.
var request struct { Email string `form:"email" binding:"required"` Name string `form:"name" binding:"required"` } // ShouldBind regular binding can also be compatible with post get json parametersif err := (&request); err != nil { (ctx, {}, err) return } // If Content-Type is all JSON, it is more reasonable to use the following paragraph for strongly typed languages. Generally speaking, the above one is still used.if err := (&request); err != nil { (ctx, {}, err) return }
2. Single binding and multiple binding request body data
message := ("message") ids := ("ids") message := ("message")
At this moment, in order to bind the interface parameters and middleware parameters, there will be a problem with secondary bound data. The editor was stunned for a long time and found that the strong type language almost distinguishes between types. Whether it is the json get post request type, each type is distinguished, and even the secondary binding is distinguished. It has to be said that the experience of writing code is sacrificed for performance. So the key is
Whether it is the binding method of Bind or ShouldBind class, it can only read the request body once for binding.
AvailableShouldBindBodyWith
To achieve secondary and multiple bindings. That question comes again, usedShouldBindBodyWith
Binding starts with a parameter being bound, and subsequent bindings must be used.ShouldBindBodyWith
To bind, only if there are such restrictions can multiple binding be achieved
if err := (&request,); err != nil { (ctx, {}, err) return }
Here I basically have a certain understanding of parameter binding. Different types need to be converted to each other, and different scenarios require different methods to implement them. Relative to weak languagePHP
For example, no matter what type, whether it is one or multiple bindings, it is also possible to accept all the same as the order. This processing also indirectly leads to weak-type languages that need to be compatible with various types in the underlying processing, and their performance requires more content space and consume more CPU resources compared to strong-type language compilation.
3. General type binding
In strongly typed languages, when receiving parameters, structures are commonly used to bind data types. Each structure defines a type. And the first letter must be capitalized.
type request struct { Email string `form:"email" binding:"required"` Name string `form:"name" binding:"required"` Age int `form:"age" binding:"required"` }
Then the question is, the format to receive data isjson
What to do with the data format? It seems that the data formats such as a single data string int cannot be met, and the nested structure needs to be as follows:
type Grouping struct { ID int `json:"id"` Remark string `json:"remark" default:""` } type Group struct { Test1 []*Grouping `json:"test1"` Test2 Grouping `json:"test2"` Test3 string `json:"test3"` Test4 int `json:"test4"` }
Then the problem comes again. When this type is fixed in json format, if the requirements change, the code must be updated and added with new structure fields. Adding a structure field requires recompilation. That does not receive a field value of a common type.
Nextinterface
Type comes in handy.interface
The type is a general type,MySQL
There is no such type in the database. Need tointerface
Only by converting types into types like json, string, int can you save them. Therefore, when receiving the general data parameter format, you have to use the MySQL third-party extension library to identify whether the general type is text string or json type
type UserGroupTask struct { Test1 interface{} `json:"test1" form:"test1" gorm:"type:'json'"` }
By signgorm
Identification,interface
Type isjson
. This wayshouldBindJSON
Only when it is JSON format can it be recognized.
Summarize
The above introduces strongly typed languages, which require conversion of field types, data binding type specification, etc., and are encapsulated at the bottom in weak typed languages.
Leave a question: The above code receives a general format. How can you take out the json format parameters for judgment and then enter the database with as little as possible, while writing as much as possible.
For more information about Golang PHP data binding, please follow my other related articles!