podman
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
15package middleware
16
17import (
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
29type UntypedRequestBinder struct {
30Spec *spec.Swagger
31Parameters map[string]spec.Parameter
32Formats strfmt.Registry
33paramBinders map[string]*untypedParamBinder
34}
35
36// NewUntypedRequestBinder creates a new binder for reading a request.
37func NewUntypedRequestBinder(parameters map[string]spec.Parameter, spec *spec.Swagger, formats strfmt.Registry) *UntypedRequestBinder {
38binders := make(map[string]*untypedParamBinder)
39for fieldName, param := range parameters {
40binders[fieldName] = newUntypedParamBinder(param, spec, formats)
41}
42return &UntypedRequestBinder{
43Parameters: parameters,
44paramBinders: binders,
45Spec: spec,
46Formats: formats,
47}
48}
49
50// Bind perform the databinding and validation
51func (o *UntypedRequestBinder) Bind(request *http.Request, routeParams RouteParams, consumer runtime.Consumer, data interface{}) error {
52val := reflect.Indirect(reflect.ValueOf(data))
53isMap := val.Kind() == reflect.Map
54var result []error
55debugLog("binding %d parameters for %s %s", len(o.Parameters), request.Method, request.URL.EscapedPath())
56for fieldName, param := range o.Parameters {
57binder := o.paramBinders[fieldName]
58debugLog("binding parameter %s for %s %s", fieldName, request.Method, request.URL.EscapedPath())
59var target reflect.Value
60if !isMap {
61binder.Name = fieldName
62target = val.FieldByName(fieldName)
63}
64
65if isMap {
66tpe := binder.Type()
67if tpe == nil {
68if param.Schema.Type.Contains("array") {
69tpe = reflect.TypeOf([]interface{}{})
70} else {
71tpe = reflect.TypeOf(map[string]interface{}{})
72}
73}
74target = reflect.Indirect(reflect.New(tpe))
75}
76
77if !target.IsValid() {
78result = append(result, errors.New(500, "parameter name %q is an unknown field", binder.Name))
79continue
80}
81
82if err := binder.Bind(request, routeParams, consumer, target); err != nil {
83result = append(result, err)
84continue
85}
86
87if binder.validator != nil {
88rr := binder.validator.Validate(target.Interface())
89if rr != nil && rr.HasErrors() {
90result = append(result, rr.AsError())
91}
92}
93
94if isMap {
95val.SetMapIndex(reflect.ValueOf(param.Name), target)
96}
97}
98
99if len(result) > 0 {
100return errors.CompositeValidationError(result...)
101}
102
103return nil
104}
105