[英]Unmarshal json to reflected struct (continued)
I want to write a gin middleware handler which gets data from c.Request.FormValue("data")
, unmarshalls it into a structure (structures are rather different) and sets a variable in context ( c.Set("Data",newP)
).我想编写一个 gin 中间件处理程序,它从
c.Request.FormValue("data")
获取数据,将其解组为一个结构(结构相当不同)并在上下文中设置一个变量( c.Set("Data",newP)
)。 So I searched and wrote this:所以我搜索并写了这个:
package middleware
import (
"reflect"
"fmt"
"github.com/gin-gonic/gin"
"encoding/json"
)
//https://semaphoreci.com/community/tutorials/test-driven-development-of-go-web-applications-with-gin
//https://github.com/gin-gonic/gin/issues/420
func Data(t reflect.Type) gin.HandlerFunc {
return func(c *gin.Context) {
//https://stackoverflow.com/a/7855298/5257901
//https://stackoverflow.com/a/45680060/5257901
//t := reflect.TypeOf(orig)
v := reflect.New(t.Elem())
// reflected pointer
newP := v.Interface()
data:=c.Request.FormValue("data")
fmt.Printf("%s data:%s\n",c.Request.URL.Path,data)
if err:=json.Unmarshal([]byte(data),newP); err!=nil{
fmt.Printf("%s data unmarshall %s, data(in quotes):\"%s\"",c.Request.URL.Path,err,data)
c.Abort()
return
}
ustr, _:=json.Marshal(newP)
fmt.Printf("%s unmarshalled:%s\n",c.Request.URL.Path,ustr)
c.Set("Data",newP)
c.Next()
}
}
and I use it like this:我像这样使用它:
func InitHandle(R *gin.Engine) {
Plan := R.Group("/Plan")
Plan.POST("/clickCreate",middleware.Data(reflect.TypeOf(new(tls.PlanTabel))), clickCreatePlanHandle)
}
and和
var data = *(c.MustGet("Data").(*tls.PlanTabel))
which is rather heavy and ugly.这是相当沉重和丑陋的。 I want to
我想
middleware.Data(tls.PlanTabel{})
and和
var data = c.MustGet("Data").(tls.PlanTabel)
In other words , omitting gin, I want a closure that eats i interface{}
and returns a function (data string) (o interface{})
换句话说,省略 gin,我想要一个吃
i interface{}
并返回一个函数(data string) (o interface{})
的闭包
func Data(i interface{}) (func (string) (interface{})) {
//some reflect magic goes here
//extract the structure type from interface{} :
//gets a reflect type pointer to it, like
//t := reflect.TypeOf(orig)
return func(data string) (o interface{}) {
//new reflected structure (pointer?)
v := reflect.New(t.Elem())
//interface to it
newP := v.Interface()
//unmarshal
json.Unmarshal([]byte(data),newP);
//get the structure from the pointer back
//returns interface to the structure
//reflect magic ends
}
}
The code in the question is close.问题中的代码很接近。
Try the following function.试试下面的功能。 The resulting function returns the same type as the type of
i
:结果函数返回与
i
的类型相同的类型:
func Data(i interface{}) func(string) (interface{}, error) {
return func(data string) (interface{}, error) {
v := reflect.New(reflect.TypeOf(i))
err := json.Unmarshal([]byte(data), v.Interface())
return v.Elem().Interface(), err
}
}
Example use:使用示例:
type Test struct {
A string
B string
}
f := Data((*Test)(nil))
v, err := f(`{"A": "hello", "B": "world"}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", v) // v is a *Test
f = Data("")
v, err = f(`"Hello"`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", v) // v is a string
If you want to return a struct value, then pass a struct value as the argument to Data:如果要返回结构值,则将结构值作为参数传递给 Data:
f = Data(Test{})
v, err = f(`{"A": "hello", "B": "world"}`)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", v) // v is a Test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.