Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
1. Params -> MoreParams
2. Add router::no_default_params config
  • Loading branch information
andeya committed Aug 27, 2018
1 parent d7bf158 commit ed19f78
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 27 deletions.
12 changes: 7 additions & 5 deletions apiware/apiware.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type (
ParamNameMapper
Pathdecoder
Bodydecoder
UseDefaultValues bool
}

// Pathdecoder parses path params function, return pathParams of KV type
Expand All @@ -36,11 +37,12 @@ type (
// Parse and store the struct object, requires a struct pointer,
// if `paramNameMapper` is nil, `paramNameMapper=toSnake`,
// if `bodydecoder` is nil, `bodydecoder=bodyJONS`,
func New(pathdecoder Pathdecoder, bodydecoder Bodydecoder, paramNameMapper ParamNameMapper) *Apiware {
func New(pathdecoder Pathdecoder, bodydecoder Bodydecoder, paramNameMapper ParamNameMapper, useDefaultValues bool) *Apiware {
return &Apiware{
ParamNameMapper: paramNameMapper,
Pathdecoder: pathdecoder,
Bodydecoder: bodydecoder,
ParamNameMapper: paramNameMapper,
Pathdecoder: pathdecoder,
Bodydecoder: bodydecoder,
UseDefaultValues: useDefaultValues,
}
}

Expand All @@ -49,7 +51,7 @@ func New(pathdecoder Pathdecoder, bodydecoder Bodydecoder, paramNameMapper Param
func (a *Apiware) Register(structPointers ...interface{}) error {
var errStr string
for _, obj := range structPointers {
err := Register(obj, a.ParamNameMapper, a.Bodydecoder)
err := Register(obj, a.ParamNameMapper, a.Bodydecoder, a.UseDefaultValues)
if err != nil {
errStr += err.Error() + "\n"
}
Expand Down
6 changes: 4 additions & 2 deletions apiware/paramapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func NewParamsAPI(
structPointer interface{},
paramNameMapper ParamNameMapper,
bodydecoder Bodydecoder,
useDefaultValues bool,
) (
*ParamsAPI,
error,
Expand Down Expand Up @@ -109,7 +110,7 @@ func NewParamsAPI(
return nil, err
}

if !reflect.DeepEqual(reflect.New(paramsAPI.structType).Interface(), paramsAPI.rawStructPointer) {
if useDefaultValues && !reflect.DeepEqual(reflect.New(paramsAPI.structType).Interface(), paramsAPI.rawStructPointer) {
buf := bytes.NewBuffer(nil)
err = gob.NewEncoder(buf).EncodeValue(v)
if err == nil {
Expand All @@ -128,8 +129,9 @@ func Register(
structPointer interface{},
paramNameMapper ParamNameMapper,
bodydecoder Bodydecoder,
useDefaultValues bool,
) error {
_, err := NewParamsAPI(structPointer, paramNameMapper, bodydecoder)
_, err := NewParamsAPI(structPointer, paramNameMapper, bodydecoder, useDefaultValues)
return err
}

Expand Down
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ type (
HandleMethodNotAllowed bool `ini:"handle_method_not_allowed" comment:"Returns 405 if the requested method does not exist, otherwise returns 404"`
// If enabled, the router automatically replies to OPTIONS requests.
// Custom OPTIONS handlers take priority over automatic replies.
HandleOPTIONS bool `ini:"handle_options" comment:"Automatic response OPTIONS request, you can set the default Handler in faygo"`
DefaultUpload bool `ini:"default_upload" comment:"Automatically register the default router: /upload/*filepath"`
DefaultStatic bool `ini:"default_static" comment:"Automatically register the default router: /static/*filepath"`
HandleOPTIONS bool `ini:"handle_options" comment:"Automatic response OPTIONS request, you can set the default Handler in faygo"`
NoDefaultParams bool `ini:"no_default_params" comment:"Don't assign default parameter values based on initial value of the routing handler"`
DefaultUpload bool `ini:"default_upload" comment:"Automatically register the default router: /upload/*filepath"`
DefaultStatic bool `ini:"default_static" comment:"Automatically register the default router: /static/*filepath"`
}
// GzipConfig is the config about gzip
GzipConfig struct {
Expand Down
17 changes: 9 additions & 8 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ type (
Doc struct {
Note string `json:"note" xml:"note"`
Return interface{} `json:"return,omitempty" xml:"return,omitempty"`
Params []ParamInfo `json:"params,omitempty" xml:"params,omitempty"`
// MoreParams extra added parameters definition
MoreParams []ParamInfo `json:"more_params,omitempty" xml:"more_params,omitempty"`
}
// Notes implementation notes of a response
Notes struct {
Expand Down Expand Up @@ -108,7 +109,7 @@ var (
var _ APIDoc = new(apiHandler)

// ToAPIHandler tries converts it to an *apiHandler.
func ToAPIHandler(handler Handler) (*apiHandler, error) {
func ToAPIHandler(handler Handler, noDefaultParams bool) (*apiHandler, error) {
v := reflect.Indirect(reflect.ValueOf(handler))
if v.Kind() != reflect.Struct {
return nil, ErrNotStructPtr
Expand All @@ -120,7 +121,7 @@ func ToAPIHandler(handler Handler) (*apiHandler, error) {
bodydecoder = h.Decode
}

paramsAPI, err := apiware.NewParamsAPI(structPointer, global.paramNameMapper, bodydecoder)
paramsAPI, err := apiware.NewParamsAPI(structPointer, global.paramNameMapper, bodydecoder, !noDefaultParams)
if err != nil {
return nil, err
}
Expand All @@ -135,12 +136,12 @@ func ToAPIHandler(handler Handler) (*apiHandler, error) {
}

// IsHandlerWithoutPath verifies that the Handler is an HandlerWithoutPath.
func IsHandlerWithoutPath(handler Handler) bool {
func IsHandlerWithoutPath(handler Handler, noDefaultParams bool) bool {
v := reflect.Indirect(reflect.ValueOf(handler))
if v.Kind() != reflect.Struct {
return true
}
paramsAPI, err := apiware.NewParamsAPI(v.Addr().Interface(), nil, nil)
paramsAPI, err := apiware.NewParamsAPI(v.Addr().Interface(), nil, nil, !noDefaultParams)
if err != nil {
return true
}
Expand Down Expand Up @@ -181,15 +182,15 @@ func (h *apiHandler) Doc() Doc {
Desc: param.Description(),
Model: param.Raw(),
}
for i, p := range doc.Params {
for i, p := range doc.MoreParams {
if p.Name == info.Name {
doc.Params[i] = info
doc.MoreParams[i] = info
had = true
break
}
}
if !had {
doc.Params = append(doc.Params, info)
doc.MoreParams = append(doc.MoreParams, info)
}
}
return doc
Expand Down
6 changes: 3 additions & 3 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func WrapDoc(fn HandlerFunc, note string, ret interface{}, params ...ParamInfo)
return &docWrap{
Handler: fn,
doc: Doc{
Note: note,
Return: ret,
Params: params,
Note: note,
Return: ret,
MoreParams: params,
},
}
}
Expand Down
8 changes: 4 additions & 4 deletions muxapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (mux *MuxAPI) Use(handlers ...Handler) *MuxAPI {
errStr := "For using middleware, handler cannot be nil:" + reflect.TypeOf(h).String()
mux.frame.Log().Panicf("%s\n", errStr)
}
if !IsHandlerWithoutPath(h) {
if !IsHandlerWithoutPath(h, mux.frame.config.Router.NoDefaultParams) {
errStr := "For using middleware, the handlers can not bind the path parameter:" + reflect.TypeOf(h).String()
mux.frame.Log().Panicf("%s\n", errStr)
}
Expand All @@ -328,7 +328,7 @@ func (mux *MuxAPI) comb() {
mux.paramInfos = mux.paramInfos[:0]
mux.notes = mux.notes[:0]
for i, handler := range mux.handlers {
h, err := ToAPIHandler(handler)
h, err := ToAPIHandler(handler, mux.frame.config.Router.NoDefaultParams)
if err != nil {
if err == ErrNotStructPtr || err == ErrNoParamHandler {
// Get the information for apidoc
Expand All @@ -337,7 +337,7 @@ func (mux *MuxAPI) comb() {
if docinfo.Note != "" || docinfo.Return != nil {
mux.notes = append(mux.notes, Notes{Note: docinfo.Note, Return: docinfo.Return})
}
for _, param := range docinfo.Params {
for _, param := range docinfo.MoreParams {
// The path parameter must be a required parameter.
if param.In == "path" {
param.Required = true
Expand All @@ -359,7 +359,7 @@ func (mux *MuxAPI) comb() {
if docinfo.Note != "" || docinfo.Return != nil {
mux.notes = append(mux.notes, Notes{Note: docinfo.Note, Return: docinfo.Return})
}
for _, param := range docinfo.Params {
for _, param := range docinfo.MoreParams {
// The path parameter must be a required parameter.
if param.In == "path" {
param.Required = true
Expand Down
2 changes: 2 additions & 0 deletions samples/demo/config/myapp1_1.0.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ handle_options = true
default_upload = true
; Automatically register the default router: /static/*filepath
default_static = true
; Don't assign default parameter values based on initial value of the routing handler
no_default_params = false

; XSRF security section
[xsrf]
Expand Down
2 changes: 2 additions & 0 deletions samples/demo/config/myapp2_1.0.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ handle_options = true
default_upload = false
; Automatically register the default router: /static/*filepath
default_static = false
; Don't assign default parameter values based on initial value of the routing handler
no_default_params = false

; XSRF security section
[xsrf]
Expand Down
2 changes: 1 addition & 1 deletion samples/demo/handler/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *Param) Doc() faygo.Doc {
Code: 1,
Info: "success",
},
Params: []faygo.ParamInfo{
MoreParams: []faygo.ParamInfo{
{
Name: "additional",
In: "path",
Expand Down
2 changes: 1 addition & 1 deletion samples/demo/handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (Search) Doc() faygo.Doc {
return faygo.Doc{
Note: "reverse proxy",
Return: nil,
Params: []faygo.ParamInfo{
MoreParams: []faygo.ParamInfo{
{
Name: "q",
In: "query",
Expand Down

0 comments on commit ed19f78

Please sign in to comment.