MCPcopy Create free account
hub / github.com/devfeel/dotweb / Bind

Method Bind

example/bind/main.go:109–136  ·  view source on GitHub ↗

Bind decode req.Body or form-value to struct

(i interface{}, ctx dotweb.Context)

Source from the content-addressed store, hash-verified

107
108//Bind decode req.Body or form-value to struct
109func (b *userBinder) Bind(i interface{}, ctx dotweb.Context) (err error) {
110 fmt.Println("UserBind.Bind")
111 req := ctx.Request()
112 ctype := req.Header.Get(dotweb.HeaderContentType)
113 if req.Body == nil {
114 err = errors.New("request body can't be empty")
115 return err
116 }
117 err = errors.New("request unsupported MediaType -> " + ctype)
118 switch {
119 case strings.HasPrefix(ctype, dotweb.MIMEApplicationJSON):
120 err = json.Unmarshal(ctx.Request().PostBody(), i)
121 case strings.HasPrefix(ctype, dotweb.MIMEApplicationXML):
122 err = xml.Unmarshal(ctx.Request().PostBody(), i)
123 //case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm),
124 // strings.HasPrefix(ctype, MIMETextHTML):
125 // err = reflects.ConvertMapToStruct(defaultTagName, i, ctx.FormValues())
126 default:
127 //check is use json tag, fixed for issue #91
128 tagName := "form"
129 if ctx.HttpServer().ServerConfig().EnabledBindUseJsonTag {
130 tagName = "json"
131 }
132 //no check content type for fixed issue #6
133 err = reflects.ConvertMapToStruct(tagName, i, ctx.Request().FormValues())
134 }
135 return err
136}
137
138//BindJsonBody default use json decode req.Body to struct
139func (b *userBinder) BindJsonBody(i interface{}, ctx dotweb.Context) (err error) {

Callers

nothing calls this directly

Calls 7

ConvertMapToStructFunction · 0.92
PostBodyMethod · 0.80
ServerConfigMethod · 0.80
FormValuesMethod · 0.80
RequestMethod · 0.65
GetMethod · 0.65
HttpServerMethod · 0.65

Tested by

no test coverage detected