func (e *Encoder) HandlerBuilder(handler interface{}) (scaffold.Handler, error) {
	typ := reflect.TypeOf(handler)

	if typ.Kind() != reflect.Func {
		return nil, errors.New("Invalid handler, must be a function")
	}

	if typ.NumIn() == 0 {
		return nil, errors.New("Invalid handler, must have at lease one argument")
	}

	params := make([]func(context.Context, http.ResponseWriter, *http.Request) (reflect.Value, error), typ.NumIn())

	reqTyp := typ.In(0)
	if reqTyp.Kind() != reflect.Struct {
		return nil, errors.New("Invalid handler, first argument must be a struct")
	}

	params[0] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
		parser := e.Parser(r)
		val := reflect.New(reqTyp)
		err := parser.Parse(val.Interface(), r)
		return val.Elem(), err
	}

	for i := 1; i < typ.NumIn(); i++ {
		switch typ.In(i) {
		case reflect.TypeOf((*context.Context)(nil)).Elem():
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(ctx), nil
			}
		case reflect.TypeOf((*http.ResponseWriter)(nil)).Elem():
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(w), nil
			}
		case reflect.TypeOf((*http.Request)(nil)):
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(r), nil
			}
		default:
			return nil, errors.New("Invalid handler, invalid argument type")
		}
	}

	if typ.NumOut() != 2 {
		return nil, errors.New("Invalid handler, must have at two return parameters")
	}

	if typ.Out(0).Kind() != reflect.Struct {
		return nil, errors.New("Invalid handler, first return parameter must be a struct")
	}

	if typ.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
		return nil, errors.New("Invalid handler, second return parameter must be an error")
	}

	return &caller{
		e:      e,
		params: params,
		caller: reflect.ValueOf(handler),
	}, nil
}
func (e *Encoder) HandlerBuilder(handler interface{}) (scaffold.Handler, error) {
	typ := reflect.TypeOf(handler)

	if typ.Kind() != reflect.Func {
		return nil, errors.New("Invalid handler, must be a function")
	}

	if typ.NumIn() == 0 {
		return nil, errors.New("Invalid handler, must have at lease one argument")
	}

	params := make([]func(context.Context, http.ResponseWriter, *http.Request) (reflect.Value, error), typ.NumIn())

	reqTyp := typ.In(0)
	if reqTyp.Kind() != reflect.Struct {
		return nil, errors.New("Invalid handler, first argument must be a struct")
	}

	params[0] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
		parser := e.Parser(r)
		val := reflect.New(reqTyp)
		err := parser.Parse(val.Interface(), r)
		return val.Elem(), err
	}

	for i := 1; i < typ.NumIn(); i++ {
		switch typ.In(i) {
		case reflect.TypeOf((*context.Context)(nil)).Elem():
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(ctx), nil
			}
		case reflect.TypeOf((*http.ResponseWriter)(nil)).Elem():
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(w), nil
			}
		case reflect.TypeOf((*http.Request)(nil)):
			params[i] = func(ctx context.Context, w http.ResponseWriter, r *http.Request) (reflect.Value, error) {
				return reflect.ValueOf(r), nil
			}
		default:
			return nil, errors.New("Invalid handler, invalid argument type")
		}
	}

	if typ.NumOut() != 2 {
		return nil, errors.New("Invalid handler, must have at two return parameters")
	}

	if typ.Out(0).Kind() != reflect.Struct {
		return nil, errors.New("Invalid handler, first return parameter must be a struct")
	}

	if typ.Out(1) != reflect.TypeOf((*error)(nil)).Elem() {
		return nil, errors.New("Invalid handler, second return parameter must be an error")
	}

	return &caller{
		e:      e,
		params: params,
		caller: reflect.ValueOf(handler),
	}, nil
}
func ( e * Encoder ) HandlerBuilder ( handler interface { } ) ( scaffold . Handler , error ) { typ := reflect . TypeOf ( handler ) if typ . Kind ( ) != reflect . Func { return nil , errors . New ( " " ) } if typ . NumIn ( ) == 0 { return nil , errors . New ( " " ) } params := make ( [ ] func ( context . Context , http . ResponseWriter , * http . Request ) ( reflect . Value , error ) , typ . NumIn ( ) ) reqTyp := typ . In ( 0 ) if reqTyp . Kind ( ) != reflect . Struct { return nil , errors . New ( " " ) } params [ 0 ] = func ( ctx context . Context , w http . ResponseWriter , r * http . Request ) ( reflect . Value , error ) { parser := e . Parser ( r ) val := reflect . New ( reqTyp ) err := parser . Parse ( val . Interface ( ) , r ) return val . Elem ( ) , err } for i := 1 ; i < typ . NumIn ( ) ; i ++ { switch typ . In ( i ) { case reflect . TypeOf ( ( * context . Context ) ( nil ) ) . Elem ( ) : params [ i ] = func ( ctx context . Context , w http . ResponseWriter , r * http . Request ) ( reflect . Value , error ) { return reflect . ValueOf ( ctx ) , nil } case reflect . TypeOf ( ( * http . ResponseWriter ) ( nil ) ) . Elem ( ) : params [ i ] = func ( ctx context . Context , w http . ResponseWriter , r * http . Request ) ( reflect . Value , error ) { return reflect . ValueOf ( w ) , nil } case reflect . TypeOf ( ( * http . Request ) ( nil ) ) : params [ i ] = func ( ctx context . Context , w http . ResponseWriter , r * http . Request ) ( reflect . Value , error ) { return reflect . ValueOf ( r ) , nil } default : return nil , errors . New ( " " ) } } if typ . NumOut ( ) != 2 { return nil , errors . New ( " " ) } if typ . Out ( 0 ) . Kind ( ) != reflect . Struct { return nil , errors . New ( " " ) } if typ . Out ( 1 ) != reflect . TypeOf ( ( * error ) ( nil ) ) . Elem ( ) { return nil , errors . New ( " " ) } return & caller { e : e , params : params , caller : reflect . ValueOf ( handler ) , } , nil }
// HandlerBuilder can be used in scaffold to create handlers based on function
HandlerBuilder can be used in scaffold to create handlers based on function