From 4f85a3bee161d9d6cba88fe81f0d98c8e5391866 Mon Sep 17 00:00:00 2001 From: Yueming Xu Date: Thu, 8 Oct 2020 13:11:27 -0600 Subject: [PATCH] Resulve issue #224 GetRef() in support package should remove 'vendor' path allows trigger developer to pass request parameters to handler w/o going through output mapper Resulve issue #224 GetRef() in support package should remove 'vendor' path resubmit pull request to resolve issue #224 Fix #244 Trigger handler should provide option to pass request data to flow without explicit output mapping update per comments on issue #246 --- trigger/context.go | 19 +++++++++++++++++++ trigger/handler.go | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/trigger/context.go b/trigger/context.go index 4df94fac..b3c098a5 100644 --- a/trigger/context.go +++ b/trigger/context.go @@ -25,3 +25,22 @@ func HandlerFromContext(ctx context.Context) (*HandlerInfo, bool) { u, ok := ctx.Value(handlerKey).(*HandlerInfo) return u, ok } + +// This allows trigger developer to pass request parameters to handler w/o going through output mapper, e.g., +// ctx := trigger.NewContextWithValues(context.Background(), values) +// results, err := handler.Handle(ctx, triggerData.ToMap()) + +type valueKey string + +const contextValueKey valueKey = "values" + +// NewContextWithValues returns a new Context that carries specified request parameter values +func NewContextWithValues(ctx context.Context, values map[string]interface{}) context.Context { + return context.WithValue(ctx, contextValueKey, values) +} + +// ValuesFromContext extracts request parameters from a context +func ValuesFromContext(ctx context.Context) (map[string]interface{}, bool) { + values, ok := ctx.Value(contextValueKey).(map[string]interface{}) + return values, ok +} diff --git a/trigger/handler.go b/trigger/handler.go index a1e7576b..0b25d98a 100644 --- a/trigger/handler.go +++ b/trigger/handler.go @@ -192,6 +192,13 @@ func (h *handlerImpl) Handle(ctx context.Context, triggerData interface{}) (resu inputMap = triggerValues } + // extract request parameters from trigger context, and make them available in actions + if reqParams, ok := ValuesFromContext(ctx); ok { + for k, v := range reqParams { + inputMap[k] = v + } + } + if ioMd := act.act.IOMetadata(); ioMd != nil { for name, tv := range ioMd.Input { if val, ok := inputMap[name]; ok {