Skip to content

Commit

Permalink
Resulve issue project-flogo#224 GetRef() in support package should re…
Browse files Browse the repository at this point in the history
…move 'vendor' path

allows trigger developer to pass request parameters to handler w/o going through output mapper
  • Loading branch information
yxuco committed Dec 3, 2020
1 parent d0ed3d0 commit 41e5656
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
16 changes: 14 additions & 2 deletions support/ref.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package support

import "reflect"
import (
"reflect"
"strings"
)

func GetRef(contrib interface{}) string {
v := reflect.ValueOf(contrib)
Expand All @@ -11,9 +14,18 @@ func GetRef(contrib interface{}) string {

ref := v.Type().PkgPath()

return ref
return fixPkgPathVendoring(ref)
}

type HasRef interface {
Ref() string
}

// fixes vendored paths
func fixPkgPathVendoring(pkgPath string) string {
const vendor = "/vendor/"
if i := strings.LastIndex(pkgPath, vendor); i != -1 {
return pkgPath[i+len(vendor):]
}
return pkgPath
}
19 changes: 19 additions & 0 deletions trigger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "RequestParams"

// 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
}
7 changes: 7 additions & 0 deletions trigger/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,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 {
Expand Down

0 comments on commit 41e5656

Please sign in to comment.