Skip to content

Commit f639c3a

Browse files
authored
Merge pull request #50 from nejisama/add_archived
add biding/netpoll/registry
2 parents 6e8f5a0 + b139979 commit f639c3a

1,091 files changed

Lines changed: 348060 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

binding/binding.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !nomsgpack
6+
7+
package binding
8+
9+
import "net/http"
10+
11+
// Content-Type MIME of the most common data formats.
12+
const (
13+
MIMEJSON = "application/json"
14+
MIMEHTML = "text/html"
15+
MIMEXML = "application/xml"
16+
MIMEXML2 = "text/xml"
17+
MIMEPlain = "text/plain"
18+
MIMEPOSTForm = "application/x-www-form-urlencoded"
19+
MIMEMultipartPOSTForm = "multipart/form-data"
20+
MIMEPROTOBUF = "application/x-protobuf"
21+
MIMEMSGPACK = "application/x-msgpack"
22+
MIMEMSGPACK2 = "application/msgpack"
23+
MIMEYAML = "application/x-yaml"
24+
)
25+
26+
// Binding describes the interface which needs to be implemented for binding the
27+
// data present in the request such as JSON request body, query parameters or
28+
// the form POST.
29+
type Binding interface {
30+
Name() string
31+
Bind(*http.Request, interface{}) error
32+
}
33+
34+
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
35+
// but it reads the body from supplied bytes instead of req.Body.
36+
type BindingBody interface {
37+
Binding
38+
BindBody([]byte, interface{}) error
39+
}
40+
41+
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
42+
// but it read the Params.
43+
type BindingUri interface {
44+
Name() string
45+
BindUri(map[string][]string, interface{}) error
46+
}
47+
48+
// StructValidator is the minimal interface which needs to be implemented in
49+
// order for it to be used as the validator engine for ensuring the correctness
50+
// of the request. Gin provides a default implementation for this using
51+
// https://github.com/go-playground/validator/tree/v8.18.2.
52+
type StructValidator interface {
53+
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
54+
// If the received type is not a struct, any validation should be skipped and nil must be returned.
55+
// If the received type is a struct or pointer to a struct, the validation should be performed.
56+
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
57+
// Otherwise nil must be returned.
58+
ValidateStruct(interface{}) error
59+
60+
// Engine returns the underlying validator engine which powers the
61+
// StructValidator implementation.
62+
Engine() interface{}
63+
}
64+
65+
// Validator is the default validator which implements the StructValidator
66+
// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
67+
// under the hood.
68+
var Validator StructValidator = &defaultValidator{}
69+
70+
// These implement the Binding interface and can be used to bind the data
71+
// present in the request to struct instances.
72+
var (
73+
JSON = jsonBinding{}
74+
XML = xmlBinding{}
75+
Form = formBinding{}
76+
Query = queryBinding{}
77+
FormPost = formPostBinding{}
78+
FormMultipart = formMultipartBinding{}
79+
ProtoBuf = protobufBinding{}
80+
MsgPack = msgpackBinding{}
81+
YAML = yamlBinding{}
82+
Uri = uriBinding{}
83+
Header = headerBinding{}
84+
)
85+
86+
// Default returns the appropriate Binding instance based on the HTTP method
87+
// and the content type.
88+
func Default(method, contentType string) Binding {
89+
if method == http.MethodGet {
90+
return Form
91+
}
92+
93+
switch contentType {
94+
case MIMEJSON:
95+
return JSON
96+
case MIMEXML, MIMEXML2:
97+
return XML
98+
case MIMEPROTOBUF:
99+
return ProtoBuf
100+
case MIMEMSGPACK, MIMEMSGPACK2:
101+
return MsgPack
102+
case MIMEYAML:
103+
return YAML
104+
case MIMEMultipartPOSTForm:
105+
return FormMultipart
106+
default: // case MIMEPOSTForm:
107+
return Form
108+
}
109+
}
110+
111+
func validate(obj interface{}) error {
112+
if Validator == nil {
113+
return nil
114+
}
115+
return Validator.ValidateStruct(obj)
116+
}

