From 4c5df47f031030ff5b7e091444f41d4248ca6acc Mon Sep 17 00:00:00 2001 From: Jan Semmelink <jan@uafrica.com> Date: Fri, 15 Oct 2021 12:51:40 +0200 Subject: [PATCH] Remove restrictions on handler response type - any type that can marshal to JSON is ok. --- api/context.go | 6 +++++- api/handler.go | 14 -------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/api/context.go b/api/context.go index 26e7fb2..637b60a 100644 --- a/api/context.go +++ b/api/context.go @@ -150,7 +150,6 @@ func (ctx apiContext) GetRequestBody(requestStructType reflect.Type) (interface{ return nil, errors.Wrapf(err, "invalid request body") } } - return requestStructValuePtr.Elem().Interface(), nil } @@ -175,6 +174,11 @@ func (ctx *apiContext) applyClaim(name string, valuePtr interface{}) error { } func (ctx *apiContext) setClaim(name string, structType reflect.Type, structValue reflect.Value) error { + if len(ctx.Claim()) == 0 { + ctx.Debugf("NO CLAIM to apply to %s of type (%s)", name, structType.Name()) + return nil + } + for fieldName, claimValue := range ctx.Claim() { if field := structValue.FieldByName(fieldName); field.IsValid() { if err := reflection.SetValue(field, claimValue); err != nil { diff --git a/api/handler.go b/api/handler.go index a258079..3432cfb 100644 --- a/api/handler.go +++ b/api/handler.go @@ -60,20 +60,6 @@ func NewHandler(fnc interface{}) (handler, error) { h.RequestBodyType = fncType.In(2) } - //if 2 results, first must be response struct or array of response structs that will be marshalled to JSON - if fncType.NumOut() > 1 { - if fncType.Out(0).Kind() == reflect.Slice { - if err := validateStructType(fncType.Out(0).Elem()); err != nil { - return h, errors.Errorf("first result %v is not valid response []struct type", fncType.Out(0)) - } - } else { - if err := validateStructType(fncType.Out(0)); err != nil { - return h, errors.Errorf("first result %v is not valid response struct type", fncType.Out(0)) - } - } - h.ResponseType = fncType.Out(0) - } - //last result must be error if _, ok := reflect.New(fncType.Out(fncType.NumOut() - 1)).Interface().(*error); !ok { return h, errors.Errorf("last result %v is not error type", fncType.Out(fncType.NumOut()-1)) -- GitLab