podman

Форк
0
104 строки · 3.0 Кб
1
// Copyright 2015 go-swagger maintainers
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package middleware
16

17
import (
18
	"net/http"
19
	"reflect"
20

21
	"github.com/go-openapi/errors"
22
	"github.com/go-openapi/spec"
23
	"github.com/go-openapi/strfmt"
24

25
	"github.com/go-openapi/runtime"
26
)
27

28
// UntypedRequestBinder binds and validates the data from a http request
29
type UntypedRequestBinder struct {
30
	Spec         *spec.Swagger
31
	Parameters   map[string]spec.Parameter
32
	Formats      strfmt.Registry
33
	paramBinders map[string]*untypedParamBinder
34
}
35

36
// NewUntypedRequestBinder creates a new binder for reading a request.
37
func NewUntypedRequestBinder(parameters map[string]spec.Parameter, spec *spec.Swagger, formats strfmt.Registry) *UntypedRequestBinder {
38
	binders := make(map[string]*untypedParamBinder)
39
	for fieldName, param := range parameters {
40
		binders[fieldName] = newUntypedParamBinder(param, spec, formats)
41
	}
42
	return &UntypedRequestBinder{
43
		Parameters:   parameters,
44
		paramBinders: binders,
45
		Spec:         spec,
46
		Formats:      formats,
47
	}
48
}
49

50
// Bind perform the databinding and validation
51
func (o *UntypedRequestBinder) Bind(request *http.Request, routeParams RouteParams, consumer runtime.Consumer, data interface{}) error {
52
	val := reflect.Indirect(reflect.ValueOf(data))
53
	isMap := val.Kind() == reflect.Map
54
	var result []error
55
	debugLog("binding %d parameters for %s %s", len(o.Parameters), request.Method, request.URL.EscapedPath())
56
	for fieldName, param := range o.Parameters {
57
		binder := o.paramBinders[fieldName]
58
		debugLog("binding parameter %s for %s %s", fieldName, request.Method, request.URL.EscapedPath())
59
		var target reflect.Value
60
		if !isMap {
61
			binder.Name = fieldName
62
			target = val.FieldByName(fieldName)
63
		}
64

65
		if isMap {
66
			tpe := binder.Type()
67
			if tpe == nil {
68
				if param.Schema.Type.Contains("array") {
69
					tpe = reflect.TypeOf([]interface{}{})
70
				} else {
71
					tpe = reflect.TypeOf(map[string]interface{}{})
72
				}
73
			}
74
			target = reflect.Indirect(reflect.New(tpe))
75
		}
76

77
		if !target.IsValid() {
78
			result = append(result, errors.New(500, "parameter name %q is an unknown field", binder.Name))
79
			continue
80
		}
81

82
		if err := binder.Bind(request, routeParams, consumer, target); err != nil {
83
			result = append(result, err)
84
			continue
85
		}
86

87
		if binder.validator != nil {
88
			rr := binder.validator.Validate(target.Interface())
89
			if rr != nil && rr.HasErrors() {
90
				result = append(result, rr.AsError())
91
			}
92
		}
93

94
		if isMap {
95
			val.SetMapIndex(reflect.ValueOf(param.Name), target)
96
		}
97
	}
98

99
	if len(result) > 0 {
100
		return errors.CompositeValidationError(result...)
101
	}
102

103
	return nil
104
}
105

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.