binding/binding_nomsgpack.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2020 Gin Core Team. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build nomsgpack
6+
7+
package binding
8+
9+
import "net/http"
10+
11+
// Content-Type MIME of the most common data formats.
12+
const (
13+
MIMEJSON = "application/json"
14+
MIMEHTML = "text/html"
15+
MIMEXML = "application/xml"
16+
MIMEXML2 = "text/xml"
17+
MIMEPlain = "text/plain"
18+
MIMEPOSTForm = "application/x-www-form-urlencoded"
19+
MIMEMultipartPOSTForm = "multipart/form-data"
20+
MIMEPROTOBUF = "application/x-protobuf"
21+
MIMEYAML = "application/x-yaml"
22+
)
23+
24+
// Binding describes the interface which needs to be implemented for binding the
25+
// data present in the request such as JSON request body, query parameters or
26+
// the form POST.
27+
type Binding interface {
28+
Name() string
29+
Bind(*http.Request, interface{}) error
30+
}
31+
32+
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
33+
// but it reads the body from supplied bytes instead of req.Body.
34+
type BindingBody interface {
35+
Binding
36+
BindBody([]byte, interface{}) error
37+
}
38+
39+
// BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
40+
// but it read the Params.
41+
type BindingUri interface {
42+
Name() string
43+
BindUri(map[string][]string, interface{}) error
44+
}
45+
46+
// StructValidator is the minimal interface which needs to be implemented in
47+
// order for it to be used as the validator engine for ensuring the correctness
48+
// of the request. Gin provides a default implementation for this using
49+
// https://github.com/go-playground/validator/tree/v8.18.2.
50+
type StructValidator interface {
51+
// ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
52+
// If the received type is not a struct, any validation should be skipped and nil must be returned.
53+
// If the received type is a struct or pointer to a struct, the validation should be performed.
54+
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
55+
// Otherwise nil must be returned.
56+
ValidateStruct(interface{}) error
57+
58+
// Engine returns the underlying validator engine which powers the
59+
// StructValidator implementation.
60+
Engine() interface{}
61+
}
62+
63+
// Validator is the default validator which implements the StructValidator
64+
// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
65+
// under the hood.
66+
var Validator StructValidator = &defaultValidator{}
67+
68+
// These implement the Binding interface and can be used to bind the data
69+
// present in the request to struct instances.
70+
var (
71+
JSON = jsonBinding{}
72+
XML = xmlBinding{}
73+
Form = formBinding{}
74+
Query = queryBinding{}
75+
FormPost = formPostBinding{}
76+
FormMultipart = formMultipartBinding{}
77+
ProtoBuf = protobufBinding{}
78+
YAML = yamlBinding{}
79+
Uri = uriBinding{}
80+
Header = headerBinding{}
81+
)
82+
83+
// Default returns the appropriate Binding instance based on the HTTP method
84+
// and the content type.
85+
func Default(method, contentType string) Binding {
86+
if method == "GET" {
87+
return Form
88+
}
89+
90+
switch contentType {
91+
case MIMEJSON:
92+
return JSON
93+
case MIMEXML, MIMEXML2:
94+
return XML
95+
case MIMEPROTOBUF:
96+
return ProtoBuf
97+
case MIMEYAML:
98+
return YAML
99+
case MIMEMultipartPOSTForm:
100+
return FormMultipart
101+
default: // case MIMEPOSTForm:
102+
return Form
103+
}
104+
}
105+
106+
func validate(obj interface{}) error {
107+
if Validator == nil {
108+
return nil
109+
}
110+
return Validator.ValidateStruct(obj)
111+
}

binding/default_validator.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package binding
6+
7+
import (
8+
"reflect"
9+
"sync"
10+
11+
"github.com/go-playground/validator/v10"
12+
)
13+
14+
type defaultValidator struct {
15+
once sync.Once
16+
validate *validator.Validate
17+
}
18+
19+
var _ StructValidator = &defaultValidator{}
20+
21+
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
22+
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
23+
value := reflect.ValueOf(obj)
24+
valueType := value.Kind()
25+
if valueType == reflect.Ptr {
26+
valueType = value.Elem().Kind()
27+
}
28+
if valueType == reflect.Struct {
29+
v.lazyinit()
30+
if err := v.validate.Struct(obj); err != nil {
31+
return err
32+
}
33+
}
34+
return nil
35+
}
36+
37+
// Engine returns the underlying validator engine which powers the default
38+
// Validator instance. This is useful if you want to register custom validations
39+
// or struct level validations. See validator GoDoc for more info -
40+
// https://godoc.org/gopkg.in/go-playground/validator.v8
41+
func (v *defaultValidator) Engine() interface{} {
42+
v.lazyinit()
43+
return v.validate
44+
}
45+
46+
func (v *defaultValidator) lazyinit() {
47+
v.once.Do(func() {
48+
v.validate = validator.New()
49+
v.validate.SetTagName("binding")
50+
})
51+
}

binding/form.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package binding
6+
7+
import (
8+
"net/http"
9+
)
10+
11+
const defaultMemory = 32 << 20
12+
13+
type formBinding struct{}
14+
type formPostBinding struct{}
15+
type formMultipartBinding struct{}
16+
17+
func (formBinding) Name() string {
18+
return "form"
19+
}
20+
21+
func (formBinding) Bind(req *http.Request, obj interface{}) error {
22+
if err := req.ParseForm(); err != nil {
23+
return err
24+
}
25+
if err := req.ParseMultipartForm(defaultMemory); err != nil {
26+
if err != http.ErrNotMultipart {
27+
return err
28+
}
29+
}
30+
if err := mapForm(obj, req.Form); err != nil {
31+
return err
32+
}
33+
return validate(obj)
34+
}
35+
36+
func (formPostBinding) Name() string {
37+
return "form-urlencoded"
38+
}
39+
40+
func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
41+
if err := req.ParseForm(); err != nil {
42+
return err
43+
}
44+
if err := mapForm(obj, req.PostForm); err != nil {
45+
return err
46+
}
47+
return validate(obj)
48+
}
49+
50+
func (formMultipartBinding) Name() string {
51+
return "multipart/form-data"
52+
}
53+
54+
func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
55+
if err := req.ParseMultipartForm(defaultMemory); err != nil {
56+
return err
57+
}
58+
if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil {
59+
return err
60+
}
61+
62+
return validate(obj)
63+
}

0 commit comments

Comments
 (0)