commit
73c0c6d8f7
@ -1,4 +1,5 @@
|
|||||||
# developing
|
# developing
|
||||||
|
- Fix 4396: Add context.param module into adapter. [4398](https://github.com/beego/beego/pull/4398)
|
||||||
- Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391)
|
- Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391)
|
||||||
- Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385)
|
- Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385)
|
||||||
- Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385)
|
- Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385)
|
||||||
|
|||||||
18
adapter/context/param/conv.go
Normal file
18
adapter/context/param/conv.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package param
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
beecontext "github.com/beego/beego/v2/adapter/context"
|
||||||
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
|
"github.com/beego/beego/v2/server/web/context/param"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConvertParams converts http method params to values that will be passed to the method controller as arguments
|
||||||
|
func ConvertParams(methodParams []*MethodParam, methodType reflect.Type, ctx *beecontext.Context) (result []reflect.Value) {
|
||||||
|
nps := make([]*param.MethodParam, 0, len(methodParams))
|
||||||
|
for _, mp := range methodParams {
|
||||||
|
nps = append(nps, (*param.MethodParam)(mp))
|
||||||
|
}
|
||||||
|
return param.ConvertParams(nps, methodType, (*context.Context)(ctx))
|
||||||
|
}
|
||||||
40
adapter/context/param/conv_test.go
Normal file
40
adapter/context/param/conv_test.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2020 beego
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package param
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/beego/beego/v2/adapter/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Demo(i int) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvertParams(t *testing.T) {
|
||||||
|
res := ConvertParams(nil, reflect.TypeOf(Demo), context.NewContext())
|
||||||
|
assert.Equal(t, 0, len(res))
|
||||||
|
ctx := context.NewContext()
|
||||||
|
ctx.Input.RequestBody = []byte("11")
|
||||||
|
res = ConvertParams([]*MethodParam{
|
||||||
|
New("A", InBody),
|
||||||
|
}, reflect.TypeOf(Demo), ctx)
|
||||||
|
assert.Equal(t, int64(11), res[0].Int())
|
||||||
|
}
|
||||||
|
|
||||||
29
adapter/context/param/methodparams.go
Normal file
29
adapter/context/param/methodparams.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package param
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/beego/beego/v2/server/web/context/param"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MethodParam keeps param information to be auto passed to controller methods
|
||||||
|
type MethodParam param.MethodParam
|
||||||
|
|
||||||
|
// New creates a new MethodParam with name and specific options
|
||||||
|
func New(name string, opts ...MethodParamOption) *MethodParam {
|
||||||
|
newOps := make([]param.MethodParamOption, 0, len(opts))
|
||||||
|
for _, o := range opts {
|
||||||
|
newOps = append(newOps, oldMpoToNew(o))
|
||||||
|
}
|
||||||
|
return (*MethodParam)(param.New(name, newOps...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make creates an array of MethodParmas or an empty array
|
||||||
|
func Make(list ...*MethodParam) []*MethodParam {
|
||||||
|
if len(list) > 0 {
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mp *MethodParam) String() string {
|
||||||
|
return (*param.MethodParam)(mp).String()
|
||||||
|
}
|
||||||
34
adapter/context/param/methodparams_test.go
Normal file
34
adapter/context/param/methodparams_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2020 beego
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package param
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMethodParam_String(t *testing.T) {
|
||||||
|
method := New("myName", IsRequired, InHeader, Default("abc"))
|
||||||
|
s := method.String()
|
||||||
|
assert.Equal(t, `param.New("myName", param.IsRequired, param.InHeader, param.Default("abc"))`, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMake(t *testing.T) {
|
||||||
|
res := Make()
|
||||||
|
assert.Equal(t, 0, len(res))
|
||||||
|
res = Make(New("myName", InBody))
|
||||||
|
assert.Equal(t, 1, len(res))
|
||||||
|
}
|
||||||
45
adapter/context/param/options.go
Normal file
45
adapter/context/param/options.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package param
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/beego/beego/v2/server/web/context/param"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MethodParamOption defines a func which apply options on a MethodParam
|
||||||
|
type MethodParamOption func(*MethodParam)
|
||||||
|
|
||||||
|
// IsRequired indicates that this param is required and can not be omitted from the http request
|
||||||
|
var IsRequired MethodParamOption = func(p *MethodParam) {
|
||||||
|
param.IsRequired((*param.MethodParam)(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// InHeader indicates that this param is passed via an http header
|
||||||
|
var InHeader MethodParamOption = func(p *MethodParam) {
|
||||||
|
param.InHeader((*param.MethodParam)(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// InPath indicates that this param is part of the URL path
|
||||||
|
var InPath MethodParamOption = func(p *MethodParam) {
|
||||||
|
param.InPath((*param.MethodParam)(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// InBody indicates that this param is passed as an http request body
|
||||||
|
var InBody MethodParamOption = func(p *MethodParam) {
|
||||||
|
param.InBody((*param.MethodParam)(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default provides a default value for the http param
|
||||||
|
func Default(defaultValue interface{}) MethodParamOption {
|
||||||
|
return newMpoToOld(param.Default(defaultValue))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMpoToOld(n param.MethodParamOption) MethodParamOption {
|
||||||
|
return func(methodParam *MethodParam) {
|
||||||
|
n((*param.MethodParam)(methodParam))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func oldMpoToNew(old MethodParamOption) param.MethodParamOption {
|
||||||
|
return func(methodParam *param.MethodParam) {
|
||||||
|
old((*MethodParam)(methodParam))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user